Skip to content

Commit d9e35fc

Browse files
committed
fix: switch 2 parameters from uint to int for consistency
1 parent 43cef1f commit d9e35fc

File tree

6 files changed

+30
-14
lines changed

6 files changed

+30
-14
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1077,7 +1077,7 @@ sub := lo.Subset(in, 2, 3)
10771077
sub := lo.Subset(in, -4, 3)
10781078
// []int{1, 2, 3}
10791079

1080-
sub := lo.Subset(in, -2, math.MaxUint)
1080+
sub := lo.Subset(in, -2, math.MaxInt)
10811081
// []int{3, 4}
10821082
```
10831083

@@ -1881,7 +1881,7 @@ sub := lo.Substring("hello", 2, 3)
18811881
sub := lo.Substring("hello", -4, 3)
18821882
// "ell"
18831883

1884-
sub := lo.Substring("hello", -2, math.MaxUint)
1884+
sub := lo.Substring("hello", -2, math.MaxInt)
18851885
// "lo"
18861886
```
18871887

slice.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -632,7 +632,11 @@ func CountValuesBy[T any, U comparable](collection []T, mapper func(item T) U) m
632632

633633
// Subset returns a copy of a slice from `offset` up to `length` elements. Like `slice[start:start+length]`, but does not panic on overflow.
634634
// Play: https://go.dev/play/p/tOQu1GhFcog
635-
func Subset[T any, Slice ~[]T](collection Slice, offset int, length uint) Slice {
635+
func Subset[T any, Slice ~[]T](collection Slice, offset int, length int) Slice {
636+
if length < 0 {
637+
panic("lo.Subset: length must not be negative")
638+
}
639+
636640
size := len(collection)
637641

638642
if offset < 0 {
@@ -646,11 +650,11 @@ func Subset[T any, Slice ~[]T](collection Slice, offset int, length uint) Slice
646650
return Slice{}
647651
}
648652

649-
if length > uint(size)-uint(offset) {
650-
length = uint(size - offset)
653+
if length > size-offset {
654+
length = size - offset
651655
}
652656

653-
return collection[offset : offset+int(length)]
657+
return collection[offset : offset+length]
654658
}
655659

656660
// Slice returns a copy of a slice from `start` up to, but not including `end`. Like `slice[start:end]`, but does not panic on overflow.

slice_test.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -893,7 +893,7 @@ func TestSubset(t *testing.T) {
893893
out9 := Subset(in, 2, 4)
894894
out10 := Subset(in, -2, 4)
895895
out11 := Subset(in, -4, 1)
896-
out12 := Subset(in, -4, math.MaxUint)
896+
out12 := Subset(in, -4, math.MaxInt)
897897

898898
is.Empty(out1)
899899
is.Empty(out2)
@@ -908,6 +908,10 @@ func TestSubset(t *testing.T) {
908908
is.Equal([]int{1}, out11)
909909
is.Equal([]int{1, 2, 3, 4}, out12)
910910

911+
is.PanicsWithValue("lo.Subset: length must not be negative", func() {
912+
Subset(in, 0, -1)
913+
})
914+
911915
type myStrings []string
912916
allStrings := myStrings{"", "foo", "bar"}
913917
nonempty := Subset(allStrings, 0, 2)

string.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,13 @@ func nearestPowerOfTwo(capacity int) int {
100100
return n + 1
101101
}
102102

103-
// Substring return part of a string.
103+
// Substring returns part of a string.
104104
// Play: https://go.dev/play/p/TQlxQi82Lu1
105-
func Substring[T ~string](str T, offset int, length uint) T {
105+
func Substring[T ~string](str T, offset int, length int) T {
106+
if length < 0 {
107+
panic("lo.Substring: length must not be negative")
108+
}
109+
106110
rs := []rune(str)
107111
size := len(rs)
108112

@@ -117,11 +121,11 @@ func Substring[T ~string](str T, offset int, length uint) T {
117121
return Empty[T]()
118122
}
119123

120-
if length > uint(size)-uint(offset) {
121-
length = uint(size - offset)
124+
if length > size-offset {
125+
length = size - offset
122126
}
123127

124-
return T(strings.ReplaceAll(string(rs[offset:offset+int(length)]), "\x00", ""))
128+
return T(strings.ReplaceAll(string(rs[offset:offset+length]), "\x00", ""))
125129
}
126130

127131
// ChunkString returns a slice of strings split into groups of length size. If the string can't be split evenly,

string_example_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
func ExampleSubstring() {
99
result1 := Substring("hello", 2, 3)
1010
result2 := Substring("hello", -4, 3)
11-
result3 := Substring("hello", -2, math.MaxUint)
11+
result3 := Substring("hello", -2, math.MaxInt)
1212
result4 := Substring("🏠🐶🐱", 0, 2)
1313
result5 := Substring("你好,世界", 0, 3)
1414

string_test.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ func TestSubstring(t *testing.T) {
7373
str9 := Substring("hello", 2, 4)
7474
str10 := Substring("hello", -2, 4)
7575
str11 := Substring("hello", -4, 1)
76-
str12 := Substring("hello", -4, math.MaxUint)
76+
str12 := Substring("hello", -4, math.MaxInt)
7777
str13 := Substring("🏠🐶🐱", 0, 2)
7878
str14 := Substring("你好,世界", 0, 3)
7979
str15 := Substring("hello", 5, 1)
@@ -93,6 +93,10 @@ func TestSubstring(t *testing.T) {
9393
is.Equal("🏠🐶", str13)
9494
is.Equal("你好,", str14)
9595
is.Empty(str15)
96+
97+
is.PanicsWithValue("lo.Substring: length must not be negative", func() {
98+
Substring("hello", 0, -1)
99+
})
96100
}
97101

98102
func TestRuneLength(t *testing.T) {

0 commit comments

Comments
 (0)