Update string.go (#496)

* Update string.go

more reasonable

* test
This commit is contained in:
mr
2024-07-18 01:08:46 +08:00
committed by GitHub
parent f6f43fb884
commit 1ca9c7b4e5
2 changed files with 3 additions and 1 deletions

View File

@@ -58,7 +58,7 @@ func Substring[T ~string](str T, offset int, length uint) T {
}
}
if offset > size {
if offset >= size {
return Empty[T]()
}

View File

@@ -76,6 +76,7 @@ func TestSubstring(t *testing.T) {
str12 := Substring("hello", -4, math.MaxUint)
str13 := Substring("🏠🐶🐱", 0, 2)
str14 := Substring("你好,世界", 0, 3)
str15 := Substring("hello", 5, 1)
is.Equal("", str1)
is.Equal("", str2)
@@ -91,6 +92,7 @@ func TestSubstring(t *testing.T) {
is.Equal("ello", str12)
is.Equal("🏠🐶", str13)
is.Equal("你好,", str14)
is.Equal("", str15)
}
func TestRuneLength(t *testing.T) {