7 // The maximum number of significant (decimal!) figures in an IEEE
11 static int tonum(unsigned char* s, int len, double* result);
12 static int fromnum(double val, unsigned char* s);
14 #define LEN(s) ((s)->emblen != -1 ? (s)->emblen : (s)->data.ref.len)
15 #define DATA(s) ((s)->emblen != -1 ? (s)->data.buf : (s)->data.ref.ptr)
17 int naStr_len(naRef s)
19 return IS_STR(s) ? LEN(PTR(s).str) : 0;
22 char* naStr_data(naRef s)
24 return IS_STR(s) ? (char*)DATA(PTR(s).str) : 0;
27 static void setlen(struct naStr* s, int sz)
29 if(s->emblen == -1 && DATA(s)) naFree(s->data.ref.ptr);
30 if(sz > MAX_STR_EMBLEN) {
33 s->data.ref.ptr = naAlloc(sz+1);
37 DATA(s)[sz] = 0; // nul terminate
40 naRef naStr_buf(naRef dst, int len)
42 setlen(PTR(dst).str, len);
43 naBZero(DATA(PTR(dst).str), len);
47 naRef naStr_fromdata(naRef dst, const char* data, int len)
49 if(!IS_STR(dst)) return naNil();
50 setlen(PTR(dst).str, len);
51 memcpy(DATA(PTR(dst).str), data, len);
55 naRef naStr_concat(naRef dest, naRef s1, naRef s2)
57 struct naStr* dst = PTR(dest).str;
58 struct naStr* a = PTR(s1).str;
59 struct naStr* b = PTR(s2).str;
60 if(!(IS_STR(s1)&&IS_STR(s2)&&IS_STR(dest))) return naNil();
61 setlen(dst, LEN(a) + LEN(b));
62 memcpy(DATA(dst), DATA(a), LEN(a));
63 memcpy(DATA(dst) + LEN(a), DATA(b), LEN(b));
67 naRef naStr_substr(naRef dest, naRef str, int start, int len)
69 struct naStr* dst = PTR(dest).str;
70 struct naStr* s = PTR(str).str;
71 if(!(IS_STR(dest)&&IS_STR(str))) return naNil();
72 if(start + len > LEN(s)) return naNil();
74 memcpy(DATA(dst), DATA(s) + start, len);
78 int naStr_equal(naRef s1, naRef s2)
80 struct naStr* a = PTR(s1).str;
81 struct naStr* b = PTR(s2).str;
82 if(DATA(a) == DATA(b)) return 1;
83 if(LEN(a) != LEN(b)) return 0;
84 if(memcmp(DATA(a), DATA(b), LEN(a)) == 0) return 1;
88 naRef naStr_fromnum(naRef dest, double num)
90 struct naStr* dst = PTR(dest).str;
91 unsigned char buf[DIGITS+8];
92 setlen(dst, fromnum(num, buf));
93 memcpy(DATA(dst), buf, LEN(dst));
97 int naStr_parsenum(char* str, int len, double* result)
99 return tonum((unsigned char*)str, len, result);
102 int naStr_tonum(naRef str, double* out)
104 return tonum(DATA(PTR(str).str), LEN(PTR(str).str), out);
107 int naStr_numeric(naRef str)
110 return tonum(DATA(PTR(str).str), LEN(PTR(str).str), &dummy);
113 void naStr_gcclean(struct naStr* str)
115 if(str->emblen == -1) naFree(str->data.ref.ptr);
116 str->data.ref.ptr = 0;
117 str->data.ref.len = 0;
121 ////////////////////////////////////////////////////////////////////////
122 // Below is a custom double<->string conversion library. Why not
123 // simply use sprintf and atof? Because they aren't acceptably
124 // platform independant, sadly. I've seen some very strange results.
125 // This works the same way everywhere, although it is tied to an
126 // assumption of standard IEEE 64 bit floating point doubles.
128 // In practice, these routines work quite well. Testing conversions
129 // of random doubles to strings and back, this routine is beaten by
130 // glibc on roundoff error 23% of the time, beats glibc in 10% of
131 // cases, and ties (usually with an error of exactly zero) the
133 ////////////////////////////////////////////////////////////////////////
135 // Reads an unsigned decimal out of the scalar starting at i, stores
136 // it in v, and returns the next index to start at. Zero-length
137 // decimal numbers are allowed, and are returned as zero.
138 static int readdec(unsigned char* s, int len, int i, double* v)
141 if(i >= len) return len;
142 while(i < len && s[i] >= '0' && s[i] <= '9') {
143 *v= (*v) * 10 + (s[i] - '0');
149 // Reads a signed integer out of the string starting at i, stores it
150 // in v, and returns the next index to start at. Zero-length
151 // decimal numbers are allowed, and are returned as zero.
152 static int readsigned(unsigned char* s, int len, int i, double* v)
156 if(i >= len) { *v = 0; return len; }
157 if(s[i] == '+') { i++; }
158 else if(s[i] == '-') { i++; sgn = -1; }
159 i2 = readdec(s, len, i, &val);
160 if(i0 == i && i2 == i) {
162 return i0; // don't successfully parse bare "+" or "-"
169 // Integer decimal power utility, with a tweak that enforces
170 // integer-exactness for arguments where that is possible.
171 static double decpow(int exp)
175 if(exp < 0 || exp >= DIGITS)
178 absexp = exp < 0 ? -exp : exp;
179 while(absexp--) v *= 10.0;
183 static int tonum(unsigned char* s, int len, double* result)
186 double sgn=1, val, frac=0, exp=0;
188 // Special case, "." is not a number, even though "1." and ".0" are.
189 if(len == 1 && s[0] == '.')
192 // Strip off the leading negative sign first, so we can correctly
193 // parse things like -.xxx which would otherwise confuse
195 if(len > 1 && s[0] == '-' && s[1] != '-') {
196 sgn = -1; s++; len--;
199 // Read the integer part
200 i = readsigned(s, len, i, &val);
201 if(val < 0) { sgn = -1; val = -val; }
203 // Read the fractional part, if any
204 if(i < len && s[i] == '.') {
206 fraclen = readdec(s, len, i, &frac) - i;
210 // Nothing so far? Then the parse failed.
213 // Read the exponent, if any
214 if(i < len && (s[i] == 'e' || s[i] == 'E')) {
216 i = readsigned(s, len, i+1, &exp);
217 if(i == i0) return 0; // Must have a number after the "e"
220 // compute the result
221 *result = sgn * (val + frac * decpow(-fraclen)) * decpow(exp);
223 // if we didn't use the whole string, return failure
224 if(i < len) return 0;
228 // Very simple positive (!) integer print routine. Puts the result in
229 // s and returns the number of characters written. Does not null
230 // terminate the result. Presumes at least a 32 bit integer, and
231 // cannot print integers larger than 9999999999.
232 static int decprint(int val, unsigned char* s)
235 if(val == 0) { *s = '0'; return 1; }
236 while(p <= 999999999 && p*10 <= val) p *= 10;
239 while(val >= p) { val -= p; count++; }
240 s[i++] = '0' + count;
246 // Takes a positive (!) floating point numbers, and returns exactly
247 // DIGITS decimal numbers in the buffer pointed to by s, and an
248 // integer exponent as the return value. For example, printing 1.0
249 // will result in "1000000000000000" in the buffer and -15 as the
250 // exponent. The caller can then place the floating point as needed.
251 static int rawprint(double val, unsigned char* s)
253 int exponent = (int)floor(log10(val));
254 double mantissa = val / pow(10, exponent);
256 for(i=0; i<DIGITS-1; i++) {
257 int digit = (int)floor(mantissa);
262 // Round (i.e. don't floor) the last digit
263 c = (int)floor(mantissa);
264 if(mantissa - c >= 0.5) c++;
268 return exponent - DIGITS + 1;
271 static int fromnum(double val, unsigned char* s)
273 unsigned char raw[DIGITS];
274 unsigned char* ptr = s;
278 if(val < 0) { *ptr++ = '-'; val = -val; }
280 // Exactly an integer is a special case
281 if(val == (int)val) {
282 ptr += decprint(val, ptr);
287 // Get the raw digits
288 exp = rawprint(val, raw);
290 // Examine trailing zeros to get a significant digit count
291 for(i=DIGITS-1; i>0; i--)
292 if(raw[i] != '0') break;
295 if(exp > 0 || exp < -(DIGITS+3)) {
296 // Standard scientific notation
301 for(i=1; i<digs; i++) *ptr++ = raw[i];
304 if(exp < 0) { exp = -exp; *ptr++ = '-'; }
305 else { *ptr++ = '+'; }
306 if(exp < 10) *ptr++ = '0';
307 ptr += decprint(exp, ptr);
308 } else if(exp < 1-DIGITS) {
309 // Fraction with insignificant leading zeros
310 *ptr++ = '0'; *ptr++ = '.';
311 for(i=0; i<-(exp+DIGITS); i++) *ptr++ = '0';
312 for(i=0; i<digs; i++) *ptr++ = raw[i];
315 for(i=0; i<DIGITS+exp; i++) *ptr++ = raw[i];
319 while(i<digs) *ptr++ = raw[i++];