mp.c: Accept `0x', `0o' and `0b' prefixes on strings with explicit base.
[catacomb-python] / mp.c
diff --git a/mp.c b/mp.c
index 59fce83..3093c83 100644 (file)
--- a/mp.c
+++ b/mp.c
@@ -122,6 +122,12 @@ mp *mp_frompyobject(PyObject *o, int radix)
     mp *x;
     size_t sz;
     TEXT_PTRLEN(o, sc.buf, sz); sc.lim = sc.buf + sz;
+    if (sc.buf + 2 < sc.lim && sc.buf[0] == '0' &&
+       (radix == 16 ? (sc.buf[1] == 'x' || sc.buf[1] == 'X') :
+        radix ==  8 ? (sc.buf[1] == 'o' || sc.buf[1] == 'O') :
+        radix ==  2 ? (sc.buf[1] == 'b' || sc.buf[1] == 'B') :
+        0))
+      sc.buf += 2;
     x = mp_read(MP_NEW, radix, &mptext_stringops, &sc);
     if (!x) return (0);
     if (sc.buf < sc.lim) { MP_DROP(x); return (0); }