Skip to content

Commit

Permalink
Stop using regexes in clean
Browse files Browse the repository at this point in the history
clean replaces special characters with '-'. Regexes are overkill for
this simple character replacement, so replace it with a simple loop.
  • Loading branch information
prashantv committed Jan 21, 2016
1 parent a8c6b0e commit 87fa6d9
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions stats/reporter.go
Expand Up @@ -3,7 +3,6 @@ package stats
import (
"flag"
"fmt"
"regexp"
"strings"
"time"
)
Expand Down Expand Up @@ -56,10 +55,17 @@ func addTagsToName(name string, tags map[string]string) string {
return strings.Join(parts, ".")
}

var specialChars = regexp.MustCompile(`[{}/\\:\s.]`)

// clean takes a string that may contain special characters, and replaces these
// characters with a '-'.
func clean(value string) string {
return specialChars.ReplaceAllString(value, "-")
newStr := make([]byte, len(value))
for i := 0; i < len(value); i++ {
switch c := value[i]; c {
case '{', '}', '/', '\\', ':', ' ', '\t', '.':
newStr[i] = '-'
default:
newStr[i] = c
}
}
return string(newStr)
}

0 comments on commit 87fa6d9

Please sign in to comment.