Skip to content

Commit ac92cbe

Browse files
1911860538huangzw1911860538
authored
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 <[email protected]> Co-authored-by: 1911860538 <[email protected]>
1 parent 0bf7c1a commit ac92cbe

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

config/file/format.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package file
33
import "strings"
44

55
func format(name string) string {
6-
if idx := strings.LastIndex(name, "."); idx >= 0 {
6+
if idx := strings.LastIndexByte(name, '.'); idx >= 0 {
77
return name[idx+1:]
88
}
99
return ""

config/file/format_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ func TestFormat(t *testing.T) {
2121
input: ".",
2222
expect: "",
2323
},
24+
{
25+
input: "a",
26+
expect: "",
27+
},
2428
{
2529
input: "a.",
2630
expect: "",
@@ -33,6 +37,10 @@ func TestFormat(t *testing.T) {
3337
input: "a.b",
3438
expect: "b",
3539
},
40+
{
41+
input: "a.b.c",
42+
expect: "c",
43+
},
3644
}
3745
for _, v := range tests {
3846
content := format(v.input)
@@ -41,3 +49,9 @@ func TestFormat(t *testing.T) {
4149
}
4250
}
4351
}
52+
53+
func BenchmarkFormat(b *testing.B) {
54+
for i := 0; i < b.N; i++ {
55+
format("abc.txt")
56+
}
57+
}

0 commit comments

Comments
 (0)