Added function to replace an element in a slice given its value

master
Aadhavan Srinivasan 13 hours ago
parent 9dc4fd4595
commit 0fb78abf7f

@ -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 LPAREN_CHAR rune = 0xF0004 // Parentheses in regex are concatenated with this - it acts as a pseudio-parentheses
var RPAREN_CHAR rune = 0xF0005 var RPAREN_CHAR rune = 0xF0005
var NONCAPLPAREN_CHAR rune = 0xF0006 // Represents a non-capturing group's LPAREN 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. // Returns true if str[idx] and str[idx-1] are separated by a word boundary.
func isWordBoundary(str []rune, idx int) bool { func isWordBoundary(str []rune, idx int) bool {
@ -139,3 +140,13 @@ func isHex(c rune) bool {
func isOctal(c rune) bool { func isOctal(c rune) bool {
return slices.Contains([]rune("01234567"), c) 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
}

Loading…
Cancel
Save