Commit 637b02f8 authored by Dong-hee Na's avatar Dong-hee Na Committed by Dylan Trotter

The implementation of str.swapcase() operation. (#229)

parent 77e088f2
......@@ -748,6 +748,28 @@ func strStr(_ *Frame, o *Object) (*Object, *BaseException) {
return NewStr(toStrUnsafe(o).Value()).ToObject(), nil
}
func strSwapCase(f *Frame, args Args, kwargs KWArgs) (*Object, *BaseException) {
if raised := checkMethodArgs(f, "swapcase", args, StrType); raised != nil {
return nil, raised
}
s := toStrUnsafe(args[0]).Value()
numBytes := len(s)
if numBytes == 0 {
return args[0], nil
}
b := make([]byte, numBytes)
for i := 0; i < numBytes; i++ {
if s[i] >= 'a' && s[i] <= 'z' {
b[i] = toUpper(s[i])
} else if s[i] >= 'A' && s[i] <= 'Z' {
b[i] = toLower(s[i])
} else {
b[i] = s[i]
}
}
return NewStr(string(b)).ToObject(), nil
}
func initStrType(dict map[string]*Object) {
dict["__getnewargs__"] = newBuiltinFunction("__getnewargs__", strGetNewArgs).ToObject()
dict["capitalize"] = newBuiltinFunction("capitalize", strCapitalize).ToObject()
......@@ -762,6 +784,7 @@ func initStrType(dict map[string]*Object) {
dict["splitlines"] = newBuiltinFunction("splitlines", strSplitLines).ToObject()
dict["startswith"] = newBuiltinFunction("startswith", strStartsWith).ToObject()
dict["strip"] = newBuiltinFunction("strip", strStrip).ToObject()
dict["swapcase"] = newBuiltinFunction("swapcase", strSwapCase).ToObject()
dict["replace"] = newBuiltinFunction("replace", strReplace).ToObject()
dict["rstrip"] = newBuiltinFunction("rstrip", strRStrip).ToObject()
dict["title"] = newBuiltinFunction("title", strTitle).ToObject()
......
......@@ -512,6 +512,17 @@ func TestStrMethods(t *testing.T) {
{"zfill", wrapArgs("", False), NewStr("").ToObject(), nil},
{"zfill", wrapArgs("34", NewStr("test")), nil, mustCreateException(TypeErrorType, "an integer is required")},
{"zfill", wrapArgs("34"), nil, mustCreateException(TypeErrorType, "'zfill' of 'str' requires 2 arguments")},
{"swapcase", wrapArgs(""), NewStr("").ToObject(), nil},
{"swapcase", wrapArgs("a"), NewStr("A").ToObject(), nil},
{"swapcase", wrapArgs("A"), NewStr("a").ToObject(), nil},
{"swapcase", wrapArgs(" A"), NewStr(" a").ToObject(), nil},
{"swapcase", wrapArgs("abc"), NewStr("ABC").ToObject(), nil},
{"swapcase", wrapArgs("ABC"), NewStr("abc").ToObject(), nil},
{"swapcase", wrapArgs("aBC"), NewStr("Abc").ToObject(), nil},
{"swapcase", wrapArgs("abc def", 123), nil, mustCreateException(TypeErrorType, "'swapcase' of 'str' requires 1 arguments")},
{"swapcase", wrapArgs(123), nil, mustCreateException(TypeErrorType, "unbound method swapcase() must be called with str instance as first argument (got int instance instead)")},
{"swapcase", wrapArgs("вол"), NewStr("вол").ToObject(), nil},
{"swapcase", wrapArgs("ВОЛ"), NewStr("ВОЛ").ToObject(), nil},
}
for _, cas := range cases {
testCase := invokeTestCase{args: cas.args, want: cas.want, wantExc: cas.wantExc}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment