xyb | c178ea4 | 2014-01-14 21:39:05 +0800 | [diff] [blame] | 1 | #if defined(UNIX) |
| 2 | |
| 3 | #include <ctype.h> |
| 4 | #include <errno.h> |
xyb | c178ea4 | 2014-01-14 21:39:05 +0800 | [diff] [blame] | 5 | #include <stdlib.h> |
xyb | c178ea4 | 2014-01-14 21:39:05 +0800 | [diff] [blame] | 6 | |
| 7 | #include "misc.h" |
Damien George | 2613ffd | 2014-02-22 17:49:15 +0000 | [diff] [blame^] | 8 | #include "strtonum.h" |
xyb | c178ea4 | 2014-01-14 21:39:05 +0800 | [diff] [blame] | 9 | #include "mpconfig.h" |
Damien George | 55baff4 | 2014-01-21 21:40:13 +0000 | [diff] [blame] | 10 | #include "qstr.h" |
xyb | c178ea4 | 2014-01-14 21:39:05 +0800 | [diff] [blame] | 11 | #include "nlr.h" |
| 12 | #include "obj.h" |
| 13 | |
Damien George | 2613ffd | 2014-02-22 17:49:15 +0000 | [diff] [blame^] | 14 | long mp_strtonum(const char *restrict s, int base) { |
xyb | c178ea4 | 2014-01-14 21:39:05 +0800 | [diff] [blame] | 15 | int c, neg = 0; |
| 16 | const char *p = s; |
| 17 | char *num; |
| 18 | long found; |
| 19 | |
| 20 | // check radix base |
| 21 | if ((base != 0 && base < 2) || base > 36) { |
Damien George | c596612 | 2014-02-15 16:10:44 +0000 | [diff] [blame] | 22 | nlr_jump(mp_obj_new_exception_msg(&mp_type_ValueError, "ValueError: int() arg 2 must be >=2 and <= 36")); |
xyb | c178ea4 | 2014-01-14 21:39:05 +0800 | [diff] [blame] | 23 | } |
| 24 | // skip surrounded whitespace |
| 25 | while (isspace((c = *(p++)))); |
| 26 | if (c == 0) { |
| 27 | goto value_error; |
| 28 | } |
| 29 | // preced sign |
| 30 | if (c == '+' || c == '-') { |
| 31 | neg = - (c == '-'); |
| 32 | c = *(p++); |
| 33 | } |
| 34 | |
| 35 | // find real radix base, and strip preced '0x', '0o' and '0b' |
Damien George | 5573f9f | 2014-01-15 22:58:39 +0000 | [diff] [blame] | 36 | // TODO somehow merge with similar code in parse.c |
xyb | c178ea4 | 2014-01-14 21:39:05 +0800 | [diff] [blame] | 37 | if ((base == 0 || base == 16) && c == '0') { |
| 38 | c = *(p++); |
| 39 | if ((c | 32) == 'x') { |
| 40 | base = 16; |
| 41 | } else if (base == 0 && (c | 32) == 'o') { |
| 42 | base = 8; |
| 43 | } else if (base == 0 && (c | 32) == 'b') { |
| 44 | base = 2; |
| 45 | } else { |
| 46 | base = 10; |
| 47 | p -= 2; |
| 48 | } |
| 49 | } else if (base == 8 && c == '0') { |
xyb | 3270fb4 | 2014-01-15 19:58:11 +0800 | [diff] [blame] | 50 | c = *(p++); |
| 51 | if ((c | 32) != 'o') { |
| 52 | p -= 2; |
xyb | c178ea4 | 2014-01-14 21:39:05 +0800 | [diff] [blame] | 53 | } |
| 54 | } else if (base == 2 && c == '0') { |
xyb | 3270fb4 | 2014-01-15 19:58:11 +0800 | [diff] [blame] | 55 | c = *(p++); |
| 56 | if ((c | 32) != 'b') { |
| 57 | p -= 2; |
xyb | c178ea4 | 2014-01-14 21:39:05 +0800 | [diff] [blame] | 58 | } |
| 59 | } else { |
| 60 | if (base == 0) base = 10; |
| 61 | p--; |
| 62 | } |
| 63 | |
Damien George | b99d9ea | 2014-01-15 23:08:33 +0000 | [diff] [blame] | 64 | errno = 0; |
xyb | c178ea4 | 2014-01-14 21:39:05 +0800 | [diff] [blame] | 65 | found = strtol(p, &num, base); |
| 66 | if (errno) { |
| 67 | goto value_error; |
| 68 | } else if (found && *(num) == 0) { |
| 69 | goto done; |
| 70 | } else if (found || num != p) { |
| 71 | goto check_tail_space; |
| 72 | } else { |
| 73 | goto value_error; |
| 74 | } |
| 75 | |
| 76 | check_tail_space: |
| 77 | if (*(num) != 0) { |
| 78 | while (isspace((c = *(num++)))); |
| 79 | if (c != 0) { |
| 80 | goto value_error; |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | done: |
| 85 | return (found ^ neg) - neg; |
| 86 | |
| 87 | value_error: |
Damien George | c596612 | 2014-02-15 16:10:44 +0000 | [diff] [blame] | 88 | nlr_jump(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "invalid literal for int() with base %d: '%s'", base, s)); |
xyb | c178ea4 | 2014-01-14 21:39:05 +0800 | [diff] [blame] | 89 | } |
| 90 | |
xyb | c178ea4 | 2014-01-14 21:39:05 +0800 | [diff] [blame] | 91 | #else /* defined(UNIX) */ |
| 92 | |
Damien George | 2613ffd | 2014-02-22 17:49:15 +0000 | [diff] [blame^] | 93 | long mp_strtonum(const char *restrict s, int base) { |
xyb | c178ea4 | 2014-01-14 21:39:05 +0800 | [diff] [blame] | 94 | // TODO port strtol to stm |
| 95 | return 0; |
| 96 | } |
| 97 | |
| 98 | #endif /* defined(UNIX) */ |