Commit 9146799c authored by Kevin Modzelewski's avatar Kevin Modzelewski

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

Conflicts:
	src/runtime/str.cpp
	test/tests/str_manipulation.py
parents 74e47fe5 d40fd2e9
......@@ -377,14 +377,6 @@ Box* _strSlice(BoxedString* self, i64 start, i64 stop, i64 step) {
return boxString(std::string(chars.begin(), chars.end()));
}
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);
......@@ -517,6 +509,29 @@ Box* strTitle(BoxedString* self) {
return boxString(s);
}
Box* strLower(BoxedString* self) {
assert(self->cls == str_cls);
return boxString(llvm::StringRef(self->s).lower());
}
Box* strUpper(BoxedString* self) {
assert(self->cls == str_cls);
return boxString(llvm::StringRef(self->s).upper());
}
Box* strSwapcase(BoxedString* self) {
std::string s(self->s);
for (auto& i : s) {
if (std::islower(i))
i = std::toupper(i);
else if (std::isupper(i))
i = std::tolower(i);
}
return boxString(s);
}
Box* strContains(BoxedString* self, Box* elt) {
assert(self->cls == str_cls);
if (elt->cls != str_cls)
......@@ -650,6 +665,8 @@ void setupStr() {
str_cls->giveAttr("__nonzero__", new BoxedFunction(boxRTFunction((void*)strNonzero, BOXED_BOOL, 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)));
str_cls->giveAttr("strip", new BoxedFunction(boxRTFunction((void*)strStrip, STR, 2, 1, false, false), { None }));
......
# Testing capitalize/title methods.
def test(s):
print 'string:', repr(s), 'capitalize:', s.capitalize(), 'title:', s.title()
print 'string:', repr(s), repr(s.lower()), repr(s.upper()), repr(s.swapcase()), repr(s.capitalize()), repr(s.title())
test(' hello ')
test('Hello ')
......@@ -12,6 +11,12 @@ test('AaAa')
test('fOrMaT thIs aS title String')
test('fOrMaT,thIs-aS*title;String')
test('getInt')
test('PoZdRaWiAm AgATE')
test('KrZySIU jem zUPe')
test('PchnAc W te LOdZ JeZa lUb OsM SkRzyN Fig\n')
test('HeLLo cOmpUteRs')
test('hEllO CoMPuTErS')
var = 'hello'
try:
......@@ -32,3 +37,24 @@ for i in xrange(256):
s = "a%sb" % c
if s.title()[2] == 'b':
print repr(c)
try:
var = 'hello'
var.lower(42)
print 'TypeError not raised'
except TypeError:
print 'TypeError raised'
try:
var = 'hello'
var.upper(42)
print 'TypeError not raised'
except TypeError:
print 'TypeError raised'
try:
var = 'hello'
var.swapcase(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