mirror of
https://gitee.com/johng/gf
synced 2026-06-07 02:12:11 +08:00
fix unit test cases for gstr/gconv/ghttp
This commit is contained in:
@ -338,75 +338,6 @@ func Str(haystack string, needle string) string {
|
||||
return haystack[idx + len([]byte(needle)) - 1 : ]
|
||||
}
|
||||
|
||||
// Translate characters or replace substrings.
|
||||
//
|
||||
// If the params length is 1, type is: map[string]string
|
||||
// Tr("baab", map[string]string{"ab": "01"}) will return "ba01".
|
||||
// If the params length is 2, type is: string, string
|
||||
// Tr("baab", "ab", "01") will return "1001", a => 0; b => 1.
|
||||
// See http://php.net/manual/en/function.strtr.php.
|
||||
//
|
||||
// 转换或者替换指定字符。
|
||||
func Tr(haystack string, params ...interface{}) string {
|
||||
ac := len(params)
|
||||
if ac == 1 {
|
||||
pairs := params[0].(map[string]string)
|
||||
length := len(pairs)
|
||||
if length == 0 {
|
||||
return haystack
|
||||
}
|
||||
oldnew := make([]string, length*2)
|
||||
for o, n := range pairs {
|
||||
if o == "" {
|
||||
return haystack
|
||||
}
|
||||
oldnew = append(oldnew, o, n)
|
||||
}
|
||||
return strings.NewReplacer(oldnew...).Replace(haystack)
|
||||
} else if ac == 2 {
|
||||
from := params[0].(string)
|
||||
to := params[1].(string)
|
||||
trlen, lt := len(from), len(to)
|
||||
if trlen > lt {
|
||||
trlen = lt
|
||||
}
|
||||
if trlen == 0 {
|
||||
return haystack
|
||||
}
|
||||
|
||||
str := make([]uint8, len(haystack))
|
||||
var xlat [256]uint8
|
||||
var i int
|
||||
var j uint8
|
||||
if trlen == 1 {
|
||||
for i = 0; i < len(haystack); i++ {
|
||||
if haystack[i] == from[0] {
|
||||
str[i] = to[0]
|
||||
} else {
|
||||
str[i] = haystack[i]
|
||||
}
|
||||
}
|
||||
return string(str)
|
||||
}
|
||||
// trlen != 1
|
||||
for {
|
||||
xlat[j] = j
|
||||
if j++; j == 0 {
|
||||
break
|
||||
}
|
||||
}
|
||||
for i = 0; i < trlen; i++ {
|
||||
xlat[from[i]] = to[i]
|
||||
}
|
||||
for i = 0; i < len(haystack); i++ {
|
||||
str[i] = xlat[haystack[i]]
|
||||
}
|
||||
return string(str)
|
||||
}
|
||||
|
||||
return haystack
|
||||
}
|
||||
|
||||
// Randomly shuffles a string.
|
||||
//
|
||||
// 将字符串打乱。
|
||||
|
||||
@ -159,7 +159,7 @@ func Test_WordCount(t *testing.T) {
|
||||
func Test_WordWrap(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.Assert(gstr.WordWrap("12 34", 2, "<br>"), "12<br>34")
|
||||
gtest.Assert(gstr.WordWrap("A very long woooooooooooooooooord. and something", 2, "<br>"),
|
||||
gtest.Assert(gstr.WordWrap("A very long woooooooooooooooooord. and something", 7, "<br>"),
|
||||
"A very<br>long<br>woooooooooooooooooord.<br>and<br>something")
|
||||
})
|
||||
}
|
||||
@ -184,12 +184,6 @@ func Test_Str(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Tr(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.Assert(gstr.Tr("name@example.com", "@"), "@example.com")
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Shuffle(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.Assert(len(gstr.Shuffle("123456")), 6)
|
||||
|
||||
Reference in New Issue
Block a user