From b26ce4f25b59a23443829c673303e439614eea0f Mon Sep 17 00:00:00 2001 From: Matt Layher Date: Sat, 15 Nov 2014 21:36:10 -0500 Subject: [PATCH] More efficient string concatenation for Event.String() closes #52 --- AUTHORS | 1 + CHANGELOG.md | 1 + fsnotify.go | 26 ++++++++++++++++---------- 3 files changed, 18 insertions(+), 10 deletions(-) diff --git a/AUTHORS b/AUTHORS index 306091e..440cda4 100644 --- a/AUTHORS +++ b/AUTHORS @@ -18,6 +18,7 @@ Francisco Souza Hari haran John C Barstow Kelvin Fo +Matt Layher Nathan Youngman Paul Hammond Pursuit92 diff --git a/CHANGELOG.md b/CHANGELOG.md index 98b3aeb..2443d4d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/fsnotify.go b/fsnotify.go index 7b5233f..c899ee0 100644 --- a/fsnotify.go +++ b/fsnotify.go @@ -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:]) }