Commit 488c1139 authored by Krzysztof Klinikowski's avatar Krzysztof Klinikowski

Implementation of isalnum, isalpha, isdigit, islower, isspace, istitle and isupper

parent e891c285
......@@ -377,6 +377,142 @@ Box* _strSlice(BoxedString* self, i64 start, i64 stop, i64 step) {
return boxString(std::string(chars.begin(), chars.end()));
}
Box* strIsAlpha(BoxedString* self) {
assert(self->cls == str_cls);
std::string str(self->s);
std::string::size_type i;
if (str.empty())
return False;
for (const auto& i : str)
if (!std::isalpha(str[i]))
return False;
return True;
}
Box* strIsDigit(BoxedString* self) {
assert(self->cls == str_cls);
std::string str(self->s);
std::string::size_type i;
if (str.empty())
return False;
for (const auto& i : str)
if (!std::isdigit(str[i]))
return False;
return True;
}
Box* strIsAlnum(BoxedString* self) {
assert(self->cls == str_cls);
std::string str(self->s);
std::string::size_type i;
if (str.empty())
return False;
for (const auto& i : str)
if (!std::isalnum(str[i]))
return False;
return True;
}
Box* strIsLower(BoxedString* self) {
assert(self->cls == str_cls);
std::string str(self->s);
std::string::size_type i;
if (str.empty())
return False;
for (const auto& i : str)
if (!std::islower(i))
return False;
return True;
}
Box* strIsUpper(BoxedString* self) {
assert(self->cls == str_cls);
std::string str(self->s);
std::string::size_type i;
if (str.empty())
return False;
for (const auto& i : str)
if (!std::isupper(str[i]))
return False;
return True;
}
Box* strIsSpace(BoxedString* self) {
assert(self->cls == str_cls);
std::string str(self->s);
std::string::size_type i;
if (str.empty())
return False;
for (const auto& i : str)
if (!std::isspace(str[i]))
return False;
return True;
}
Box* strIsTitle(BoxedString* self) {
assert(self->cls == str_cls);
std::string str(self->s);
std::string::size_type i;
if (str.empty())
return False;
if (str.size() == 1)
return boxBool(std::isupper(str[0]));
bool cased = false, previous_is_cased = false;
for (const auto& i : str) {
if (std::isupper(str[i])) {
if (previous_is_cased) {
return False;
}
previous_is_cased = true;
cased = true;
} else if (std::islower(str[i])) {
if (!previous_is_cased) {
return False;
}
previous_is_cased = true;
cased = true;
} else {
previous_is_cased = false;
}
}
return boxBool(cased);
}
Box* strLower(BoxedString* self) {
assert(self->cls == str_cls);
std::string lowered(self->s);
std::transform(lowered.begin(), lowered.end(), lowered.begin(), tolower);
return boxString(std::move(lowered));
}
Box* strJoin(BoxedString* self, Box* rhs) {
assert(self->cls == str_cls);
......@@ -664,6 +800,14 @@ void setupStr() {
str_cls->giveAttr("__hash__", new BoxedFunction(boxRTFunction((void*)strHash, BOXED_INT, 1)));
str_cls->giveAttr("__nonzero__", new BoxedFunction(boxRTFunction((void*)strNonzero, BOXED_BOOL, 1)));
str_cls->giveAttr("isalnum", new BoxedFunction(boxRTFunction((void*)strIsAlnum, STR, 1)));
str_cls->giveAttr("isalpha", new BoxedFunction(boxRTFunction((void*)strIsAlpha, STR, 1)));
str_cls->giveAttr("isdigit", new BoxedFunction(boxRTFunction((void*)strIsDigit, STR, 1)));
str_cls->giveAttr("islower", new BoxedFunction(boxRTFunction((void*)strIsLower, STR, 1)));
str_cls->giveAttr("isspace", new BoxedFunction(boxRTFunction((void*)strIsSpace, STR, 1)));
str_cls->giveAttr("istitle", new BoxedFunction(boxRTFunction((void*)strIsTitle, STR, 1)));
str_cls->giveAttr("isupper", new BoxedFunction(boxRTFunction((void*)strIsUpper, STR, 1)));
str_cls->giveAttr("lower", new BoxedFunction(boxRTFunction((void*)strLower, STR, 1)));
str_cls->giveAttr("swapcase", new BoxedFunction(boxRTFunction((void*)strSwapcase, STR, 1)));
str_cls->giveAttr("upper", new BoxedFunction(boxRTFunction((void*)strUpper, STR, 1)));
......
var = ''
assert var.isalnum() == False
var = 'a'
assert var.isalnum() == True
var = 'A'
assert var.isalnum() == True
var = '\n'
assert var.isalnum() == False
var = '123abc456'
assert var.isalnum() == True
var = 'a1b3c'
assert var.isalnum() == True
var = 'aBc000 '
assert var.isalnum() == False
var = 'abc\n'
assert var.isalnum() == False
try:
var = 'abc'
var.isalnum(42)
assert False
except TypeError:
assert True
var = ''
assert var.isalpha() == False
var = 'a'
assert var.isalpha() == True
var = 'A'
assert var.isalpha() == True
var = '\n'
assert var.isalpha() == False
var = 'abc'
assert var.isalpha() == True
var = 'aBc123'
assert var.isalpha() == False
var = 'abc\n'
assert var.isalpha() == False
try:
var = 'abc'
var.isalpha(42)
assert False
except TypeError:
assert True
var = ''
assert var.isdigit() == False
var = 'a'
assert var.isdigit() == False
var = '0'
assert var.isdigit() == True
var = '0123456789'
assert var.isdigit() == True
var = '0123456789a'
assert var.isdigit() == False
try:
var = 'abc'
var.isdigit(42)
assert False
except TypeError:
assert True
var = ''
assert var.islower() == False
var = 'a'
assert var.islower() == True
var = 'A'
assert var.islower() == False
var = '\n'
assert var.islower() == False
var = 'abc'
assert var.islower() == True
var = 'aBc'
assert var.islower() == False
# TODO: not supported yet
#var = 'abc\n'
#assert var.islower() == True
try:
var = 'abc'
var.islower(42)
assert False
except TypeError:
assert True
var = ''
assert var.isspace() == False
var = 'a'
assert var.isspace() == False
var = ' '
assert var.isspace() == True
var = '\t'
assert var.isspace() == True
var = '\r'
assert var.isspace() == True
var = '\n'
assert var.isspace() == True
var = ' \t\r\n'
assert var.isspace() == True
var = ' \t\r\na'
assert var.isspace() == False
try:
var = 'abc'
var.isspace(42)
assert False
except TypeError:
assert True
var = ''
assert var.istitle() == False
var = 'a'
assert var.istitle() == False
var = 'A'
assert var.istitle() == True
var = '\n'
assert var.istitle() == False
var = 'A Titlecased Line'
assert var.istitle() == True
var = 'A\nTitlecased Line'
assert var.istitle() == True
var = 'A Titlecased, Line'
assert var.istitle() == True
var = 'Not a capitalized String'
assert var.istitle() == False
var = 'Not\ta Titlecase String'
assert var.istitle() == False
var = 'Not--a Titlecase String'
assert var.istitle() == False
var = 'NOT'
assert var.istitle() == False
try:
var = 'abc'
var.istitle(42)
assert False
except TypeError:
assert True
var = ''
assert var.isupper() == False
var = 'a'
assert var.isupper() == False
var = 'A'
assert var.isupper() == True
var = '\n'
assert var.isupper() == False
var = 'ABC'
assert var.isupper() == True
var = 'AbC'
assert var.isupper() == False
# TODO: not supported yet
#var = 'ABC\n'
#assert var.isupper() == True
try:
var = 'abc'
var.isupper(42)
assert False
except TypeError:
assert True
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