]> go.fuhry.dev Git - fsnotify.git/commitdiff
More efficient string concatenation for Event.String()
authorMatt Layher <mdlayher@gmail.com>
Sun, 16 Nov 2014 02:36:10 +0000 (21:36 -0500)
committerNathan Youngman <git@nathany.com>
Sun, 16 Nov 2014 03:14:50 +0000 (20:14 -0700)
closes #52

AUTHORS
CHANGELOG.md
fsnotify.go

diff --git a/AUTHORS b/AUTHORS
index 306091eda6a35bc6e57734e44721d72c54c7fa80..440cda441461ad8c36d44ce7dd45d9c9be9c6a7a 100644 (file)
--- a/AUTHORS
+++ b/AUTHORS
@@ -18,6 +18,7 @@ Francisco Souza <f@souza.cc>
 Hari haran <hariharan.uno@gmail.com>
 John C Barstow
 Kelvin Fo <vmirage@gmail.com>
+Matt Layher <mdlayher@gmail.com>
 Nathan Youngman <git@nathany.com>
 Paul Hammond <paul@paulhammond.org>
 Pursuit92 <JoshChase@techpursuit.net>
index 98b3aeb93c6590e7db1cb60e1871a621e0e83fc9..2443d4d9b9cbd1298759ed3d8aa8257bde50122a 100644 (file)
@@ -8,6 +8,7 @@
     * less mutexes [#13](https://github.com/go-fsnotify/fsnotify/issues/13)
     * done can be an unbuffered channel
     * remove calls to os.NewSyscallError
+* More efficient string concatenation for Event.String() [#52](https://github.com/go-fsnotify/fsnotify/pull/52) (thanks @mdlayher)
 
 ## v1.0.4 / 2014-09-07
 
index 7b5233f4bbaac3a26f23aac6f44b477f68c85d65..c899ee008387756f6fd6408d755849362a98c356 100644 (file)
@@ -7,7 +7,10 @@
 // Package fsnotify provides a platform-independent interface for file system notifications.
 package fsnotify
 
-import "fmt"
+import (
+       "bytes"
+       "fmt"
+)
 
 // Event represents a single file system notification.
 type Event struct {
@@ -30,27 +33,30 @@ const (
 // String returns a string representation of the event in the form
 // "file: REMOVE|WRITE|..."
 func (e Event) String() string {
-       events := ""
+       // Use a buffer for efficient string concatenation
+       var buffer bytes.Buffer
 
        if e.Op&Create == Create {
-               events += "|CREATE"
+               buffer.WriteString("|CREATE")
        }
        if e.Op&Remove == Remove {
-               events += "|REMOVE"
+               buffer.WriteString("|REMOVE")
        }
        if e.Op&Write == Write {
-               events += "|WRITE"
+               buffer.WriteString("|WRITE")
        }
        if e.Op&Rename == Rename {
-               events += "|RENAME"
+               buffer.WriteString("|RENAME")
        }
        if e.Op&Chmod == Chmod {
-               events += "|CHMOD"
+               buffer.WriteString("|CHMOD")
        }
 
-       if len(events) > 0 {
-               events = events[1:]
+       // If buffer remains empty, return no event names
+       if buffer.Len() == 0 {
+               return fmt.Sprintf("%q: ", e.Name)
        }
 
-       return fmt.Sprintf("%q: %s", e.Name, events)
+       // Return a list of event names, with leading pipe character stripped
+       return fmt.Sprintf("%q: %s", e.Name, buffer.String()[1:])
 }