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 int naStr_len(naRef s)
16 if(!IS_STR(s)) return 0;
17 return s.ref.ptr.str->len;
20 char* naStr_data(naRef s)
22 if(!IS_STR(s)) return 0;
23 return s.ref.ptr.str->data;
26 static void setlen(struct naStr* s, int sz)
28 if(s->data) naFree(s->data);
30 s->data = naAlloc(sz+1);
31 s->data[sz] = 0; // nul terminate
34 naRef naStr_fromdata(naRef dst, char* data, int len)
36 if(!IS_STR(dst)) return naNil();
37 setlen(dst.ref.ptr.str, len);
38 memcpy(dst.ref.ptr.str->data, data, len);
42 naRef naStr_concat(naRef dest, naRef s1, naRef s2)
44 struct naStr* dst = dest.ref.ptr.str;
45 struct naStr* a = s1.ref.ptr.str;
46 struct naStr* b = s2.ref.ptr.str;
47 if(!(IS_STR(s1)&&IS_STR(s2)&&IS_STR(dest))) return naNil();
48 setlen(dst, a->len + b->len);
49 memcpy(dst->data, a->data, a->len);
50 memcpy(dst->data + a->len, b->data, b->len);
54 naRef naStr_substr(naRef dest, naRef str, int start, int len)
56 struct naStr* dst = dest.ref.ptr.str;
57 struct naStr* s = str.ref.ptr.str;
58 if(!(IS_STR(dest)&&IS_STR(str))) return naNil();
59 if(start + len > s->len) { dst->len = 0; dst->data = 0; return naNil(); }
61 memcpy(dst->data, s->data + start, len);
65 int naStr_equal(naRef s1, naRef s2)
67 struct naStr* a = s1.ref.ptr.str;
68 struct naStr* b = s2.ref.ptr.str;
69 if(a->data == b->data) return 1;
70 if(a->len != b->len) return 0;
71 if(memcmp(a->data, b->data, a->len) == 0) return 1;
75 naRef naStr_fromnum(naRef dest, double num)
77 struct naStr* dst = dest.ref.ptr.str;
78 unsigned char buf[DIGITS+8];
79 setlen(dst, fromnum(num, buf));
80 memcpy(dst->data, buf, dst->len);
84 int naStr_parsenum(char* str, int len, double* result)
86 return tonum(str, len, result);
89 int naStr_tonum(naRef str, double* out)
91 return tonum(str.ref.ptr.str->data, str.ref.ptr.str->len, out);
94 int naStr_numeric(naRef str)
97 return tonum(str.ref.ptr.str->data, str.ref.ptr.str->len, &dummy);
100 void naStr_gcclean(struct naStr* str)
107 ////////////////////////////////////////////////////////////////////////
108 // Below is a custom double<->string conversion library. Why not
109 // simply use sprintf and atof? Because they aren't acceptably
110 // platform independant, sadly. I've seen some very strange results.
111 // This works the same way everywhere, although it is tied to an
112 // assumption of standard IEEE 64 bit floating point doubles.
114 // In practice, these routines work quite well. Testing conversions
115 // of random doubles to strings and back, this routine is beaten by
116 // glibc on roundoff error 23% of the time, beats glibc in 10% of
117 // cases, and ties (usually with an error of exactly zero) the
119 ////////////////////////////////////////////////////////////////////////
121 // Reads an unsigned decimal out of the scalar starting at i, stores
122 // it in v, and returns the next index to start at. Zero-length
123 // decimal numbers are allowed, and are returned as zero.
124 static int readdec(unsigned char* s, int len, int i, double* v)
127 if(i >= len) return len;
128 while(i < len && s[i] >= '0' && s[i] <= '9') {
129 *v= (*v) * 10 + (s[i] - '0');
135 // Reads a signed integer out of the string starting at i, stores it
136 // 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 readsigned(unsigned char* s, int len, int i, double* v)
142 if(i >= len) { *v = 0; return len; }
143 if(s[i] == '+') { i++; }
144 else if(s[i] == '-') { i++; sgn = -1; }
145 i2 = readdec(s, len, i, &val);
146 if(i0 == i && i2 == i) {
148 return i0; // don't successfully parse bare "+" or "-"
155 // Integer decimal power utility, with a tweak that enforces
156 // integer-exactness for arguments where that is possible.
157 static double decpow(int exp)
161 if(exp < 0 || exp >= DIGITS)
164 absexp = exp < 0 ? -exp : exp;
165 while(absexp--) v *= 10.0;
169 static int tonum(unsigned char* s, int len, double* result)
172 double sgn=1, val, frac=0, exp=0;
174 // Special case, "." is not a number, even though "1." and ".0" are.
175 if(len == 1 && s[0] == '.')
178 // Read the integer part
179 i = readsigned(s, len, i, &val);
180 if(val < 0) { sgn = -1; val = -val; }
182 // Read the fractional part, if any
183 if(i < len && s[i] == '.') {
185 fraclen = readdec(s, len, i, &frac) - i;
189 // Nothing so far? Then the parse failed.
192 // Read the exponent, if any
193 if(i < len && (s[i] == 'e' || s[i] == 'E')) {
195 i = readsigned(s, len, i+1, &exp);
196 if(i == i0) return 0; // Must have a number after the "e"
199 // compute the result
200 *result = sgn * (val + frac * decpow(-fraclen)) * decpow(exp);
202 // if we didn't use the whole string, return failure
203 if(i < len) return 0;
207 // Very simple positive (!) integer print routine. Puts the result in
208 // s and returns the number of characters written. Does not null
209 // terminate the result. Presumes at least a 32 bit integer, and
210 // cannot print integers larger than 9999999999.
211 static int decprint(int val, unsigned char* s)
214 if(val == 0) { *s = '0'; return 1; }
215 while(p <= 999999999 && p*10 <= val) p *= 10;
218 while(val >= p) { val -= p; count++; }
219 s[i++] = '0' + count;
225 // Takes a positive (!) floating point numbers, and returns exactly
226 // DIGITS decimal numbers in the buffer pointed to by s, and an
227 // integer exponent as the return value. For example, printing 1.0
228 // will result in "1000000000000000" in the buffer and -15 as the
229 // exponent. The caller can then place the floating point as needed.
230 static int rawprint(double val, unsigned char* s)
232 int exponent = (int)floor(log10(val));
233 double mantissa = val / pow(10, exponent);
235 for(i=0; i<DIGITS-1; i++) {
236 int digit = (int)floor(mantissa);
241 // Round (i.e. don't floor) the last digit
242 c = (int)floor(mantissa);
243 if(mantissa - c >= 0.5) c++;
247 return exponent - DIGITS + 1;
250 static int fromnum(double val, unsigned char* s)
252 unsigned char raw[DIGITS];
253 unsigned char* ptr = s;
257 if(val < 0) { *ptr++ = '-'; val = -val; }
259 // Exactly an integer is a special case
260 if(val == (int)val) {
261 ptr += decprint(val, ptr);
266 // Get the raw digits
267 exp = rawprint(val, raw);
269 // Examine trailing zeros to get a significant digit count
270 for(i=DIGITS-1; i>0; i--)
271 if(raw[i] != '0') break;
274 if(exp > 0 || exp < -(DIGITS+3)) {
275 // Standard scientific notation
280 for(i=1; i<digs; i++) *ptr++ = raw[i];
283 if(exp < 0) { exp = -exp; *ptr++ = '-'; }
284 else { *ptr++ = '+'; }
285 if(exp < 10) *ptr++ = '0';
286 ptr += decprint(exp, ptr);
287 } else if(exp < 1-DIGITS) {
288 // Fraction with insignificant leading zeros
289 *ptr++ = '0'; *ptr++ = '.';
290 for(i=0; i<-(exp+DIGITS); i++) *ptr++ = '0';
291 for(i=0; i<digs; i++) *ptr++ = raw[i];
294 for(i=0; i<DIGITS+exp; i++) *ptr++ = raw[i];
298 while(i<digs) *ptr++ = raw[i++];