1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-02-04 12:52:28 +08:00

feat: add FindAllOccurrences

This commit is contained in:
dudaodong
2025-01-10 14:03:45 +08:00
parent a0d97cf38e
commit 7653afa919
5 changed files with 119 additions and 7 deletions

View File

@@ -69,6 +69,7 @@ import (
- [TemplateReplace](#TemplateReplace)
- [RegexMatchAllGroups](#RegexMatchAllGroups)
- [ExtractContent](#ExtractContent)
- [FindAllOccurrences](#FindAllOccurrences)
<div STYLE="page-break-after: always;"></div>
@@ -1708,7 +1709,7 @@ func main() {
func RegexMatchAllGroups(pattern, str string) [][]string
```
<b>示例:<span style="float:right;display:inline-block;">[Run](https://go.dev/play/p/JZiu0RXpgN-)</span></b>
<b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/JZiu0RXpgN-)</span></b>
```go
import (
@@ -1741,7 +1742,7 @@ func main() {
func ExtractContent(s, start, end string) []string
```
<b>示例:<span style="float:right;display:inline-block;">[Run](https://go.dev/play/p/Ay9UIk7Rum9)</span></b>
<b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/Ay9UIk7Rum9)</span></b>
```go
import (
@@ -1760,3 +1761,31 @@ func main() {
// [content1 content2 content1]
}
```
### <span id="FindAllOccurrences">FindAllOccurrences</span>
<p>返回子字符串在字符串中所有出现的位置。</p>
<b>函数签名:</b>
```go
func FindAllOccurrences(str, substr string) []int
```
<b>示例:<span style="float:right;display:inline-block;">[运行](todo)</span></b>
```go
import (
"fmt"
"github.com/duke-git/lancet/v2/strutil"
)
func main() {
result := strutil.FindAllOccurrences("ababab", "ab")
fmt.Println(result)
// Output:
// [0 2 4]
}
```

View File

@@ -68,7 +68,8 @@ import (
- [Rotate](#Rotate)
- [TemplateReplace](#TemplateReplace)
- [RegexMatchAllGroups](#RegexMatchAllGroups)
- [ExtractContent](#RegexMatchAllGroups)
- [ExtractContent](#ExtractContent)
- [FindAllOccurrences](#FindAllOccurrences)
<div STYLE="page-break-after: always;"></div>
@@ -1762,3 +1763,31 @@ func main() {
// [content1 content2 content1]
}
```
### <span id="FindAllOccurrences">FindAllOccurrences</span>
<p>Returns the positions of all occurrences of a substring in a string.</p>
<b>Signature:</b>
```go
func FindAllOccurrences(str, substr string) []int
```
<b>Example:<span style="float:right;display:inline-block;">[Run](todo)</span></b>
```go
import (
"fmt"
"github.com/duke-git/lancet/v2/strutil"
)
func main() {
result := strutil.FindAllOccurrences("ababab", "ab")
fmt.Println(result)
// Output:
// [0 2 4]
}
```

View File

@@ -738,14 +738,14 @@ func RegexMatchAllGroups(pattern, str string) [][]string {
// ExtractContent extracts the content between the start and end strings in the source string.
// Play: https://go.dev/play/p/Ay9UIk7Rum9
func ExtractContent(s, start, end string) []string {
func ExtractContent(str, start, end string) []string {
result := []string{}
for {
if _, after, ok := strings.Cut(s, start); ok {
if _, after, ok := strings.Cut(str, start); ok {
if before, _, ok := strings.Cut(after, end); ok {
result = append(result, before)
s = after
str = after
} else {
break
}
@@ -756,3 +756,20 @@ func ExtractContent(s, start, end string) []string {
return result
}
// FindAllOccurrences returns the positions of all occurrences of a substring in a string.
// Play: todo
func FindAllOccurrences(str, substr string) []int {
var positions []int
for i := 0; i < len(str); {
index := strings.Index(str[i:], substr)
if index == -1 {
break
}
positions = append(positions, i+index)
i += index + 1
}
return positions
}

View File

@@ -763,5 +763,13 @@ func ExampleExtractContent() {
// Output:
// [content1 content2 content1]
}
func ExampleFindAllOccurrences() {
result := FindAllOccurrences("ababab", "ab")
fmt.Println(result)
// Output:
// [0 2 4]
}

View File

@@ -937,3 +937,32 @@ func TestExtractContent(t *testing.T) {
})
}
}
func TestFindAllOccurrences(t *testing.T) {
t.Parallel()
assert := internal.NewAssert(t, "TestFindAllOccurrences")
var empty []int
tests := []struct {
input string
substr string
expected []int
}{
{"", "", empty},
{"", "a", empty},
{"a", "", []int{0}},
{"a", "a", []int{0}},
{"aa", "a", []int{0, 1}},
{"ababab", "ab", []int{0, 2, 4}},
{"ababab", "ba", []int{1, 3}},
{"ababab", "c", empty},
{"ababab", "ababab", []int{0}},
{"ababab", "abababc", empty},
{"ababab", "abababa", empty},
}
for _, tt := range tests {
assert.Equal(tt.expected, FindAllOccurrences(tt.input, tt.substr))
}
}