mirror of
https://github.com/zeromicro/go-zero.git
synced 2026-06-21 17:31:57 +08:00
fix(stringx): return empty string from FirstN when n is negative (#5620)
This commit is contained in:
@@ -34,6 +34,10 @@ func Filter(s string, remove func(r rune) bool) string {
|
|||||||
|
|
||||||
// FirstN returns first n runes from s.
|
// FirstN returns first n runes from s.
|
||||||
func FirstN(s string, n int, ellipsis ...string) string {
|
func FirstN(s string, n int, ellipsis ...string) string {
|
||||||
|
if n < 0 {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
var i int
|
var i int
|
||||||
|
|
||||||
for j := range s {
|
for j := range s {
|
||||||
|
|||||||
@@ -190,6 +190,18 @@ func TestFirstN(t *testing.T) {
|
|||||||
n: 10,
|
n: 10,
|
||||||
expect: "我是中国人",
|
expect: "我是中国人",
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
name: "negative n returns empty string",
|
||||||
|
input: "hello world",
|
||||||
|
n: -1,
|
||||||
|
expect: "",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "negative n with utf8 returns empty string",
|
||||||
|
input: "我是中国人",
|
||||||
|
n: -5,
|
||||||
|
expect: "",
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, test := range tests {
|
for _, test := range tests {
|
||||||
|
|||||||
Reference in New Issue
Block a user