1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-02-08 22:52:29 +08:00

fix: fix issue #275

This commit is contained in:
dudaodong
2024-12-04 16:37:46 +08:00
parent 0f9683a764
commit f58b706285
6 changed files with 182 additions and 7 deletions

View File

@@ -535,15 +535,22 @@ func RemoveWhiteSpace(str string, repalceAll bool) string {
// SubInBetween return substring between the start and end position(excluded) of source string.
func SubInBetween(str string, start string, end string) string {
if _, after, ok := strings.Cut(str, start); ok {
if before, _, ok := strings.Cut(after, end); ok {
if _, after, ok := Cut(str, start); ok {
if before, _, ok := Cut(after, end); ok {
return before
}
}
return ""
}
// Cut splits the string at the first occurrence of separator.
func Cut(str, sep string) (before, after string, found bool) {
if i := strings.Index(str, sep); i >= 0 {
return str[:i], str[i+len(sep):], true
}
return str, "", false
}
// HammingDistance calculates the Hamming distance between two strings.
// The Hamming distance is the number of positions at which the corresponding symbols are different.
// This func returns an error if the input strings are of unequal lengths.

View File

@@ -668,3 +668,77 @@ func TestRegexMatchAllGroups(t *testing.T) {
assert.Equal(tt.expected, result)
}
}
func TestCut(t *testing.T) {
t.Parallel()
assert := internal.NewAssert(t, "TestCut")
tests := []struct {
name string
s string
sep string
before string
after string
found bool
}{
{
name: "test with separator",
s: "hello-world",
sep: "-",
before: "hello",
after: "world",
found: true,
},
{
name: "test without separator",
s: "helloworld",
sep: "-",
before: "helloworld",
after: "",
found: false,
},
{
name: "test empty string",
s: "",
sep: "-",
before: "",
after: "",
found: false,
},
{
name: "test separator at the beginning",
s: "-hello",
sep: "-",
before: "",
after: "hello",
found: true,
},
{
name: "test separator at the end",
s: "hello-",
sep: "-",
before: "hello",
after: "",
found: true,
},
{
name: "test multiple separators",
s: "a-b-c-d",
sep: "-",
before: "a",
after: "b-c-d",
found: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
before, after, found := Cut(tt.s, tt.sep)
assert.Equal(tt.before, before)
assert.Equal(tt.after, after)
assert.Equal(tt.found, found)
})
}
}