Commit 0920ed13 authored by Kevin Modzelewski's avatar Kevin Modzelewski

More unicode support

parent 3be62ae7
......@@ -577,6 +577,9 @@ std::string floatFmt(double x, int precision, char code) {
}
BoxedFloat* _floatNew(Box* a) {
// FIXME CPython uses PyUnicode_EncodeDecimal:
a = coerceUnicodeToStr(a);
if (a->cls == float_cls) {
return static_cast<BoxedFloat*>(a);
} else if (isSubclass(a->cls, float_cls)) {
......
......@@ -1548,6 +1548,11 @@ Box* strReplace(Box* _self, Box* _old, Box* _new, Box** _args) {
getTypeName(_self));
BoxedString* self = static_cast<BoxedString*>(_self);
#ifdef Py_USING_UNICODE
if (PyUnicode_Check(_old) || PyUnicode_Check(_new))
return PyUnicode_Replace((PyObject*)self, _old, _new, -1 /*count*/);
#endif
if (_old->cls != str_cls)
raiseExcHelper(TypeError, "expected a character buffer object");
BoxedString* old = static_cast<BoxedString*>(_old);
......@@ -1796,9 +1801,6 @@ Box* strStartswith(BoxedString* self, Box* elt, Box* start, Box** _args) {
raiseExcHelper(TypeError, "descriptor 'startswith' requires a 'str' object but received a '%s'",
getTypeName(self));
if (elt->cls != str_cls)
raiseExcHelper(TypeError, "expected a character buffer object");
Py_ssize_t istart = 0, iend = PY_SSIZE_T_MAX;
if (start) {
int r = _PyEval_SliceIndex(start, &istart);
......@@ -1812,6 +1814,27 @@ Box* strStartswith(BoxedString* self, Box* elt, Box* start, Box** _args) {
throwCAPIException();
}
if (isSubclass(elt->cls, tuple_cls)) {
for (auto e : static_cast<BoxedTuple*>(elt)->elts) {
auto b = strStartswith(self, e, start, _args);
assert(b->cls == bool_cls);
if (b == True)
return True;
}
return False;
}
if (isSubclass(elt->cls, unicode_cls)) {
int r = PyUnicode_Tailmatch(self, elt, istart, iend, -1);
if (r < 0)
throwCAPIException();
assert(r == 0 || r == 1);
return boxBool(r);
}
if (elt->cls != str_cls)
raiseExcHelper(TypeError, "expected a character buffer object");
BoxedString* sub = static_cast<BoxedString*>(elt);
Py_ssize_t n = self->s.size();
......@@ -1841,9 +1864,6 @@ Box* strEndswith(BoxedString* self, Box* elt, Box* start, Box** _args) {
raiseExcHelper(TypeError, "descriptor 'endswith' requires a 'str' object but received a '%s'",
getTypeName(self));
if (elt->cls != str_cls)
raiseExcHelper(TypeError, "expected a character buffer object");
Py_ssize_t istart = 0, iend = PY_SSIZE_T_MAX;
if (start) {
int r = _PyEval_SliceIndex(start, &istart);
......@@ -1857,6 +1877,27 @@ Box* strEndswith(BoxedString* self, Box* elt, Box* start, Box** _args) {
throwCAPIException();
}
if (isSubclass(elt->cls, unicode_cls)) {
int r = PyUnicode_Tailmatch(self, elt, istart, iend, +1);
if (r < 0)
throwCAPIException();
assert(r == 0 || r == 1);
return boxBool(r);
}
if (isSubclass(elt->cls, tuple_cls)) {
for (auto e : static_cast<BoxedTuple*>(elt)->elts) {
auto b = strEndswith(self, e, start, _args);
assert(b->cls == bool_cls);
if (b == True)
return True;
}
return False;
}
if (elt->cls != str_cls)
raiseExcHelper(TypeError, "expected a character buffer object");
BoxedString* sub = static_cast<BoxedString*>(elt);
Py_ssize_t n = self->s.size();
......@@ -2358,7 +2399,7 @@ void setupStr() {
str_cls->giveAttr("join", new BoxedFunction(boxRTFunction((void*)strJoin, STR, 2)));
str_cls->giveAttr("replace",
new BoxedFunction(boxRTFunction((void*)strReplace, STR, 4, 1, false, false), { boxInt(-1) }));
new BoxedFunction(boxRTFunction((void*)strReplace, UNKNOWN, 4, 1, false, false), { boxInt(-1) }));
str_cls->giveAttr(
"split", new BoxedFunction(boxRTFunction((void*)strSplit, LIST, 3, 2, false, false), { None, boxInt(-1) }));
......
......@@ -139,3 +139,6 @@ except:
print repr("hello\tworld\t".expandtabs())
print repr("hello\tworld\t".expandtabs(12))
print "hello world".startswith(("x", "h"))
print "hello world".endswith(("x", "h"))
......@@ -81,3 +81,12 @@ f(**{'a':2})
f(**{u'a':3})
print repr('%s' % u'') # this gives a unicode object!
print repr('hello world'.replace(u'hello', u'hi'))
print "hello world".endswith(u'hello')
print "hello world".endswith(u'world')
print "hello world".startswith(u'hello')
print "hello world".startswith(u'world')
print float(u'1.0')
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