diff --git a/strutil/string.go b/strutil/string.go index 02a2b2c..b8785fa 100644 --- a/strutil/string.go +++ b/strutil/string.go @@ -121,40 +121,48 @@ func UpperSnakeCase(s string) string { // Before returns the substring of the source string up to the first occurrence of the specified string. // Play: https://go.dev/play/p/JAWTZDS4F5w func Before(s, char string) string { - if s == "" || char == "" { + i := strings.Index(s, char) + + if s == "" || char == "" || i == -1 { return s } - i := strings.Index(s, char) + return s[0:i] } // BeforeLast returns the substring of the source string up to the last occurrence of the specified string. // Play: https://go.dev/play/p/pJfXXAoG_Te func BeforeLast(s, char string) string { - if s == "" || char == "" { + i := strings.LastIndex(s, char) + + if s == "" || char == "" || i == -1 { return s } - i := strings.LastIndex(s, char) + return s[0:i] } // After returns the substring after the first occurrence of a specified string in the source string. // Play: https://go.dev/play/p/RbCOQqCDA7m func After(s, char string) string { - if s == "" || char == "" { + i := strings.Index(s, char) + + if s == "" || char == "" || i == -1 { return s } - i := strings.Index(s, char) + return s[i+len(char):] } // AfterLast returns the substring after the last occurrence of a specified string in the source string. // Play: https://go.dev/play/p/1TegARrb8Yn func AfterLast(s, char string) string { - if s == "" || char == "" { + i := strings.LastIndex(s, char) + + if s == "" || char == "" || i == -1 { return s } - i := strings.LastIndex(s, char) + return s[i+len(char):] } diff --git a/strutil/string_test.go b/strutil/string_test.go index ce39059..10c6135 100644 --- a/strutil/string_test.go +++ b/strutil/string_test.go @@ -230,6 +230,8 @@ func TestBefore(t *testing.T) { assert.Equal("lancet", Before("lancet", "")) assert.Equal("", Before("lancet", "lancet")) + assert.Equal("lancet", Before("lancet", "abcdef")) + assert.Equal("github.com", Before("github.com/test/lancet", "/")) assert.Equal("github.com/", Before("github.com/test/lancet", "test")) } @@ -240,10 +242,10 @@ func TestBeforeLast(t *testing.T) { assert := internal.NewAssert(t, "TestBeforeLast") assert.Equal("lancet", BeforeLast("lancet", "")) + assert.Equal("lancet", BeforeLast("lancet", "abcdef")) + assert.Equal("github.com/test", BeforeLast("github.com/test/lancet", "/")) assert.Equal("github.com/test/", BeforeLast("github.com/test/test/lancet", "test")) - - assert.NotEqual("github.com/", BeforeLast("github.com/test/test/lancet", "test")) } func TestAfter(t *testing.T) { @@ -255,6 +257,8 @@ func TestAfter(t *testing.T) { assert.Equal("", After("lancet", "lancet")) assert.Equal("test/lancet", After("github.com/test/lancet", "/")) assert.Equal("/lancet", After("github.com/test/lancet", "test")) + + assert.Equal("lancet", After("lancet", "abcdef")) } func TestAfterLast(t *testing.T) { @@ -266,8 +270,7 @@ func TestAfterLast(t *testing.T) { assert.Equal("lancet", AfterLast("github.com/test/lancet", "/")) assert.Equal("/lancet", AfterLast("github.com/test/lancet", "test")) assert.Equal("/lancet", AfterLast("github.com/test/test/lancet", "test")) - - assert.NotEqual("/test/lancet", AfterLast("github.com/test/test/lancet", "test")) + assert.Equal("lancet", AfterLast("lancet", "abcdef")) } func TestIsString(t *testing.T) {