From 4a45d1c95eddbd908562fe01bdaa2b0137d0a2e9 Mon Sep 17 00:00:00 2001 From: Aadhavan Srinivasan Date: Tue, 28 Jan 2025 11:41:51 -0500 Subject: [PATCH] allCases() now takes a boolean parameter that indicates whether we are case-sensitive or not --- misc.go | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/misc.go b/misc.go index ba6191a..fb22c21 100644 --- a/misc.go +++ b/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) +}