mirror of
https://gitea.com/gitea/gitea-mirror.git
synced 2026-03-20 03:40:27 +00:00
feat(db): Improve BuildCaseInsensitiveLike with lowercase (#36598)
Improve BuildCaseInsensitiveLike with lowercase, users are more likely to input lowercase letters, so lowercase letters are used. --------- Signed-off-by: Tyrone Yeh <siryeh@gmail.com> Co-authored-by: silverwind <me@silverwind.io> Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
This commit is contained in:
@@ -90,12 +90,12 @@ func CryptoRandomBytes(length int64) ([]byte, error) {
|
||||
return buf, err
|
||||
}
|
||||
|
||||
// ToUpperASCII returns s with all ASCII letters mapped to their upper case.
|
||||
func ToUpperASCII(s string) string {
|
||||
// ToLowerASCII returns s with all ASCII letters mapped to their lower case.
|
||||
func ToLowerASCII(s string) string {
|
||||
b := []byte(s)
|
||||
for i, c := range b {
|
||||
if 'a' <= c && c <= 'z' {
|
||||
b[i] -= 'a' - 'A'
|
||||
if 'A' <= c && c <= 'Z' {
|
||||
b[i] += 'a' - 'A'
|
||||
}
|
||||
}
|
||||
return string(b)
|
||||
|
||||
@@ -178,30 +178,26 @@ type StringTest struct {
|
||||
in, out string
|
||||
}
|
||||
|
||||
var upperTests = []StringTest{
|
||||
var lowerTests = []StringTest{
|
||||
{"", ""},
|
||||
{"ONLYUPPER", "ONLYUPPER"},
|
||||
{"abc", "ABC"},
|
||||
{"AbC123", "ABC123"},
|
||||
{"azAZ09_", "AZAZ09_"},
|
||||
{"longStrinGwitHmixofsmaLLandcAps", "LONGSTRINGWITHMIXOFSMALLANDCAPS"},
|
||||
{"long\u0250string\u0250with\u0250nonascii\u2C6Fchars", "LONG\u0250STRING\u0250WITH\u0250NONASCII\u2C6FCHARS"},
|
||||
{"\u0250\u0250\u0250\u0250\u0250", "\u0250\u0250\u0250\u0250\u0250"},
|
||||
{"a\u0080\U0010FFFF", "A\u0080\U0010FFFF"},
|
||||
{"lél", "LéL"},
|
||||
{"ABC", "abc"},
|
||||
{"AbC123_", "abc123_"},
|
||||
{"LONG\u0250string\u0250WITH\u0250non-ascii\u2C6FCHARS\u0080\uFFFF", "long\u0250string\u0250with\u0250non-ascii\u2C6Fchars\u0080\uFFFF"},
|
||||
{"lél", "lél"},
|
||||
{"LÉL", "lÉl"},
|
||||
}
|
||||
|
||||
func TestToUpperASCII(t *testing.T) {
|
||||
for _, tc := range upperTests {
|
||||
assert.Equal(t, ToUpperASCII(tc.in), tc.out)
|
||||
func TestToLowerASCII(t *testing.T) {
|
||||
for _, tc := range lowerTests {
|
||||
assert.Equal(t, ToLowerASCII(tc.in), tc.out)
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkToUpper(b *testing.B) {
|
||||
for _, tc := range upperTests {
|
||||
func BenchmarkToLower(b *testing.B) {
|
||||
for _, tc := range lowerTests {
|
||||
b.Run(tc.in, func(b *testing.B) {
|
||||
for b.Loop() {
|
||||
ToUpperASCII(tc.in)
|
||||
ToLowerASCII(tc.in)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user