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:
@@ -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 ""
|
||||
|
@@ -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")
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user