From 0fb78abf7f970dca64615fa6765351808133d49a Mon Sep 17 00:00:00 2001 From: Aadhavan Srinivasan Date: Tue, 21 Jan 2025 22:09:41 -0500 Subject: [PATCH] Added function to replace an element in a slice given its value --- misc.go | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/misc.go b/misc.go index 828bf91..4a83232 100644 --- a/misc.go +++ b/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 +}