Skip to content

Commit

Permalink
Merge pull request #66 from markrechler/non_regex
Browse files Browse the repository at this point in the history
remove regexp for performance
  • Loading branch information
jehiah committed May 6, 2015
2 parents c19b47a + 8a69a93 commit 0c1b556
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 11 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
language: go
sudo: false
install:
- go get github.com/bmizerany/assert
script:
Expand Down
29 changes: 18 additions & 11 deletions statsdaemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"net"
"os"
"os/signal"
"regexp"
"runtime"
"sort"
"strconv"
Expand Down Expand Up @@ -67,17 +66,25 @@ func (a *Percentiles) String() string {
return fmt.Sprintf("%v", *a)
}

var (
sanitizeFilter1 = regexp.MustCompile(`\s+`)
sanitizeFilter2 = regexp.MustCompile(`\/`)
sanitizeFilter3 = regexp.MustCompile(`[^a-zA-Z_\-0-9\.]`)
)

func sanitizeBucket(bucket string) string {
bucket = sanitizeFilter1.ReplaceAllString(bucket, "_")
bucket = sanitizeFilter2.ReplaceAllString(bucket, "-")
bucket = sanitizeFilter3.ReplaceAllString(bucket, "")
return bucket
b := make([]byte, len(bucket))
var bl int

for i := 0; i < len(bucket); i++ {
c := bucket[i]
switch {
case (c >= byte('a') && c <= byte('z')) || (c >= byte('A') && c <= byte('Z')) || (c >= byte('0') && c <= byte('9')) || c == byte('-') || c == byte('.') || c == byte('_'):
b[bl] = c
bl++
case c == byte(' '):
b[bl] = byte('_')
bl++
case c == byte('/'):
b[bl] = byte('-')
bl++
}
}
return string(b[:bl])
}

var (
Expand Down

0 comments on commit 0c1b556

Please sign in to comment.