Commit 0d2c9b6e authored by Kevin Modzelewski's avatar Kevin Modzelewski

str.partition

parent b655ef4d
......@@ -637,6 +637,21 @@ Box* strReplace(Box* _self, Box* _old, Box* _new, Box** _args) {
return rtn;
}
Box* strPartition(BoxedString* self, BoxedString* sep) {
RELEASE_ASSERT(self->cls == str_cls, "");
RELEASE_ASSERT(sep->cls == str_cls, "");
size_t found_idx = self->s.find(sep->s);
if (found_idx == std::string::npos)
return new BoxedTuple({ self, boxStrConstant(""), boxStrConstant("") });
return new BoxedTuple({ boxStrConstantSize(self->s.c_str(), found_idx),
boxStrConstantSize(self->s.c_str() + found_idx, sep->s.size()),
boxStrConstantSize(self->s.c_str() + found_idx + sep->s.size(),
self->s.size() - found_idx - sep->s.size()) });
}
Box* strSplit(BoxedString* self, BoxedString* sep, BoxedInt* _max_split) {
assert(self->cls == str_cls);
if (_max_split->cls != int_cls)
......@@ -1105,6 +1120,8 @@ void setupStr() {
new BoxedFunction(boxRTFunction((void*)strFind, BOXED_INT, 3, 1, false, false), { boxInt(0) }));
str_cls->giveAttr("rfind", new BoxedFunction(boxRTFunction((void*)strRfind, BOXED_INT, 2)));
str_cls->giveAttr("partition", new BoxedFunction(boxRTFunction((void*)strPartition, UNKNOWN, 2)));
str_cls->giveAttr("__add__", new BoxedFunction(boxRTFunction((void*)strAdd, UNKNOWN, 2)));
str_cls->giveAttr("__mod__", new BoxedFunction(boxRTFunction((void*)strMod, STR, 2)));
str_cls->giveAttr("__mul__", new BoxedFunction(boxRTFunction((void*)strMul, UNKNOWN, 2)));
......
......@@ -72,3 +72,7 @@ print "hello world".translate(translation_map, "")
for i in xrange(-10, 10):
print i, "aaaaa".find("a", i)
print "hello world".partition("hi")
print "hello world".partition("hello")
print "hello world".partition("o")
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