Commit c0516913 authored by Krzysztof Klinikowski's avatar Krzysztof Klinikowski

Code cleanup, fixes after CR

parent 488c1139
......@@ -381,13 +381,13 @@ 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]))
for (const auto& i : str) {
if (!std::isalpha(i))
return False;
}
return True;
}
......@@ -396,13 +396,13 @@ 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]))
for (const auto& i : str) {
if (!std::isdigit(i))
return False;
}
return True;
}
......@@ -411,13 +411,13 @@ 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]))
for (const auto& i : str) {
if (!std::isalnum(i))
return False;
}
return True;
}
......@@ -427,28 +427,44 @@ Box* strIsLower(BoxedString* self) {
assert(self->cls == str_cls);
std::string str(self->s);
std::string::size_type i;
bool lowered = false;
if (str.empty())
return False;
for (const auto& i : str)
if (!std::islower(i))
for (const auto& i : str) {
if (std::isspace(i) || std::isdigit(i)) {
continue;
} else if (!std::islower(i)) {
return False;
} else {
lowered = true;
}
}
return True;
return boxBool(lowered);
}
Box* strIsUpper(BoxedString* self) {
assert(self->cls == str_cls);
std::string str(self->s);
std::string::size_type i;
bool uppered = false;
if (str.empty())
return False;
for (const auto& i : str)
if (!std::isupper(str[i]))
for (const auto& i : str) {
if (std::isspace(i) || std::isdigit(i)) {
continue;
} else if (!std::isupper(i)) {
return False;
} else {
uppered = true;
}
}
return boxBool(uppered);
return True;
}
......@@ -457,13 +473,13 @@ 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]))
for (const auto& i : str) {
if (!std::isspace(i))
return False;
}
return True;
}
......@@ -472,47 +488,38 @@ 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;
bool cased = false, start_of_word = false;
for (const auto& i : str) {
if (std::isupper(str[i])) {
if (previous_is_cased) {
if (std::isupper(i)) {
if (start_of_word) {
return False;
}
previous_is_cased = true;
start_of_word = true;
cased = true;
} else if (std::islower(str[i])) {
if (!previous_is_cased) {
} else if (std::islower(i)) {
if (!start_of_word) {
return False;
}
previous_is_cased = true;
start_of_word = true;
cased = true;
} else {
previous_is_cased = false;
start_of_word = 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);
......
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
# Testing capitalize/title methods.
def test(s):
......@@ -32,12 +33,6 @@ try:
except TypeError:
print 'TypeError raised'
for i in xrange(256):
c = chr(i)
s = "a%sb" % c
if s.title()[2] == 'b':
print repr(c)
try:
var = 'hello'
var.lower(42)
......@@ -58,3 +53,92 @@ try:
print 'TypeError not raised'
except TypeError:
print 'TypeError raised'
for i in xrange(256):
c = chr(i)
s = "a%sb" % c
if s.title()[2] == 'b':
print repr(c)
# Testing isalnum, isalpha, isdigit, islower, isspace, istitle and isupper methods
def test_is(s):
print 'string:', repr(s), 'isalnum:', s.isalnum(), 'isalpha:', s.isalpha(), 'isdigit:', s.isdigit(), 'islower:', s.islower(), 'isspace:', s.isspace(), 'istitle:', s.istitle(), 'isupper:', s.isupper()
test_is('')
test_is('a')
test_is('A')
test_is('123abc456')
test_is('a1b3c')
test_is('aBc000 ')
test_is('abc\n')
test_is('aBc123')
test_is('abc')
test_is('0')
test_is('0123456789')
test_is('0123456789a')
test_is(' ')
test_is('\t')
test_is('\r')
test_is('\n')
test_is(' \t\r\n')
test_is(' \t\r\na')
test_is('A Titlecased Line')
test_is('A\nTitlecased Line')
test_is('A Titlecased, Line')
test_is('Not a capitalized String')
test_is('Not\ta Titlecase String')
test_is('Not--a Titlecase String')
test_is('NOT')
try:
var = 'abc'
var.isalnum(42)
print 'TypeError not raised'
except TypeError:
print 'TypeError raised'
try:
var = 'abc'
var.isalpha(42)
print 'TypeError not raised'
except TypeError:
print 'TypeError raised'
try:
var = 'abc'
var.isdigit(42)
print 'TypeError not raised'
except TypeError:
print 'TypeError raised'
try:
var = 'abc'
var.islower(42)
print 'TypeError not raised'
except TypeError:
print 'TypeError raised'
try:
var = 'abc'
var.isspace(42)
print 'TypeError not raised'
except TypeError:
print 'TypeError raised'
try:
var = 'abc'
var.istitle(42)
print 'TypeError not raised'
except TypeError:
print 'TypeError raised'
try:
var = 'abc'
var.isupper(42)
print 'TypeError not raised'
except TypeError:
print 'TypeError raised'
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