1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-02-05 13:22:26 +08:00

Compare commits

..

4 Commits

Author SHA1 Message Date
dudaodong
07d1704cb2 release v1.1.7 2022-01-04 10:56:05 +08:00
dudaodong
74d262e609 Merge branch 'main' of github.com:duke-git/lancet into main 2022-01-03 20:11:32 +08:00
dudaodong
97e0789ea4 feat: add Wrap and Unwrap func 2022-01-03 20:11:15 +08:00
donutloop
bc39b0887b Filter: remove second loop and indexes slice (#12)
Use less memory and cpu resources to filter out elements from the slice.
2022-01-03 20:10:15 +08:00
5 changed files with 74 additions and 11 deletions

View File

@@ -6,7 +6,7 @@
<div align="center" style="text-align: center;">
![Go version](https://img.shields.io/badge/go-%3E%3D1.16<recommend>-9cf)
[![Release](https://img.shields.io/badge/release-1.1.6-green.svg)](https://github.com/duke-git/lancet/releases)
[![Release](https://img.shields.io/badge/release-1.1.7-green.svg)](https://github.com/duke-git/lancet/releases)
[![GoDoc](https://godoc.org/github.com//duke-git/lancet?status.svg)](https://pkg.go.dev/github.com/duke-git/lancet)
[![Go Report Card](https://goreportcard.com/badge/github.com/duke-git/lancet)](https://goreportcard.com/report/github.com/duke-git/lancet)
[![codecov](https://codecov.io/gh/duke-git/lancet/branch/main/graph/badge.svg?token=FC48T1F078)](https://codecov.io/gh/duke-git/lancet)

View File

@@ -6,7 +6,7 @@
<div align="center" style="text-align: center;">
![Go version](https://img.shields.io/badge/go-%3E%3D1.16<recommend>-9cf)
[![Release](https://img.shields.io/badge/release-1.1.6-green.svg)](https://github.com/duke-git/lancet/releases)
[![Release](https://img.shields.io/badge/release-1.1.7-green.svg)](https://github.com/duke-git/lancet/releases)
[![GoDoc](https://godoc.org/github.com//duke-git/lancet?status.svg)](https://pkg.go.dev/github.com/duke-git/lancet)
[![Go Report Card](https://goreportcard.com/badge/github.com/duke-git/lancet)](https://goreportcard.com/report/github.com/duke-git/lancet)
[![codecov](https://codecov.io/gh/duke-git/lancet/branch/main/graph/badge.svg?token=FC48T1F078)](https://codecov.io/gh/duke-git/lancet)

View File

@@ -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())
}
var indexes []int
res := reflect.MakeSlice(sv.Type(), 0, 0)
for i := 0; i < sv.Len(); i++ {
flag := fn.Call([]reflect.Value{reflect.ValueOf(i), sv.Index(i)})[0]
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()
}

View File

@@ -205,3 +205,34 @@ func ReverseStr(s string) string {
}
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
}

View File

@@ -187,9 +187,6 @@ func isString(t *testing.T, test interface{}, expected bool) {
func TestReverseStr(t *testing.T) {
reverseStr(t, "abc", "cba")
reverseStr(t, "12345", "54321")
//failed
//reverseStr(t, "abc", "abc")
}
func reverseStr(t *testing.T, test string, expected string) {
@@ -199,3 +196,42 @@ func reverseStr(t *testing.T, test string, expected string) {
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()
}
}