Replace ReplaceExtension regexp with manual string operations

regexp is overkill for a simple extension replacement, just find
the last '.' and add the new extension after it.  Saves 5% cpu time
on one workload.

Change-Id: Ic299c9ec5132d15bbea2c9c7778c000d6fdf509a
This commit is contained in:
Colin Cross 2015-04-14 16:00:47 -07:00
parent 8ac0a812f1
commit 7bf6f62130

View file

@ -16,11 +16,7 @@ package pathtools
import (
"path/filepath"
"regexp"
)
var (
replaceRegexp = regexp.MustCompile(`\.[^\.]+$`)
"strings"
)
// PrefixPaths returns a list of paths consisting of prefix joined with each
@ -43,5 +39,9 @@ func ReplaceExtensions(paths []string, extension string) []string {
}
func ReplaceExtension(path string, extension string) string {
return replaceRegexp.ReplaceAllString(path, "."+extension)
dot := strings.LastIndex(path, ".")
if dot == -1 {
return path
}
return path[:dot+1] + extension
}