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:
@@ -69,6 +69,7 @@ import (
|
|||||||
- [TemplateReplace](#TemplateReplace)
|
- [TemplateReplace](#TemplateReplace)
|
||||||
- [RegexMatchAllGroups](#RegexMatchAllGroups)
|
- [RegexMatchAllGroups](#RegexMatchAllGroups)
|
||||||
- [ExtractContent](#ExtractContent)
|
- [ExtractContent](#ExtractContent)
|
||||||
|
- [FindAllOccurrences](#FindAllOccurrences)
|
||||||
|
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
@@ -1708,7 +1709,7 @@ func main() {
|
|||||||
func RegexMatchAllGroups(pattern, str string) [][]string
|
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
|
```go
|
||||||
import (
|
import (
|
||||||
@@ -1741,7 +1742,7 @@ func main() {
|
|||||||
func ExtractContent(s, start, end string) []string
|
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
|
```go
|
||||||
import (
|
import (
|
||||||
@@ -1760,3 +1761,31 @@ func main() {
|
|||||||
// [content1 content2 content1]
|
// [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]
|
||||||
|
}
|
||||||
|
```
|
||||||
@@ -68,7 +68,8 @@ import (
|
|||||||
- [Rotate](#Rotate)
|
- [Rotate](#Rotate)
|
||||||
- [TemplateReplace](#TemplateReplace)
|
- [TemplateReplace](#TemplateReplace)
|
||||||
- [RegexMatchAllGroups](#RegexMatchAllGroups)
|
- [RegexMatchAllGroups](#RegexMatchAllGroups)
|
||||||
- [ExtractContent](#RegexMatchAllGroups)
|
- [ExtractContent](#ExtractContent)
|
||||||
|
- [FindAllOccurrences](#FindAllOccurrences)
|
||||||
|
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
@@ -1762,3 +1763,31 @@ func main() {
|
|||||||
// [content1 content2 content1]
|
// [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]
|
||||||
|
}
|
||||||
|
```
|
||||||
@@ -738,14 +738,14 @@ func RegexMatchAllGroups(pattern, str string) [][]string {
|
|||||||
|
|
||||||
// ExtractContent extracts the content between the start and end strings in the source string.
|
// ExtractContent extracts the content between the start and end strings in the source string.
|
||||||
// Play: https://go.dev/play/p/Ay9UIk7Rum9
|
// Play: https://go.dev/play/p/Ay9UIk7Rum9
|
||||||
func ExtractContent(s, start, end string) []string {
|
func ExtractContent(str, start, end string) []string {
|
||||||
result := []string{}
|
result := []string{}
|
||||||
|
|
||||||
for {
|
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 {
|
if before, _, ok := strings.Cut(after, end); ok {
|
||||||
result = append(result, before)
|
result = append(result, before)
|
||||||
s = after
|
str = after
|
||||||
} else {
|
} else {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
@@ -756,3 +756,20 @@ func ExtractContent(s, start, end string) []string {
|
|||||||
|
|
||||||
return result
|
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
|
||||||
|
}
|
||||||
|
|||||||
@@ -763,5 +763,13 @@ func ExampleExtractContent() {
|
|||||||
|
|
||||||
// Output:
|
// Output:
|
||||||
// [content1 content2 content1]
|
// [content1 content2 content1]
|
||||||
|
}
|
||||||
|
|
||||||
|
func ExampleFindAllOccurrences() {
|
||||||
|
result := FindAllOccurrences("ababab", "ab")
|
||||||
|
|
||||||
|
fmt.Println(result)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// [0 2 4]
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user