From cf9bc2c81956dcfee50be307046cdedd6420e0e3 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Sat, 20 Jul 2013 16:50:18 -0700 Subject: [PATCH] packer: Use strings operations, faster than regexp and easy /cc @sit --- packer/cache.go | 10 +++++----- packer/cache_test.go | 1 + 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/packer/cache.go b/packer/cache.go index 9d69f0c56..91fd92593 100644 --- a/packer/cache.go +++ b/packer/cache.go @@ -4,7 +4,7 @@ import ( "crypto/sha256" "encoding/hex" "path/filepath" - "regexp" + "strings" "sync" ) @@ -72,12 +72,12 @@ func (f *FileCache) RUnlock(key string) { } func (f *FileCache) cachePath(key string, hashKey string) string { - var suffixPattern = regexp.MustCompile(`(\.\w+)$`) - matches := suffixPattern.FindStringSubmatch(key) suffix := "" - if matches != nil { - suffix = matches[0] + dotIndex := strings.LastIndex(key, ".") + if dotIndex > -1 { + suffix = key[dotIndex:len(key)] } + return filepath.Join(f.CacheDir, hashKey+suffix) } diff --git a/packer/cache_test.go b/packer/cache_test.go index 5fe6f50d0..3d2ac70dc 100644 --- a/packer/cache_test.go +++ b/packer/cache_test.go @@ -42,6 +42,7 @@ func TestFileCache(t *testing.T) { if !strings.HasSuffix(path, ".iso") { t.Fatalf("path doesn't end with suffix '%s': '%s'", ".iso", path) } + err = ioutil.WriteFile(path, []byte("data"), 0666) if err != nil { t.Fatalf("error writing: %s", err)