mirror of
https://github.com/duke-git/lancet.git
synced 2026-02-08 14:42:27 +08:00
Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
07d1704cb2 | ||
|
|
74d262e609 | ||
|
|
97e0789ea4 | ||
|
|
bc39b0887b |
@@ -6,7 +6,7 @@
|
|||||||
<div align="center" style="text-align: center;">
|
<div align="center" style="text-align: center;">
|
||||||
|
|
||||||

|

|
||||||
[](https://github.com/duke-git/lancet/releases)
|
[](https://github.com/duke-git/lancet/releases)
|
||||||
[](https://pkg.go.dev/github.com/duke-git/lancet)
|
[](https://pkg.go.dev/github.com/duke-git/lancet)
|
||||||
[](https://goreportcard.com/report/github.com/duke-git/lancet)
|
[](https://goreportcard.com/report/github.com/duke-git/lancet)
|
||||||
[](https://codecov.io/gh/duke-git/lancet)
|
[](https://codecov.io/gh/duke-git/lancet)
|
||||||
|
|||||||
@@ -6,7 +6,7 @@
|
|||||||
<div align="center" style="text-align: center;">
|
<div align="center" style="text-align: center;">
|
||||||
|
|
||||||

|

|
||||||
[](https://github.com/duke-git/lancet/releases)
|
[](https://github.com/duke-git/lancet/releases)
|
||||||
[](https://pkg.go.dev/github.com/duke-git/lancet)
|
[](https://pkg.go.dev/github.com/duke-git/lancet)
|
||||||
[](https://goreportcard.com/report/github.com/duke-git/lancet)
|
[](https://goreportcard.com/report/github.com/duke-git/lancet)
|
||||||
[](https://codecov.io/gh/duke-git/lancet)
|
[](https://codecov.io/gh/duke-git/lancet)
|
||||||
|
|||||||
@@ -154,18 +154,14 @@ func Filter(slice, function interface{}) interface{} {
|
|||||||
panic("function param should be of type func(int, " + elemType.String() + ")" + reflect.ValueOf(true).Type().String())
|
panic("function param should be of type func(int, " + elemType.String() + ")" + reflect.ValueOf(true).Type().String())
|
||||||
}
|
}
|
||||||
|
|
||||||
var indexes []int
|
res := reflect.MakeSlice(sv.Type(), 0, 0)
|
||||||
for i := 0; i < sv.Len(); i++ {
|
for i := 0; i < sv.Len(); i++ {
|
||||||
flag := fn.Call([]reflect.Value{reflect.ValueOf(i), sv.Index(i)})[0]
|
flag := fn.Call([]reflect.Value{reflect.ValueOf(i), sv.Index(i)})[0]
|
||||||
if flag.Bool() {
|
if flag.Bool() {
|
||||||
indexes = append(indexes, i)
|
res = reflect.Append(res, sv.Index(i))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
res := reflect.MakeSlice(sv.Type(), len(indexes), len(indexes))
|
|
||||||
for i := range indexes {
|
|
||||||
res.Index(i).Set(sv.Index(indexes[i]))
|
|
||||||
}
|
|
||||||
return res.Interface()
|
return res.Interface()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -205,3 +205,34 @@ func ReverseStr(s string) string {
|
|||||||
}
|
}
|
||||||
return string(r)
|
return string(r)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Wrap a string with another string.
|
||||||
|
func Wrap(str string, wrapWith string) string {
|
||||||
|
if str == "" || wrapWith == "" {
|
||||||
|
return str
|
||||||
|
}
|
||||||
|
var sb strings.Builder
|
||||||
|
sb.WriteString(wrapWith)
|
||||||
|
sb.WriteString(str)
|
||||||
|
sb.WriteString(wrapWith)
|
||||||
|
|
||||||
|
return sb.String()
|
||||||
|
}
|
||||||
|
|
||||||
|
// Unwrap a given string from anther string. will change str value
|
||||||
|
func Unwrap(str string, wrapToken string) string {
|
||||||
|
if str == "" || wrapToken == "" {
|
||||||
|
return str
|
||||||
|
}
|
||||||
|
|
||||||
|
firstIndex := strings.Index(str, wrapToken)
|
||||||
|
lastIndex := strings.LastIndex(str, wrapToken)
|
||||||
|
|
||||||
|
if firstIndex == 0 && lastIndex > 0 && lastIndex <= len(str)-1 {
|
||||||
|
if len(wrapToken) <= lastIndex {
|
||||||
|
str = str[len(wrapToken):lastIndex]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return str
|
||||||
|
}
|
||||||
|
|||||||
@@ -187,9 +187,6 @@ func isString(t *testing.T, test interface{}, expected bool) {
|
|||||||
func TestReverseStr(t *testing.T) {
|
func TestReverseStr(t *testing.T) {
|
||||||
reverseStr(t, "abc", "cba")
|
reverseStr(t, "abc", "cba")
|
||||||
reverseStr(t, "12345", "54321")
|
reverseStr(t, "12345", "54321")
|
||||||
|
|
||||||
//failed
|
|
||||||
//reverseStr(t, "abc", "abc")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func reverseStr(t *testing.T, test string, expected string) {
|
func reverseStr(t *testing.T, test string, expected string) {
|
||||||
@@ -199,3 +196,42 @@ func reverseStr(t *testing.T, test string, expected string) {
|
|||||||
t.FailNow()
|
t.FailNow()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestWrap(t *testing.T) {
|
||||||
|
wrap(t, "ab", "", "ab")
|
||||||
|
wrap(t, "", "*", "")
|
||||||
|
wrap(t, "ab", "*", "*ab*")
|
||||||
|
wrap(t, "ab", "\"", "\"ab\"")
|
||||||
|
wrap(t, "ab", "'", "'ab'")
|
||||||
|
}
|
||||||
|
|
||||||
|
func wrap(t *testing.T, test string, wrapWith string, expected string) {
|
||||||
|
res := Wrap(test, wrapWith)
|
||||||
|
if res != expected {
|
||||||
|
internal.LogFailedTestInfo(t, "Wrap", test, expected, res)
|
||||||
|
t.FailNow()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestUnwrap(t *testing.T) {
|
||||||
|
unwrap(t, "", "*", "")
|
||||||
|
unwrap(t, "ab", "", "ab")
|
||||||
|
unwrap(t, "ab", "*", "ab")
|
||||||
|
unwrap(t, "**ab**", "*", "*ab*")
|
||||||
|
unwrap(t, "**ab**", "**", "ab")
|
||||||
|
unwrap(t, "\"ab\"", "\"", "ab")
|
||||||
|
unwrap(t, "*ab", "*", "*ab")
|
||||||
|
unwrap(t, "ab*", "*", "ab*")
|
||||||
|
unwrap(t, "***", "*", "*")
|
||||||
|
unwrap(t, "**", "*", "")
|
||||||
|
unwrap(t, "***", "**", "***")
|
||||||
|
unwrap(t, "**", "**", "**")
|
||||||
|
}
|
||||||
|
|
||||||
|
func unwrap(t *testing.T, test string, wrapToken string, expected string) {
|
||||||
|
res := Unwrap(test, wrapToken)
|
||||||
|
if res != expected {
|
||||||
|
internal.LogFailedTestInfo(t, "Unwrap", test+"->"+wrapToken, expected, res)
|
||||||
|
t.FailNow()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user