blob: ad24cc678b4f578a346a55361051952356d83001 [file] [log] [blame]
Damien George06201ff2014-03-01 19:50:50 +00001#include "misc.h"
2#include "mpconfig.h"
3#include "parsenumbase.h"
4
5// find real radix base, and strip preceding '0x', '0o' and '0b'
6// puts base in *base, and returns number of bytes to skip the prefix
7int mp_parse_num_base(const char *str, uint len, int *base) {
8 const char *p = str;
9 int c = *(p++);
10 if ((*base == 0 || *base == 16) && c == '0') {
11 c = *(p++);
12 if ((c | 32) == 'x') {
13 *base = 16;
14 } else if (*base == 0 && (c | 32) == 'o') {
15 *base = 8;
16 } else if (*base == 0 && (c | 32) == 'b') {
17 *base = 2;
18 } else {
19 *base = 10;
20 p -= 2;
21 }
22 } else if (*base == 8 && c == '0') {
23 c = *(p++);
24 if ((c | 32) != 'o') {
25 p -= 2;
26 }
27 } else if (*base == 2 && c == '0') {
28 c = *(p++);
29 if ((c | 32) != 'b') {
30 p -= 2;
31 }
32 } else {
33 if (*base == 0) {
34 *base = 10;
35 }
36 p--;
37 }
38 return p - str;
39}
40