add List2/List3/ListAndTrim2/ListAndTrim3 functions for package gstr (#2986)

This commit is contained in:
John Guo 2023-10-09 19:58:14 +08:00 committed by GitHub
parent 00e83fed3f
commit d6362cad16
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 211 additions and 0 deletions

55
text/gstr/gstr_list.go Normal file
View File

@ -0,0 +1,55 @@
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
package gstr
// List2 Split the `str` with `delimiter` and returns the result as two parts string.
func List2(str, delimiter string) (part1, part2 string) {
return doList2(delimiter, Split(str, delimiter))
}
// ListAndTrim2 SplitAndTrim the `str` with `delimiter` and returns the result as two parts string.
func ListAndTrim2(str, delimiter string) (part1, part2 string) {
return doList2(delimiter, SplitAndTrim(str, delimiter))
}
func doList2(delimiter string, array []string) (part1, part2 string) {
switch len(array) {
case 0:
return "", ""
case 1:
return array[0], ""
case 2:
return array[0], array[1]
default:
return array[0], Join(array[1:], delimiter)
}
}
// List3 Split the `str` with `delimiter` and returns the result as three parts string.
func List3(str, delimiter string) (part1, part2, part3 string) {
return doList3(delimiter, Split(str, delimiter))
}
// ListAndTrim3 SplitAndTrim the `str` with `delimiter` and returns the result as three parts string.
func ListAndTrim3(str, delimiter string) (part1, part2, part3 string) {
return doList3(delimiter, SplitAndTrim(str, delimiter))
}
func doList3(delimiter string, array []string) (part1, part2, part3 string) {
switch len(array) {
case 0:
return "", "", ""
case 1:
return array[0], "", ""
case 2:
return array[0], array[1], ""
case 3:
return array[0], array[1], array[2]
default:
return array[0], array[1], Join(array[2:], delimiter)
}
}

View File

@ -0,0 +1,156 @@
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
// go test *.go -bench=".*"
package gstr_test
import (
"testing"
"github.com/gogf/gf/v2/test/gtest"
"github.com/gogf/gf/v2/text/gstr"
)
func Test_List2(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
p1, p2 := gstr.List2("1:2", ":")
t.Assert(p1, "1")
t.Assert(p2, "2")
})
gtest.C(t, func(t *gtest.T) {
p1, p2 := gstr.List2("1:", ":")
t.Assert(p1, "1")
t.Assert(p2, "")
})
gtest.C(t, func(t *gtest.T) {
p1, p2 := gstr.List2("1", ":")
t.Assert(p1, "1")
t.Assert(p2, "")
})
gtest.C(t, func(t *gtest.T) {
p1, p2 := gstr.List2("", ":")
t.Assert(p1, "")
t.Assert(p2, "")
})
gtest.C(t, func(t *gtest.T) {
p1, p2 := gstr.List2("1:2:3", ":")
t.Assert(p1, "1")
t.Assert(p2, "2:3")
})
}
func Test_ListAndTrim2(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
p1, p2 := gstr.ListAndTrim2("1::2", ":")
t.Assert(p1, "1")
t.Assert(p2, "2")
})
gtest.C(t, func(t *gtest.T) {
p1, p2 := gstr.ListAndTrim2("1::", ":")
t.Assert(p1, "1")
t.Assert(p2, "")
})
gtest.C(t, func(t *gtest.T) {
p1, p2 := gstr.ListAndTrim2("1:", ":")
t.Assert(p1, "1")
t.Assert(p2, "")
})
gtest.C(t, func(t *gtest.T) {
p1, p2 := gstr.ListAndTrim2("", ":")
t.Assert(p1, "")
t.Assert(p2, "")
})
gtest.C(t, func(t *gtest.T) {
p1, p2 := gstr.ListAndTrim2("1::2::3", ":")
t.Assert(p1, "1")
t.Assert(p2, "2:3")
})
}
func Test_List3(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
p1, p2, p3 := gstr.List3("1:2:3", ":")
t.Assert(p1, "1")
t.Assert(p2, "2")
t.Assert(p3, "3")
})
gtest.C(t, func(t *gtest.T) {
p1, p2, p3 := gstr.List3("1:2:", ":")
t.Assert(p1, "1")
t.Assert(p2, "2")
t.Assert(p3, "")
})
gtest.C(t, func(t *gtest.T) {
p1, p2, p3 := gstr.List3("1:2", ":")
t.Assert(p1, "1")
t.Assert(p2, "2")
t.Assert(p3, "")
})
gtest.C(t, func(t *gtest.T) {
p1, p2, p3 := gstr.List3("1:", ":")
t.Assert(p1, "1")
t.Assert(p2, "")
t.Assert(p3, "")
})
gtest.C(t, func(t *gtest.T) {
p1, p2, p3 := gstr.List3("1", ":")
t.Assert(p1, "1")
t.Assert(p2, "")
t.Assert(p3, "")
})
gtest.C(t, func(t *gtest.T) {
p1, p2, p3 := gstr.List3("", ":")
t.Assert(p1, "")
t.Assert(p2, "")
t.Assert(p3, "")
})
gtest.C(t, func(t *gtest.T) {
p1, p2, p3 := gstr.List3("1:2:3:4", ":")
t.Assert(p1, "1")
t.Assert(p2, "2")
t.Assert(p3, "3:4")
})
}
func Test_ListAndTrim3(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
p1, p2, p3 := gstr.ListAndTrim3("1::2:3", ":")
t.Assert(p1, "1")
t.Assert(p2, "2")
t.Assert(p3, "3")
})
gtest.C(t, func(t *gtest.T) {
p1, p2, p3 := gstr.ListAndTrim3("1::2:", ":")
t.Assert(p1, "1")
t.Assert(p2, "2")
t.Assert(p3, "")
})
gtest.C(t, func(t *gtest.T) {
p1, p2, p3 := gstr.ListAndTrim3("1::2", ":")
t.Assert(p1, "1")
t.Assert(p2, "2")
t.Assert(p3, "")
})
gtest.C(t, func(t *gtest.T) {
p1, p2, p3 := gstr.ListAndTrim3("1::", ":")
t.Assert(p1, "1")
t.Assert(p2, "")
t.Assert(p3, "")
})
gtest.C(t, func(t *gtest.T) {
p1, p2, p3 := gstr.ListAndTrim3("1::", ":")
t.Assert(p1, "1")
t.Assert(p2, "")
t.Assert(p3, "")
})
gtest.C(t, func(t *gtest.T) {
p1, p2, p3 := gstr.ListAndTrim3("", ":")
t.Assert(p1, "")
t.Assert(p2, "")
t.Assert(p3, "")
})
}