Skip to content

Commit

Permalink
changed strings.HasPrefix(strings.ToLower(name) to use strings.EqualF…
Browse files Browse the repository at this point in the history
…old instead
  • Loading branch information
anatoly-kussul committed Dec 2, 2024
1 parent fc49380 commit 545761e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
8 changes: 6 additions & 2 deletions shortuuid.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ func NewWithNamespace(name string) string {
switch {
case name == "":
u = uuid.New()
case strings.HasPrefix(strings.ToLower(name), "http://"):
case hasPrefixCaseInsensitive(name, "https://"):
u = uuid.NewSHA1(uuid.NameSpaceURL, []byte(name))
case strings.HasPrefix(strings.ToLower(name), "https://"):
case hasPrefixCaseInsensitive(name, "http://"):
u = uuid.NewSHA1(uuid.NameSpaceURL, []byte(name))
default:
u = uuid.NewSHA1(uuid.NameSpaceDNS, []byte(name))
Expand All @@ -50,3 +50,7 @@ func NewWithAlphabet(abc string) string {
enc := encoder{newAlphabet(abc)}
return enc.Encode(uuid.New())
}

func hasPrefixCaseInsensitive(s, prefix string) bool {
return len(s) >= len(prefix) && strings.EqualFold(s[:len(prefix)], prefix)
}
18 changes: 18 additions & 0 deletions shortuuid_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -333,3 +333,21 @@ func BenchmarkNewWithAlphabetB16_MB(b *testing.B) {
_ = NewWithAlphabet("うえおなにぬねのウエオナニヌネノ")
}
}

func BenchmarkNewWithNamespace(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = NewWithNamespace("someaveragelengthurl")
}
}

func BenchmarkNewWithNamespaceHttp(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = NewWithNamespace("http://someaveragelengthurl.test")
}
}

func BenchmarkNewWithNamespaceHttps(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = NewWithNamespace("https://someaveragelengthurl.test")
}
}

0 comments on commit 545761e

Please sign in to comment.