1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00
moby--moby/pkg/stringid/stringid.go
unclejack cb08c1173f pkg/stringid: precompile regexp
Signed-off-by: Cristian Staretu <cristian.staretu@gmail.com>
2015-05-13 21:48:48 +03:00

48 lines
1.2 KiB
Go

package stringid
import (
"crypto/rand"
"encoding/hex"
"io"
"regexp"
"strconv"
)
const shortLen = 12
var validShortID = regexp.MustCompile("^[a-z0-9]{12}$")
// Determine if an arbitrary string *looks like* a short ID.
func IsShortID(id string) bool {
return validShortID.MatchString(id)
}
// TruncateID returns a shorthand version of a string identifier for convenience.
// A collision with other shorthands is very unlikely, but possible.
// In case of a collision a lookup with TruncIndex.Get() will fail, and the caller
// will need to use a langer prefix, or the full-length Id.
func TruncateID(id string) string {
trimTo := shortLen
if len(id) < shortLen {
trimTo = len(id)
}
return id[:trimTo]
}
// GenerateRandomID returns an unique id
func GenerateRandomID() string {
for {
id := make([]byte, 32)
if _, err := io.ReadFull(rand.Reader, id); err != nil {
panic(err) // This shouldn't happen
}
value := hex.EncodeToString(id)
// if we try to parse the truncated for as an int and we don't have
// an error then the value is all numberic and causes issues when
// used as a hostname. ref #3869
if _, err := strconv.ParseInt(TruncateID(value), 10, 64); err == nil {
continue
}
return value
}
}