Skip to content

Commit

Permalink
More efficient string concatenation for Event.String()
Browse files Browse the repository at this point in the history
closes howeyc#52
  • Loading branch information
mdlayher authored and nathany committed Nov 16, 2014
1 parent 3556b33 commit b26ce4f
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 10 deletions.
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ Francisco Souza <[email protected]>
Hari haran <[email protected]>
John C Barstow
Kelvin Fo <[email protected]>
Matt Layher <[email protected]>
Nathan Youngman <[email protected]>
Paul Hammond <[email protected]>
Pursuit92 <[email protected]>
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
26 changes: 16 additions & 10 deletions fsnotify.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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:])
}

0 comments on commit b26ce4f

Please sign in to comment.