Added function to replace an element in a slice given its value
This commit is contained in:
11
misc.go
11
misc.go
@@ -14,6 +14,7 @@ var ANY_CHAR rune = 0xF0003 // Represents any character - used for states whe
|
||||
var LPAREN_CHAR rune = 0xF0004 // Parentheses in regex are concatenated with this - it acts as a pseudio-parentheses
|
||||
var RPAREN_CHAR rune = 0xF0005
|
||||
var NONCAPLPAREN_CHAR rune = 0xF0006 // Represents a non-capturing group's LPAREN
|
||||
var ESC_BACKSLASH rune = 0xF0007 // Represents an escaped backslash
|
||||
|
||||
// Returns true if str[idx] and str[idx-1] are separated by a word boundary.
|
||||
func isWordBoundary(str []rune, idx int) bool {
|
||||
@@ -139,3 +140,13 @@ func isHex(c rune) bool {
|
||||
func isOctal(c rune) bool {
|
||||
return slices.Contains([]rune("01234567"), c)
|
||||
}
|
||||
|
||||
// Replace an element in a slice with another, given both values
|
||||
func replaceByValue[T comparable](slc []T, toReplace T, replaceWith T) []T {
|
||||
for i, val := range slc {
|
||||
if val == toReplace {
|
||||
slc[i] = replaceWith
|
||||
}
|
||||
}
|
||||
return slc
|
||||
}
|
||||
|
Reference in New Issue
Block a user