Commit 4cfcfafb authored by Kevin Modzelewski's avatar Kevin Modzelewski

Merge branch 'str_iseverything' of https://github.com/kkszysiu/pyston

Merges PR #109
parents 32ccd8a8 c0516913
......@@ -376,6 +376,145 @@ 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);
const std::string& str(self->s);
if (str.empty())
return False;
for (const auto& c : str) {
if (!std::isalpha(c))
return False;
}
return True;
}
Box* strIsDigit(BoxedString* self) {
assert(self->cls == str_cls);
const std::string& str(self->s);
if (str.empty())
return False;
for (const auto& c : str) {
if (!std::isdigit(c))
return False;
}
return True;
}
Box* strIsAlnum(BoxedString* self) {
assert(self->cls == str_cls);
const std::string& str(self->s);
if (str.empty())
return False;
for (const auto& c : str) {
if (!std::isalnum(c))
return False;
}
return True;
}
Box* strIsLower(BoxedString* self) {
assert(self->cls == str_cls);
const std::string& str(self->s);
bool lowered = false;
if (str.empty())
return False;
for (const auto& c : str) {
if (std::isspace(c) || std::isdigit(c)) {
continue;
} else if (!std::islower(c)) {
return False;
} else {
lowered = true;
}
}
return boxBool(lowered);
}
Box* strIsUpper(BoxedString* self) {
assert(self->cls == str_cls);
const std::string& str(self->s);
bool uppered = false;
if (str.empty())
return False;
for (const auto& c : str) {
if (std::isspace(c) || std::isdigit(c)) {
continue;
} else if (!std::isupper(c)) {
return False;
} else {
uppered = true;
}
}
return boxBool(uppered);
}
Box* strIsSpace(BoxedString* self) {
assert(self->cls == str_cls);
const std::string& str(self->s);
if (str.empty())
return False;
for (const auto& c : str) {
if (!std::isspace(c))
return False;
}
return True;
}
Box* strIsTitle(BoxedString* self) {
assert(self->cls == str_cls);
const std::string& str(self->s);
if (str.empty())
return False;
if (str.size() == 1)
return boxBool(std::isupper(str[0]));
bool cased = false, start_of_word = true;
for (const auto& c : str) {
if (std::isupper(c)) {
if (!start_of_word) {
return False;
}
start_of_word = false;
cased = true;
} else if (std::islower(c)) {
if (start_of_word) {
return False;
}
start_of_word = false;
cased = true;
} else {
start_of_word = true;
}
}
return boxBool(cased);
}
Box* strJoin(BoxedString* self, Box* rhs) {
assert(self->cls == str_cls);
......@@ -660,6 +799,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)));
......
......@@ -32,12 +32,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 +52,94 @@ try:
print 'TypeError not raised'
except TypeError:
print 'TypeError raised'
# 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')
for i in xrange(256):
c = chr(i)
s = "a%sb" % c
if s.title()[2] == 'b':
print repr(c)
test(c)
test_is(c)
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