File tree Expand file tree Collapse file tree 1 file changed +22
-0
lines changed Expand file tree Collapse file tree 1 file changed +22
-0
lines changed Original file line number Diff line number Diff line change @@ -32,6 +32,7 @@ func ConstantTimeEqual(a, b string) bool {
32
32
return subtle .ConstantTimeCompare (UnsafeBytes (a ), UnsafeBytes (b )) == 1
33
33
}
34
34
35
+ // LongestSequenceOf returns the length of the longest contiguous sequence of a single rune in a string.
35
36
func LongestSequenceOf (a string , b rune ) int {
36
37
// IndexRune has some optimizations, so use it to find the starting point
37
38
firstIndex := strings .IndexRune (a , b )
@@ -52,3 +53,24 @@ func LongestSequenceOf(a string, b rune) int {
52
53
}
53
54
return maxCount
54
55
}
56
+
57
+ // LongestSequenceOfFunc returns the length of the longest contiguous sequence of runes in a string.
58
+ //
59
+ // If the provided function returns zero or higher, the return value is added to the current count.
60
+ // If the return value is negative, the count is reset to zero.
61
+ func LongestSequenceOfFunc (a string , fn func (b rune ) int ) int {
62
+ count := 0
63
+ maxCount := 0
64
+ for _ , r := range a {
65
+ val := fn (r )
66
+ if val < 0 {
67
+ count = 0
68
+ } else {
69
+ count += val
70
+ if count > maxCount {
71
+ maxCount = count
72
+ }
73
+ }
74
+ }
75
+ return maxCount
76
+ }
You can’t perform that action at this time.
0 commit comments