1
0
mirror of https://github.com/go-kratos/kratos.git synced 2025-09-16 09:16:35 +02:00

perf(config/env): use strings.LastIndexByte instead of strings.LastIndex (#3660)

* perf(config/env): use strings.LastIndexByte instead of strings.LastIndex

* test(config/env): add test cases for TestFormat

* test(config/file): add benchmark test for format function

---------

Co-authored-by: huangzw <huangzw@2345.com>
Co-authored-by: 1911860538 <alxps1911@gmail.com>
This commit is contained in:
Name
2025-07-16 14:02:40 +08:00
committed by GitHub
parent 0bf7c1ad32
commit ac92cbe570
2 changed files with 15 additions and 1 deletions

View File

@@ -3,7 +3,7 @@ package file
import "strings"
func format(name string) string {
if idx := strings.LastIndex(name, "."); idx >= 0 {
if idx := strings.LastIndexByte(name, '.'); idx >= 0 {
return name[idx+1:]
}
return ""

View File

@@ -21,6 +21,10 @@ func TestFormat(t *testing.T) {
input: ".",
expect: "",
},
{
input: "a",
expect: "",
},
{
input: "a.",
expect: "",
@@ -33,6 +37,10 @@ func TestFormat(t *testing.T) {
input: "a.b",
expect: "b",
},
{
input: "a.b.c",
expect: "c",
},
}
for _, v := range tests {
content := format(v.input)
@@ -41,3 +49,9 @@ func TestFormat(t *testing.T) {
}
}
}
func BenchmarkFormat(b *testing.B) {
for i := 0; i < b.N; i++ {
format("abc.txt")
}
}