mirror of
https://gitee.com/johng/gf.git
synced 2024-11-29 18:57:44 +08:00
add List2/List3/ListAndTrim2/ListAndTrim3
functions for package gstr
(#2986)
This commit is contained in:
parent
00e83fed3f
commit
d6362cad16
55
text/gstr/gstr_list.go
Normal file
55
text/gstr/gstr_list.go
Normal 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)
|
||||
}
|
||||
}
|
156
text/gstr/gstr_z_unit_list_test.go
Normal file
156
text/gstr/gstr_z_unit_list_test.go
Normal 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, "")
|
||||
})
|
||||
}
|
Loading…
Reference in New Issue
Block a user