MCPcopy Create free account
hub / github.com/F-Stack/f-stack / lua_strx2number

Function lua_strx2number

freebsd/contrib/openzfs/module/lua/lobject.c:113–148  ·  view source on GitHub ↗

** convert an hexadecimal numeric string to a number, following ** C99 specification for 'strtod' */

Source from the content-addressed store, hash-verified

111** C99 specification for 'strtod'
112*/
113static 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

Callers 1

luaO_str2dFunction · 0.85

Calls 2

isnegFunction · 0.85
readhexaFunction · 0.85

Tested by

no test coverage detected