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

Merge pull request #75 from undingen/strip

Implement str.lstrip and str.rstrip
parents 1ac8b1ac 6c41c33a
...@@ -380,34 +380,55 @@ Box* strSplit2(BoxedString* self, BoxedString* sep) { ...@@ -380,34 +380,55 @@ Box* strSplit2(BoxedString* self, BoxedString* sep) {
} }
} }
Box* strStrip(BoxedString* self) { Box* strStrip1(BoxedString* self) {
assert(self->cls == str_cls); assert(self->cls == str_cls);
return new BoxedString(llvm::StringRef(self->s).trim(" \t\n\r\f\v"));
}
const std::string& s = self->s; Box* strStrip2(BoxedString* self, Box* chars) {
int n = s.size(); assert(self->cls == str_cls);
int strip_beginning = 0; if (chars->cls == str_cls) {
while (strip_beginning < n) { return new BoxedString(llvm::StringRef(self->s).trim(static_cast<BoxedString*>(chars)->s));
char c = s[strip_beginning]; } else if (chars->cls == none_cls) {
if (c == ' ' || c == '\t' || c == '\n' || c == '\r' || c == '\f' || c == '\v') return strStrip1(self);
strip_beginning++; } else {
else raiseExcHelper(TypeError, "strip arg must be None, str or unicode");
break;
} }
}
if (strip_beginning == n) Box* strLStrip1(BoxedString* self) {
return boxStrConstant(""); assert(self->cls == str_cls);
return new BoxedString(llvm::StringRef(self->s).ltrim(" \t\n\r\f\v"));
}
int strip_end = 0; Box* strLStrip2(BoxedString* self, Box* chars) {
while (strip_end < n) { assert(self->cls == str_cls);
char c = s[n - strip_end - 1];
if (c == ' ' || c == '\t' || c == '\n' || c == '\r' || c == '\f' || c == '\v') if (chars->cls == str_cls) {
strip_end++; return new BoxedString(llvm::StringRef(self->s).ltrim(static_cast<BoxedString*>(chars)->s));
else } else if (chars->cls == none_cls) {
break; return strLStrip1(self);
} else {
raiseExcHelper(TypeError, "lstrip arg must be None, str or unicode");
} }
}
Box* strRStrip1(BoxedString* self) {
assert(self->cls == str_cls);
return new BoxedString(llvm::StringRef(self->s).rtrim(" \t\n\r\f\v"));
}
Box* strRStrip2(BoxedString* self, Box* chars) {
assert(self->cls == str_cls);
return new BoxedString(s.substr(strip_beginning, n - strip_beginning - strip_end)); if (chars->cls == str_cls) {
return new BoxedString(llvm::StringRef(self->s).rtrim(static_cast<BoxedString*>(chars)->s));
} else if (chars->cls == none_cls) {
return strRStrip1(self);
} else {
raiseExcHelper(TypeError, "rstrip arg must be None, str or unicode");
}
} }
Box* strContains(BoxedString* self, Box* elt) { Box* strContains(BoxedString* self, Box* elt) {
...@@ -541,7 +562,19 @@ void setupStr() { ...@@ -541,7 +562,19 @@ void setupStr() {
str_cls->giveAttr("__nonzero__", new BoxedFunction(boxRTFunction((void*)strNonzero, NULL, 1, false))); str_cls->giveAttr("__nonzero__", new BoxedFunction(boxRTFunction((void*)strNonzero, NULL, 1, false)));
str_cls->giveAttr("lower", new BoxedFunction(boxRTFunction((void*)strLower, STR, 1, false))); str_cls->giveAttr("lower", new BoxedFunction(boxRTFunction((void*)strLower, STR, 1, false)));
str_cls->giveAttr("strip", new BoxedFunction(boxRTFunction((void*)strStrip, STR, 1, false)));
CLFunction* strStrip = boxRTFunction((void*)strStrip1, STR, 1, false);
addRTFunction(strStrip, (void*)strStrip2, STR, 2, false);
str_cls->giveAttr("strip", new BoxedFunction(strStrip));
CLFunction* strLStrip = boxRTFunction((void*)strLStrip1, STR, 1, false);
addRTFunction(strLStrip, (void*)strLStrip2, STR, 2, false);
str_cls->giveAttr("lstrip", new BoxedFunction(strLStrip));
CLFunction* strRStrip = boxRTFunction((void*)strRStrip1, STR, 1, false);
addRTFunction(strRStrip, (void*)strRStrip2, STR, 2, false);
str_cls->giveAttr("rstrip", new BoxedFunction(strRStrip));
str_cls->giveAttr("__contains__", new BoxedFunction(boxRTFunction((void*)strContains, BOXED_BOOL, 2, false))); str_cls->giveAttr("__contains__", new BoxedFunction(boxRTFunction((void*)strContains, BOXED_BOOL, 2, false)));
str_cls->giveAttr("__add__", new BoxedFunction(boxRTFunction((void*)strAdd, NULL, 2, false))); str_cls->giveAttr("__add__", new BoxedFunction(boxRTFunction((void*)strAdd, NULL, 2, false)));
......
...@@ -20,7 +20,11 @@ print map(bool, ["hello", "", "world"]) ...@@ -20,7 +20,11 @@ print map(bool, ["hello", "", "world"])
if "": if "":
print "bad" print "bad"
print repr(" \t\n\v\ftest \t\n\v\f".strip()) testStr = " \t\n\v\ftest \t\n\v\f"
print repr(testStr.strip()), repr(testStr.lstrip()), repr(testStr.rstrip())
for c in [None, " ", "\t\f", "test"]:
print repr(testStr.strip(c)), repr(testStr.lstrip(c)), repr(testStr.rstrip(c))
for pattern in ["hello", "o w", "nope"]: for pattern in ["hello", "o w", "nope"]:
print pattern in "hello world" print pattern in "hello world"
......
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