allCases() now takes a boolean parameter that indicates whether we are case-sensitive or not
This commit is contained in:
29
misc.go
29
misc.go
@@ -2,6 +2,7 @@ package main
|
||||
|
||||
import (
|
||||
"slices"
|
||||
"strings"
|
||||
"unicode"
|
||||
)
|
||||
|
||||
@@ -124,13 +125,21 @@ func genRangeInclusive[T character](start, end T) []T {
|
||||
return toRet
|
||||
}
|
||||
|
||||
// Returns a rune-slice containing all possible cases of the given rune.
|
||||
// Returns a rune-slice containing all possible cases of the given rune, given the
|
||||
// 'caseInsensitive' boolean variable.
|
||||
// If this variable is false, the rune is returned as-is, without modifications.
|
||||
// If it is true, then we return all possible cases of the
|
||||
// rune.
|
||||
// At the moment, this includes:
|
||||
// 1. Upper case
|
||||
// 2. Lower case
|
||||
// 3. Title case
|
||||
func allCases(r rune) []rune {
|
||||
return []rune{unicode.ToLower(r), unicode.ToUpper(r), unicode.ToTitle(r)}
|
||||
func allCases(r rune, caseInsensitive bool) []rune {
|
||||
if caseInsensitive {
|
||||
return []rune{unicode.ToLower(r), unicode.ToUpper(r), unicode.ToTitle(r)}
|
||||
} else {
|
||||
return []rune{r}
|
||||
}
|
||||
}
|
||||
|
||||
func isHex(c rune) bool {
|
||||
@@ -150,3 +159,17 @@ func replaceByValue[T comparable](slc []T, toReplace T, replaceWith T) []T {
|
||||
}
|
||||
return slc
|
||||
}
|
||||
|
||||
// swapCase swaps the case of every character in the given string, and returns
|
||||
// the new string.
|
||||
func swapCase(str string) string {
|
||||
return strings.Map(func(r rune) rune {
|
||||
switch {
|
||||
case unicode.IsLower(r):
|
||||
return unicode.ToUpper(r)
|
||||
case unicode.IsUpper(r):
|
||||
return unicode.ToLower(r)
|
||||
}
|
||||
return r
|
||||
}, str)
|
||||
}
|
||||
|
Reference in New Issue
Block a user