Skip to content

Commit

Permalink
feat: adding runelength
Browse files Browse the repository at this point in the history
  • Loading branch information
samber committed Apr 30, 2022
1 parent d22a2a2 commit 94d54a8
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 68 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@
Adding:

- lo.RepeatBy
- lo.Substring
- lo.Subset
- lo.Replace
- lo.ReplaceAll
- lo.Substring
- lo.RuneLength

## 1.18.0 (2022-04-28)

Expand Down
47 changes: 32 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,11 @@ Supported math helpers:
- Range / RangeFrom / RangeWithSteps
- Clamp

Supported helpers for strings:

- Substring
- RuneLength

Supported helpers for tuples:

- T2 -> T9
Expand Down Expand Up @@ -578,21 +583,6 @@ count := lo.CountBy[int]([]int{1, 5, 1}, func(i int) bool {
// 2
```

### Substring

Return part of a string.

```go
sub := lo.Substring("hello", 2, 3)
// "llo"

sub := lo.Substring("hello", -4, 3)
// "ell"

sub := lo.Substring("hello", -2, math.MaxUint)
// "lo"
```

### Subset

Return part of a slice.
Expand Down Expand Up @@ -849,6 +839,33 @@ r3 := lo.Clamp(42, -10, 10)
// 10
```

### Substring

Return part of a string.

```go
sub := lo.Substring("hello", 2, 3)
// "llo"

sub := lo.Substring("hello", -4, 3)
// "ell"

sub := lo.Substring("hello", -2, math.MaxUint)
// "lo"
```

### RuneLength

An alias to utf8.RuneCountInString which returns the number of runes in string.

```go
sub := lo.RuneLength("hell么")
// 5

sub := len("hell么")
// 6
```

### T2 -> T9

Creates a tuple from a list of values.
Expand Down
22 changes: 0 additions & 22 deletions slice.go
Original file line number Diff line number Diff line change
Expand Up @@ -363,28 +363,6 @@ func CountBy[T any](collection []T, predicate func(T) bool) (count int) {
return count
}

// Substring return part of a string.
func Substring[T ~string](str T, offset int, length uint) T {
size := len(str)

if offset < 0 {
offset = size + offset
if offset < 0 {
offset = 0
}
}

if offset > size {
return Empty[T]()
}

if length > uint(size)-uint(offset) {
length = uint(size - offset)
}

return str[offset : offset+int(length)]
}

// Subset return part of a slice.
func Subset[T any](collection []T, offset int, length uint) []T {
size := len(collection)
Expand Down
30 changes: 0 additions & 30 deletions slice_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -384,36 +384,6 @@ func TestCountBy(t *testing.T) {
is.Equal(count3, 0)
}

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

str1 := Substring("hello", 0, 0)
str2 := Substring("hello", 10, 2)
str3 := Substring("hello", -10, 2)
str4 := Substring("hello", 0, 10)
str5 := Substring("hello", 0, 2)
str6 := Substring("hello", 2, 2)
str7 := Substring("hello", 2, 5)
str8 := Substring("hello", 2, 3)
str9 := Substring("hello", 2, 4)
str10 := Substring("hello", -2, 4)
str11 := Substring("hello", -4, 1)
str12 := Substring("hello", -4, math.MaxUint)

is.Equal("", str1)
is.Equal("", str2)
is.Equal("he", str3)
is.Equal("hello", str4)
is.Equal("he", str5)
is.Equal("ll", str6)
is.Equal("llo", str7)
is.Equal("llo", str8)
is.Equal("llo", str9)
is.Equal("lo", str10)
is.Equal("e", str11)
is.Equal("ello", str12)
}

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

Expand Down
30 changes: 30 additions & 0 deletions string.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package lo

import "unicode/utf8"

// Substring return part of a string.
func Substring[T ~string](str T, offset int, length uint) T {
size := len(str)

if offset < 0 {
offset = size + offset
if offset < 0 {
offset = 0
}
}

if offset > size {
return Empty[T]()
}

if length > uint(size)-uint(offset) {
length = uint(size - offset)
}

return str[offset : offset+int(length)]
}

// RuneLength is an alias to utf8.RuneCountInString which returns the number of runes in string.
func RuneLength(str string) int {
return utf8.RuneCountInString(str)
}
45 changes: 45 additions & 0 deletions string_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package lo

import (
"math"
"testing"

"github.com/stretchr/testify/assert"
)

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

str1 := Substring("hello", 0, 0)
str2 := Substring("hello", 10, 2)
str3 := Substring("hello", -10, 2)
str4 := Substring("hello", 0, 10)
str5 := Substring("hello", 0, 2)
str6 := Substring("hello", 2, 2)
str7 := Substring("hello", 2, 5)
str8 := Substring("hello", 2, 3)
str9 := Substring("hello", 2, 4)
str10 := Substring("hello", -2, 4)
str11 := Substring("hello", -4, 1)
str12 := Substring("hello", -4, math.MaxUint)

is.Equal("", str1)
is.Equal("", str2)
is.Equal("he", str3)
is.Equal("hello", str4)
is.Equal("he", str5)
is.Equal("ll", str6)
is.Equal("llo", str7)
is.Equal("llo", str8)
is.Equal("llo", str9)
is.Equal("lo", str10)
is.Equal("e", str11)
is.Equal("ello", str12)
}

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

is.Equal(5, RuneLength("hell么"))
is.Equal(6, len("hell么"))
}

0 comments on commit 94d54a8

Please sign in to comment.