Skip to content

Commit 48ffe63

Browse files
committed
refs #12. swap case.
1 parent d66e903 commit 48ffe63

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

convert.go

+27
Original file line numberDiff line numberDiff line change
@@ -157,3 +157,30 @@ func ToSnakeCase(str string) string {
157157

158158
return buf.String()
159159
}
160+
161+
// SwapCase will swap characters case from upper to lower or lower to upper.
162+
func SwapCase(str string) string {
163+
var r rune
164+
var size int
165+
166+
buf := &bytes.Buffer{}
167+
168+
for len(str) > 0 {
169+
r, size = utf8.DecodeRuneInString(str)
170+
171+
switch {
172+
case unicode.IsUpper(r):
173+
buf.WriteRune(unicode.ToLower(r))
174+
175+
case unicode.IsLower(r):
176+
buf.WriteRune(unicode.ToUpper(r))
177+
178+
default:
179+
buf.WriteRune(r)
180+
}
181+
182+
str = str[size:]
183+
}
184+
185+
return buf.String()
186+
}

convert_test.go

+7
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,10 @@ func TestToCamelCase(t *testing.T) {
3535
"all": "All",
3636
})
3737
}
38+
39+
func TestSwapCase(t *testing.T) {
40+
runTestCases(t, SwapCase, map[string]string{
41+
"swapCase": "SWAPcASE",
42+
"Θ~λa云Ξπ": "θ~ΛA云ξΠ",
43+
})
44+
}

0 commit comments

Comments
 (0)