Compare commits

...
3 Commits
Author SHA1 Message Date
dudaodong 22af59565e fix: fix issue #168 2024-02-06 16:59:01 +08:00
dudaodong f9e047f190 feat: add SubInBetween 2024-02-06 16:47:30 +08:00
dudaodong fa298b740d add playground demo 2024-02-01 10:41:09 +08:00
13 changed files with 152 additions and 32 deletions
+3 -1
View File
@@ -708,7 +708,7 @@ import "github.com/duke-git/lancet/v2/fileutil"
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/fileutil.md#WriteCsvFile)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/fileutil.md#WriteCsvFile)]
- **<big>WriteMapsToCsv</big>** : write slice of map to csv file. - **<big>WriteMapsToCsv</big>** : write slice of map to csv file.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/fileutil.md#WriteMapsToCsv)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/fileutil.md#WriteMapsToCsv)]
[[play](https://go.dev/play/p/dAXm58Q5U1o)] [[play](https://go.dev/play/p/umAIomZFV1c)]
- **<big>WriteBytesToFile</big>** : write bytes to target file. - **<big>WriteBytesToFile</big>** : write bytes to target file.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/fileutil.md#WriteBytesToFile)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/fileutil.md#WriteBytesToFile)]
[[play](https://go.dev/play/p/s7QlDxMj3P8)] [[play](https://go.dev/play/p/s7QlDxMj3P8)]
@@ -1182,8 +1182,10 @@ import "github.com/duke-git/lancet/v2/slice"
[[play](https://go.dev/play/p/v2U2deugKuV)] [[play](https://go.dev/play/p/v2U2deugKuV)]
- **<big>DeleteAt</big>** : delete the element of slice at index. - **<big>DeleteAt</big>** : delete the element of slice at index.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/slice.md#DeleteAt)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/slice.md#DeleteAt)]
[[play](https://go.dev/play/p/800B1dPBYyd)]
- **<big>DeleteRange</big>** : delete the element of slice from start index to end indexexclude). - **<big>DeleteRange</big>** : delete the element of slice from start index to end indexexclude).
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/slice.md#DeleteRange)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/slice.md#DeleteRange)]
[[play](https://go.dev/play/p/945HwiNrnle)]
- **<big>Drop</big>** : drop n elements from the start of a slice. - **<big>Drop</big>** : drop n elements from the start of a slice.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/slice.md#Drop)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/slice.md#Drop)]
[[play](https://go.dev/play/p/jnPO2yQsT8H)] [[play](https://go.dev/play/p/jnPO2yQsT8H)]
+3 -1
View File
@@ -709,7 +709,7 @@ import "github.com/duke-git/lancet/v2/fileutil"
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/fileutil.md#WriteCsvFile)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/fileutil.md#WriteCsvFile)]
- **<big>WriteMapsToCsv</big>** : 将map切片写入csv文件中。 - **<big>WriteMapsToCsv</big>** : 将map切片写入csv文件中。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/fileutil.md#WriteMapsToCsv)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/fileutil.md#WriteMapsToCsv)]
[[play](https://go.dev/play/p/dAXm58Q5U1o)] [[play](https://go.dev/play/p/umAIomZFV1c)]
- **<big>WriteBytesToFile</big>** : 将 bytes 写入文件。 - **<big>WriteBytesToFile</big>** : 将 bytes 写入文件。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/fileutil.md#WriteBytesToFile)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/fileutil.md#WriteBytesToFile)]
[[play](https://go.dev/play/p/s7QlDxMj3P8)] [[play](https://go.dev/play/p/s7QlDxMj3P8)]
@@ -1181,8 +1181,10 @@ import "github.com/duke-git/lancet/v2/slice"
[[play](https://go.dev/play/p/v2U2deugKuV)] [[play](https://go.dev/play/p/v2U2deugKuV)]
- **<big>DeleteAt</big>** : 删除切片中指定索引到的元素。 - **<big>DeleteAt</big>** : 删除切片中指定索引到的元素。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/slice.md#DeleteAt)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/slice.md#DeleteAt)]
[[play](https://go.dev/play/p/800B1dPBYyd)]
- **<big>DeleteRange</big>** : 删除切片中指定开始索引到结束索引的元素。 - **<big>DeleteRange</big>** : 删除切片中指定开始索引到结束索引的元素。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/slice.md#DeleteRange)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/slice.md#DeleteRange)]
[[play](https://go.dev/play/p/945HwiNrnle)]
- **<big>Drop</big>** : 从切片头部删除 n 个元素。 - **<big>Drop</big>** : 从切片头部删除 n 个元素。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/slice.md#Drop)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/slice.md#Drop)]
[[play](https://go.dev/play/p/jnPO2yQsT8H)] [[play](https://go.dev/play/p/jnPO2yQsT8H)]
+2 -2
View File
@@ -759,7 +759,7 @@ func main() {
func WriteMapsToCsv(filepath string, records []map[string]any, appendToExistingFile bool, delimiter rune, headers ...[]string) error func WriteMapsToCsv(filepath string, records []map[string]any, appendToExistingFile bool, delimiter rune, headers ...[]string) error
``` ```
<b>示例:</b> <b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/umAIomZFV1c)</span></b>
```go ```go
package main package main
@@ -782,7 +782,7 @@ func main() {
} }
headers := []string{"Name", "Age", "Gender"} headers := []string{"Name", "Age", "Gender"}
err := WriteMapsToCsv(csvFilePath, records, false, ';', headers) err := fileutil.WriteMapsToCsv(csvFilePath, records, false, ';', headers)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
+2 -2
View File
@@ -525,7 +525,7 @@ func main() {
func DeleteAt[T any](slice []T, index int) []T func DeleteAt[T any](slice []T, index int) []T
``` ```
<b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/pJ-d6MUWcvK)</span></b> <b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/800B1dPBYyd)</span></b>
```go ```go
import ( import (
@@ -565,7 +565,7 @@ func main() {
func DeleteRange[T any](slice []T, start, end int) []T func DeleteRange[T any](slice []T, start, end int) []T
``` ```
<b>示例:</b> <b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/945HwiNrnle)</span></b>
```go ```go
import ( import (
+36 -2
View File
@@ -60,6 +60,7 @@ import (
- [ContainsAll](#ContainsAll) - [ContainsAll](#ContainsAll)
- [ContainsAny](#ContainsAny) - [ContainsAny](#ContainsAny)
- [RemoveWhiteSpace](#RemoveWhiteSpace) - [RemoveWhiteSpace](#RemoveWhiteSpace)
- [SubInBetween](#SubInBetween)
<div STYLE="page-break-after: always;"></div> <div STYLE="page-break-after: always;"></div>
@@ -1096,10 +1097,10 @@ import (
func main() { func main() {
result1 := strutil.IsNotBlank("") result1 := strutil.IsNotBlank("")
result2 := strutil.IsNotBlank(" ") result2 := strutil.IsNotBlank(" ")
result3 := strutil.IsNotBlank("\t\v\f\n") result3 := strutil.IsNotBlank("\t\v\f\n")
result4 := strutil.IsNotBlank(" 中文") result4 := strutil.IsNotBlank(" 中文")
result5 := strutil.IsNotBlank(" world ") result5 := strutil.IsNotBlank(" world ")
fmt.Println(result1) fmt.Println(result1)
fmt.Println(result2) fmt.Println(result2)
@@ -1462,3 +1463,36 @@ func main() {
// hello world // hello world
} }
``` ```
### <span id="SubInBetween">SubInBetween</span>
<p>获取字符串中指定的起始字符串start和终止字符串end直接的子字符串。</p>
<b>函数签名:</b>
```go
func SubInBetween(str string, start string, end string) string
```
<b>示例:</b>
```go
import (
"fmt"
"github.com/duke-git/lancet/v2/strutil"
)
func main() {
str := "abcde"
result1 := strutil.SubInBetween(str, "", "de")
result2 := strutil.SubInBetween(str, "a", "d")
fmt.Println(result1)
fmt.Println(result2)
// Output:
// abc
// bc
}
```
+1 -2
View File
@@ -45,7 +45,6 @@ import (
- [Sha](#Sha) - [Sha](#Sha)
- [ReadCsvFile](#ReadCsvFile) - [ReadCsvFile](#ReadCsvFile)
- [WriteCsvFile](#WriteCsvFile) - [WriteCsvFile](#WriteCsvFile)
- [WriteCsvFile](#WriteCsvFile)
- [WriteMapsToCsv](#WriteMapsToCsv) - [WriteMapsToCsv](#WriteMapsToCsv)
- [WriteStringToFile](#WriteStringToFile) - [WriteStringToFile](#WriteStringToFile)
- [WriteBytesToFile](#WriteBytesToFile) - [WriteBytesToFile](#WriteBytesToFile)
@@ -760,7 +759,7 @@ func main() {
func WriteMapsToCsv(filepath string, records []map[string]any, appendToExistingFile bool, delimiter rune, headers ...[]string) error func WriteMapsToCsv(filepath string, records []map[string]any, appendToExistingFile bool, delimiter rune, headers ...[]string) error
``` ```
<b>Example:</b> <b>Example:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/umAIomZFV1c)</span></b>
```go ```go
package main package main
+7 -7
View File
@@ -524,7 +524,7 @@ func main() {
func DeleteAt[T any](slice []T, index int) func DeleteAt[T any](slice []T, index int)
``` ```
<b>Example:<span style="float:right;display:inline-block;">[Run](https://go.dev/play/p/pJ-d6MUWcvK)</span></b> <b>Example:<span style="float:right;display:inline-block;">[Run](https://go.dev/play/p/800B1dPBYyd)</span></b>
```go ```go
import ( import (
@@ -563,7 +563,7 @@ func main() {
func DeleteRange[T any](slice []T, start, end int) []T func DeleteRange[T any](slice []T, start, end int) []T
``` ```
<b>Example:</b> <b>Example:<span style="float:right;display:inline-block;">[Run](https://go.dev/play/p/945HwiNrnle)</span></b>
```go ```go
import ( import (
@@ -574,11 +574,11 @@ import (
func main() { func main() {
chars := []string{"a", "b", "c", "d", "e"} chars := []string{"a", "b", "c", "d", "e"}
result1 := DeleteRange(chars, 0, 0) result1 := slice.DeleteRange(chars, 0, 0)
result2 := DeleteRange(chars, 0, 1) result2 := slice.DeleteRange(chars, 0, 1)
result3 := DeleteRange(chars, 0, 3) result3 := slice.DeleteRange(chars, 0, 3)
result4 := DeleteRange(chars, 0, 4) result4 := slice.DeleteRange(chars, 0, 4)
result5 := DeleteRange(chars, 0, 5) result5 := slice.DeleteRange(chars, 0, 5)
fmt.Println(result1) fmt.Println(result1)
fmt.Println(result2) fmt.Println(result2)
+34
View File
@@ -1463,3 +1463,37 @@ func main() {
// hello world // hello world
} }
``` ```
### <span id="SubInBetween">SubInBetween</span>
<p>Return substring between the start and end position(excluded) of source string.</p>
<b>Signature:</b>
```go
func SubInBetween(str string, start string, end string) string
```
<b>Example:</b>
```go
import (
"fmt"
"github.com/duke-git/lancet/v2/strutil"
)
func main() {
str := "abcde"
result1 := strutil.SubInBetween(str, "", "de")
result2 := strutil.SubInBetween(str, "a", "d")
fmt.Println(result1)
fmt.Println(result2)
// Output:
// abc
// bc
}
```
+1 -1
View File
@@ -754,7 +754,7 @@ func escapeCSVField(field string, delimiter rune) string {
} }
// WriteMapsToCsv write slice of map to csv file. // WriteMapsToCsv write slice of map to csv file.
// Play: todo // Play: https://go.dev/play/p/umAIomZFV1c
// filepath: Path to the CSV file. // filepath: Path to the CSV file.
// records: Slice of maps to be written. the value of map should be basic type. // records: Slice of maps to be written. the value of map should be basic type.
// the maps will be sorted by key in alphabeta order, then be written into csv file. // the maps will be sorted by key in alphabeta order, then be written into csv file.
+2 -2
View File
@@ -619,7 +619,7 @@ func IntSlice(slice any) []int {
} }
// DeleteAt delete the element of slice at index. // DeleteAt delete the element of slice at index.
// Play: https://go.dev/play/p/pJ-d6MUWcvK // Play: https://go.dev/play/p/800B1dPBYyd
func DeleteAt[T any](slice []T, index int) []T { func DeleteAt[T any](slice []T, index int) []T {
if index >= len(slice) { if index >= len(slice) {
index = len(slice) - 1 index = len(slice) - 1
@@ -633,7 +633,7 @@ func DeleteAt[T any](slice []T, index int) []T {
} }
// DeleteRange delete the element of slice from start index to end indexexclude). // DeleteRange delete the element of slice from start index to end indexexclude).
// Play: todo // Play: https://go.dev/play/p/945HwiNrnle
func DeleteRange[T any](slice []T, start, end int) []T { func DeleteRange[T any](slice []T, start, end int) []T {
result := make([]T, 0, len(slice)-(end-start)) result := make([]T, 0, len(slice)-(end-start))
+28 -8
View File
@@ -121,40 +121,48 @@ func UpperSnakeCase(s string) string {
// Before returns the substring of the source string up to the first occurrence of the specified string. // Before returns the substring of the source string up to the first occurrence of the specified string.
// Play: https://go.dev/play/p/JAWTZDS4F5w // Play: https://go.dev/play/p/JAWTZDS4F5w
func Before(s, char string) string { func Before(s, char string) string {
if s == "" || char == "" { i := strings.Index(s, char)
if s == "" || char == "" || i == -1 {
return s return s
} }
i := strings.Index(s, char)
return s[0:i] return s[0:i]
} }
// BeforeLast returns the substring of the source string up to the last occurrence of the specified string. // BeforeLast returns the substring of the source string up to the last occurrence of the specified string.
// Play: https://go.dev/play/p/pJfXXAoG_Te // Play: https://go.dev/play/p/pJfXXAoG_Te
func BeforeLast(s, char string) string { func BeforeLast(s, char string) string {
if s == "" || char == "" { i := strings.LastIndex(s, char)
if s == "" || char == "" || i == -1 {
return s return s
} }
i := strings.LastIndex(s, char)
return s[0:i] return s[0:i]
} }
// After returns the substring after the first occurrence of a specified string in the source string. // After returns the substring after the first occurrence of a specified string in the source string.
// Play: https://go.dev/play/p/RbCOQqCDA7m // Play: https://go.dev/play/p/RbCOQqCDA7m
func After(s, char string) string { func After(s, char string) string {
if s == "" || char == "" { i := strings.Index(s, char)
if s == "" || char == "" || i == -1 {
return s return s
} }
i := strings.Index(s, char)
return s[i+len(char):] return s[i+len(char):]
} }
// AfterLast returns the substring after the last occurrence of a specified string in the source string. // AfterLast returns the substring after the last occurrence of a specified string in the source string.
// Play: https://go.dev/play/p/1TegARrb8Yn // Play: https://go.dev/play/p/1TegARrb8Yn
func AfterLast(s, char string) string { func AfterLast(s, char string) string {
if s == "" || char == "" { i := strings.LastIndex(s, char)
if s == "" || char == "" || i == -1 {
return s return s
} }
i := strings.LastIndex(s, char)
return s[i+len(char):] return s[i+len(char):]
} }
@@ -574,3 +582,15 @@ func RemoveWhiteSpace(str string, repalceAll bool) string {
return strings.TrimSpace(str) return strings.TrimSpace(str)
} }
// SubInBetween return substring between the start and end position(excluded) of source string.
// Play: todo
func SubInBetween(str string, start string, end string) string {
if _, after, ok := strings.Cut(str, start); ok {
if before, _, ok := strings.Cut(after, end); ok {
return before
}
}
return ""
}
+14
View File
@@ -653,3 +653,17 @@ func ExampleRemoveWhiteSpace() {
// helloworld // helloworld
// hello world // hello world
} }
func ExampleSubInBetween() {
str := "abcde"
result1 := SubInBetween(str, "", "de")
result2 := SubInBetween(str, "a", "d")
fmt.Println(result1)
fmt.Println(result2)
// Output:
// abc
// bc
}
+19 -4
View File
@@ -230,6 +230,8 @@ func TestBefore(t *testing.T) {
assert.Equal("lancet", Before("lancet", "")) assert.Equal("lancet", Before("lancet", ""))
assert.Equal("", Before("lancet", "lancet")) assert.Equal("", Before("lancet", "lancet"))
assert.Equal("lancet", Before("lancet", "abcdef"))
assert.Equal("github.com", Before("github.com/test/lancet", "/")) assert.Equal("github.com", Before("github.com/test/lancet", "/"))
assert.Equal("github.com/", Before("github.com/test/lancet", "test")) assert.Equal("github.com/", Before("github.com/test/lancet", "test"))
} }
@@ -240,10 +242,10 @@ func TestBeforeLast(t *testing.T) {
assert := internal.NewAssert(t, "TestBeforeLast") assert := internal.NewAssert(t, "TestBeforeLast")
assert.Equal("lancet", BeforeLast("lancet", "")) assert.Equal("lancet", BeforeLast("lancet", ""))
assert.Equal("lancet", BeforeLast("lancet", "abcdef"))
assert.Equal("github.com/test", BeforeLast("github.com/test/lancet", "/")) assert.Equal("github.com/test", BeforeLast("github.com/test/lancet", "/"))
assert.Equal("github.com/test/", BeforeLast("github.com/test/test/lancet", "test")) assert.Equal("github.com/test/", BeforeLast("github.com/test/test/lancet", "test"))
assert.NotEqual("github.com/", BeforeLast("github.com/test/test/lancet", "test"))
} }
func TestAfter(t *testing.T) { func TestAfter(t *testing.T) {
@@ -255,6 +257,8 @@ func TestAfter(t *testing.T) {
assert.Equal("", After("lancet", "lancet")) assert.Equal("", After("lancet", "lancet"))
assert.Equal("test/lancet", After("github.com/test/lancet", "/")) assert.Equal("test/lancet", After("github.com/test/lancet", "/"))
assert.Equal("/lancet", After("github.com/test/lancet", "test")) assert.Equal("/lancet", After("github.com/test/lancet", "test"))
assert.Equal("lancet", After("lancet", "abcdef"))
} }
func TestAfterLast(t *testing.T) { func TestAfterLast(t *testing.T) {
@@ -266,8 +270,7 @@ func TestAfterLast(t *testing.T) {
assert.Equal("lancet", AfterLast("github.com/test/lancet", "/")) assert.Equal("lancet", AfterLast("github.com/test/lancet", "/"))
assert.Equal("/lancet", AfterLast("github.com/test/lancet", "test")) assert.Equal("/lancet", AfterLast("github.com/test/lancet", "test"))
assert.Equal("/lancet", AfterLast("github.com/test/test/lancet", "test")) assert.Equal("/lancet", AfterLast("github.com/test/test/lancet", "test"))
assert.Equal("lancet", AfterLast("lancet", "abcdef"))
assert.NotEqual("/test/lancet", AfterLast("github.com/test/test/lancet", "test"))
} }
func TestIsString(t *testing.T) { func TestIsString(t *testing.T) {
@@ -566,3 +569,15 @@ func TestRemoveWhiteSpace(t *testing.T) {
assert.Equal("helloworld", RemoveWhiteSpace(str, true)) assert.Equal("helloworld", RemoveWhiteSpace(str, true))
assert.Equal("hello world", RemoveWhiteSpace(str, false)) assert.Equal("hello world", RemoveWhiteSpace(str, false))
} }
func TestSubInBetween(t *testing.T) {
assert := internal.NewAssert(t, "TestSubInBetween")
str := "abcde"
assert.Equal("", SubInBetween(str, "", ""))
assert.Equal("ab", SubInBetween(str, "", "c"))
assert.Equal("bc", SubInBetween(str, "a", "d"))
assert.Equal("", SubInBetween(str, "a", ""))
assert.Equal("", SubInBetween(str, "a", "f"))
}