File tree 2 files changed +34
-0
lines changed
2 files changed +34
-0
lines changed Original file line number Diff line number Diff line change @@ -157,3 +157,30 @@ func ToSnakeCase(str string) string {
157
157
158
158
return buf .String ()
159
159
}
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
+ }
Original file line number Diff line number Diff line change @@ -35,3 +35,10 @@ func TestToCamelCase(t *testing.T) {
35
35
"all" : "All" ,
36
36
})
37
37
}
38
+
39
+ func TestSwapCase (t * testing.T ) {
40
+ runTestCases (t , SwapCase , map [string ]string {
41
+ "swapCase" : "SWAPcASE" ,
42
+ "Θ~λa云Ξπ" : "θ~ΛA云ξΠ" ,
43
+ })
44
+ }
You can’t perform that action at this time.
0 commit comments