Skip to content

Commit

Permalink
Implement ChunkString (#188)
Browse files Browse the repository at this point in the history
Implement ChunkString
  • Loading branch information
CorentinClabaut committed Jul 29, 2022
1 parent 579b1d5 commit 6126b64
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 1 deletion.
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ Supported math helpers:
Supported helpers for strings:

- [Substring](#substring)
- [ChunkString](#chunkstring)
- [RuneLength](#runelength)

Supported helpers for tuples:
Expand Down Expand Up @@ -978,6 +979,24 @@ sub := lo.Substring("hello", -2, math.MaxUint)
// "lo"
```

### ChunkString

Returns an array of strings split into groups the length of size. If array can't be split evenly, the final chunk will be the remaining elements.

```go
lo.ChunkString("123456", 2)
// []string{"12", "34", "56"}

lo.ChunkString("1234567", 2)
// []string{"12", "34", "56", "7"}

lo.ChunkString("", 2)
// []string{""}

lo.ChunkString("1", 2)
// []string{"1"}
```

### RuneLength

An alias to utf8.RuneCountInString which returns the number of runes in string.
Expand Down
34 changes: 33 additions & 1 deletion string.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package lo

import "unicode/utf8"
import (
"unicode/utf8"
)

// Substring return part of a string.
func Substring[T ~string](str T, offset int, length uint) T {
Expand All @@ -24,6 +26,36 @@ func Substring[T ~string](str T, offset int, length uint) T {
return str[offset : offset+int(length)]
}

// ChunkString returns an array of strings split into groups the length of size. If array can't be split evenly,
// the final chunk will be the remaining elements.
func ChunkString[T ~string](str T, size int) []T {
if size <= 0 {
panic("lo.ChunkString: Size parameter must be greater than 0")
}

if len(str) == 0 {
return []T{""}
}

if size >= len(str) {
return []T{str}
}

var chunks []T = make([]T, 0, ((len(str)-1)/size)+1)
currentLen := 0
currentStart := 0
for i := range str {
if currentLen == size {
chunks = append(chunks, str[currentStart:i])
currentLen = 0
currentStart = i
}
currentLen++
}
chunks = append(chunks, str[currentStart:])
return chunks
}

// RuneLength is an alias to utf8.RuneCountInString which returns the number of runes in string.
func RuneLength(str string) int {
return utf8.RuneCountInString(str)
Expand Down
26 changes: 26 additions & 0 deletions string_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,32 @@ import (
"github.com/stretchr/testify/assert"
)

func TestChunkString(t *testing.T) {
is := assert.New(t)

result1 := ChunkString("12345", 2)
is.Equal([]string{"12", "34", "5"}, result1)

result2 := ChunkString("123456", 2)
is.Equal([]string{"12", "34", "56"}, result2)

result3 := ChunkString("123456", 6)
is.Equal([]string{"123456"}, result3)

result4 := ChunkString("123456", 10)
is.Equal([]string{"123456"}, result4)

result5 := ChunkString("", 2)
is.Equal([]string{""}, result5)

result6 := ChunkString("明1好休2林森", 2)
is.Equal([]string{"明1", "好休", "2林", "森"}, result6)

is.Panics(func() {
ChunkString("12345", 0)
})
}

func TestSubstring(t *testing.T) {
is := assert.New(t)

Expand Down

0 comments on commit 6126b64

Please sign in to comment.