** convert an hexadecimal numeric string to a number, following ** C99 specification for 'strtod' */
| 111 | ** C99 specification for 'strtod' |
| 112 | */ |
| 113 | static lua_Number lua_strx2number (const char *s, char **endptr) { |
| 114 | lua_Number r = 0.0; |
| 115 | int e = 0, i = 0; |
| 116 | int neg = 0; /* 1 if number is negative */ |
| 117 | *endptr = cast(char *, s); /* nothing is valid yet */ |
| 118 | while (lisspace(cast_uchar(*s))) s++; /* skip initial spaces */ |
| 119 | neg = isneg(&s); /* check signal */ |
| 120 | if (!(*s == '0' && (*(s + 1) == 'x' || *(s + 1) == 'X'))) /* check '0x' */ |
| 121 | return 0.0; /* invalid format (no '0x') */ |
| 122 | s += 2; /* skip '0x' */ |
| 123 | r = readhexa(&s, r, &i); /* read integer part */ |
| 124 | if (*s == '.') { |
| 125 | s++; /* skip dot */ |
| 126 | r = readhexa(&s, r, &e); /* read fractional part */ |
| 127 | } |
| 128 | if (i == 0 && e == 0) |
| 129 | return 0.0; /* invalid format (no digit) */ |
| 130 | e *= -4; /* each fractional digit divides value by 2^-4 */ |
| 131 | *endptr = cast(char *, s); /* valid up to here */ |
| 132 | if (*s == 'p' || *s == 'P') { /* exponent part? */ |
| 133 | int exp1 = 0; |
| 134 | int neg1; |
| 135 | s++; /* skip 'p' */ |
| 136 | neg1 = isneg(&s); /* signal */ |
| 137 | if (!lisdigit(cast_uchar(*s))) |
| 138 | goto ret; /* must have at least one digit */ |
| 139 | while (lisdigit(cast_uchar(*s))) /* read exponent */ |
| 140 | exp1 = exp1 * 10 + *(s++) - '0'; |
| 141 | if (neg1) exp1 = -exp1; |
| 142 | e += exp1; |
| 143 | } |
| 144 | *endptr = cast(char *, s); /* valid up to here */ |
| 145 | ret: |
| 146 | if (neg) r = -r; |
| 147 | return (r * (1 << e)); |
| 148 | } |
| 149 | |
| 150 | #endif |
| 151 |
no test coverage detected