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

[Runtime] Implement str.isalpha(), str.isdigit(), str.isalnum() (#239)

parent c7f97e72
......@@ -335,6 +335,54 @@ func strHash(f *Frame, o *Object) (*Object, *BaseException) {
return h.ToObject(), nil
}
func strIsAlNum(f *Frame, args Args, kwargs KWArgs) (*Object, *BaseException) {
if raised := checkMethodArgs(f, "isalnum", args, StrType); raised != nil {
return nil, raised
}
s := toStrUnsafe(args[0]).Value()
if len(s) == 0 {
return False.ToObject(), nil
}
for i := range s {
if !isAlNum(s[i]) {
return False.ToObject(), nil
}
}
return True.ToObject(), nil
}
func strIsAlpha(f *Frame, args Args, kwargs KWArgs) (*Object, *BaseException) {
if raised := checkMethodArgs(f, "isalpha", args, StrType); raised != nil {
return nil, raised
}
s := toStrUnsafe(args[0]).Value()
if len(s) == 0 {
return False.ToObject(), nil
}
for i := range s {
if !isAlpha(s[i]) {
return False.ToObject(), nil
}
}
return True.ToObject(), nil
}
func strIsDigit(f *Frame, args Args, kwargs KWArgs) (*Object, *BaseException) {
if raised := checkMethodArgs(f, "isdigit", args, StrType); raised != nil {
return nil, raised
}
s := toStrUnsafe(args[0]).Value()
if len(s) == 0 {
return False.ToObject(), nil
}
for i := range s {
if !isDigit(s[i]) {
return False.ToObject(), nil
}
}
return True.ToObject(), nil
}
func strJoin(f *Frame, args Args, _ KWArgs) (*Object, *BaseException) {
if raised := checkMethodArgs(f, "join", args, StrType, ObjectType); raised != nil {
return nil, raised
......@@ -777,6 +825,9 @@ func initStrType(dict map[string]*Object) {
dict["decode"] = newBuiltinFunction("decode", strDecode).ToObject()
dict["endswith"] = newBuiltinFunction("endswith", strEndsWith).ToObject()
dict["find"] = newBuiltinFunction("find", strFind).ToObject()
dict["isalnum"] = newBuiltinFunction("isalnum", strIsAlNum).ToObject()
dict["isalpha"] = newBuiltinFunction("isalpha", strIsAlpha).ToObject()
dict["isdigit"] = newBuiltinFunction("isdigit", strIsDigit).ToObject()
dict["join"] = newBuiltinFunction("join", strJoin).ToObject()
dict["lower"] = newBuiltinFunction("lower", strLower).ToObject()
dict["lstrip"] = newBuiltinFunction("lstrip", strLStrip).ToObject()
......@@ -1136,6 +1187,18 @@ func toUpper(b byte) byte {
return b
}
func isAlNum(c byte) bool {
return isAlpha(c) || isDigit(c)
}
func isAlpha(c byte) bool {
return 'A' <= c && c <= 'Z' || 'a' <= c && c <= 'z'
}
func isDigit(c byte) bool {
return '0' <= c && c <= '9'
}
// strLeftPad returns s padded with fillchar so that its length is at least width.
// Fillchar must be a single character. When fillchar is "0", s starting with a
// sign are handled correctly.
......
......@@ -356,6 +356,21 @@ func TestStrMethods(t *testing.T) {
{"find", wrapArgs("bar", "a", 0, -1), NewInt(1).ToObject(), nil},
{"find", wrapArgs("foo", newTestTuple("barfoo", "oo").ToObject()), nil, mustCreateException(TypeErrorType, "'find/index' requires a 'str' object but received a 'tuple'")},
{"find", wrapArgs("foo", 123), nil, mustCreateException(TypeErrorType, "'find/index' requires a 'str' object but received a 'int'")},
{"isalnum", wrapArgs("123abc"), True.ToObject(), nil},
{"isalnum", wrapArgs(""), False.ToObject(), nil},
{"isalnum", wrapArgs("#$%"), False.ToObject(), nil},
{"isalnum", wrapArgs("abc#123"), False.ToObject(), nil},
{"isalnum", wrapArgs("123abc", "efg"), nil, mustCreateException(TypeErrorType, "'isalnum' of 'str' requires 1 arguments")},
{"isalpha", wrapArgs("xyz"), True.ToObject(), nil},
{"isalpha", wrapArgs(""), False.ToObject(), nil},
{"isalpha", wrapArgs("#$%"), False.ToObject(), nil},
{"isalpha", wrapArgs("abc#123"), False.ToObject(), nil},
{"isalpha", wrapArgs("absd", "efg"), nil, mustCreateException(TypeErrorType, "'isalpha' of 'str' requires 1 arguments")},
{"isdigit", wrapArgs("abc"), False.ToObject(), nil},
{"isdigit", wrapArgs("123"), True.ToObject(), nil},
{"isdigit", wrapArgs(""), False.ToObject(), nil},
{"isdigit", wrapArgs("abc#123"), False.ToObject(), nil},
{"isdigit", wrapArgs("123", "456"), nil, mustCreateException(TypeErrorType, "'isdigit' of 'str' requires 1 arguments")},
{"join", wrapArgs(",", newTestList("foo", "bar")), NewStr("foo,bar").ToObject(), nil},
{"join", wrapArgs(":", newTestList("foo", "bar", NewUnicode("baz"))), NewUnicode("foo:bar:baz").ToObject(), nil},
{"join", wrapArgs("nope", NewTuple()), NewStr("").ToObject(), nil},
......
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