diff options
author | Damien George <damien.p.george@gmail.com> | 2014-03-01 19:50:50 +0000 |
---|---|---|
committer | Damien George <damien.p.george@gmail.com> | 2014-03-01 19:50:50 +0000 |
commit | 06201ff3d6d9485b2657fc9ac4aa8a306884322f (patch) | |
tree | ade288601c3afccafa403ee8e7e569b83c1d8443 /py/parsenumbase.c | |
parent | 793838a919c9cb848cb8c3f2d88d8b347bfd6083 (diff) | |
download | micropython-06201ff3d6d9485b2657fc9ac4aa8a306884322f.tar.gz micropython-06201ff3d6d9485b2657fc9ac4aa8a306884322f.zip |
py: Implement bit-shift and not operations for mpz.
Implement not, shl and shr in mpz library. Add function to create mpzs
on the stack, used for memory efficiency when rhs is a small int.
Factor out code to parse base-prefix of number into a dedicated function.
Diffstat (limited to 'py/parsenumbase.c')
-rw-r--r-- | py/parsenumbase.c | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/py/parsenumbase.c b/py/parsenumbase.c new file mode 100644 index 0000000000..ad24cc678b --- /dev/null +++ b/py/parsenumbase.c @@ -0,0 +1,40 @@ +#include "misc.h" +#include "mpconfig.h" +#include "parsenumbase.h" + +// find real radix base, and strip preceding '0x', '0o' and '0b' +// puts base in *base, and returns number of bytes to skip the prefix +int mp_parse_num_base(const char *str, uint len, int *base) { + const char *p = str; + int c = *(p++); + if ((*base == 0 || *base == 16) && c == '0') { + c = *(p++); + if ((c | 32) == 'x') { + *base = 16; + } else if (*base == 0 && (c | 32) == 'o') { + *base = 8; + } else if (*base == 0 && (c | 32) == 'b') { + *base = 2; + } else { + *base = 10; + p -= 2; + } + } else if (*base == 8 && c == '0') { + c = *(p++); + if ((c | 32) != 'o') { + p -= 2; + } + } else if (*base == 2 && c == '0') { + c = *(p++); + if ((c | 32) != 'b') { + p -= 2; + } + } else { + if (*base == 0) { + *base = 10; + } + p--; + } + return p - str; +} + |