]> git.mxchange.org Git - simgear.git/blob - simgear/nasal/string.c
Sync with Nasal upstream (Melchior already had a chance to test this,
[simgear.git] / simgear / nasal / string.c
1 #include <math.h>
2 #include <string.h>
3
4 #include "nasal.h"
5 #include "data.h"
6
7 // The maximum number of significant (decimal!) figures in an IEEE
8 // double.
9 #define DIGITS 16
10
11 static int tonum(unsigned char* s, int len, double* result);
12 static int fromnum(double val, unsigned char* s);
13
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)
16
17 int naStr_len(naRef s)
18 {
19     return IS_STR(s) ? LEN(PTR(s).str) : 0;
20 }
21
22 char* naStr_data(naRef s)
23 {
24     return IS_STR(s) ? (char*)DATA(PTR(s).str) : 0;
25 }
26
27 static void setlen(struct naStr* s, int sz)
28 {
29     if(s->emblen == -1 && DATA(s)) naFree(s->data.ref.ptr);
30     if(sz > MAX_STR_EMBLEN) {
31         s->emblen = -1;
32         s->data.ref.len = sz;
33         s->data.ref.ptr = naAlloc(sz+1);
34     } else {
35         s->emblen = sz;
36     }
37     DATA(s)[sz] = 0; // nul terminate
38 }
39
40 naRef naStr_buf(naRef dst, int len)
41 {
42     setlen(PTR(dst).str, len);
43     naBZero(DATA(PTR(dst).str), len);
44     return dst;
45 }
46
47 naRef naStr_fromdata(naRef dst, const char* data, int len)
48 {
49     if(!IS_STR(dst)) return naNil();
50     setlen(PTR(dst).str, len);
51     memcpy(DATA(PTR(dst).str), data, len);
52     return dst;
53 }
54
55 naRef naStr_concat(naRef dest, naRef s1, naRef s2)
56 {
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));
64     return dest;
65 }
66
67 naRef naStr_substr(naRef dest, naRef str, int start, int len)
68 {
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();
73     setlen(dst, len);
74     memcpy(DATA(dst), DATA(s) + start, len);
75     return dest;
76 }
77
78 int naStr_equal(naRef s1, naRef s2)
79 {
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;
85     return 0;
86 }
87
88 naRef naStr_fromnum(naRef dest, double num)
89 {
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));
94     return dest;
95 }
96
97 int naStr_parsenum(char* str, int len, double* result)
98 {
99     return tonum((unsigned char*)str, len, result);
100 }
101
102 int naStr_tonum(naRef str, double* out)
103 {
104     return tonum(DATA(PTR(str).str), LEN(PTR(str).str), out);
105 }
106
107 int naStr_numeric(naRef str)
108 {
109     double dummy;
110     return tonum(DATA(PTR(str).str), LEN(PTR(str).str), &dummy);
111 }
112
113 void naStr_gcclean(struct naStr* str)
114 {
115     if(str->emblen == -1) naFree(str->data.ref.ptr);
116     str->data.ref.ptr = 0;
117     str->data.ref.len = 0;
118     str->emblen = -1;
119 }
120
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.
127 //
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
132 // remaining 67%.
133 ////////////////////////////////////////////////////////////////////////
134
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)
139 {
140     *v = 0;
141     if(i >= len) return len;
142     while(i < len && s[i] >= '0' && s[i] <= '9') {
143         *v= (*v) * 10 + (s[i] - '0');
144         i++;
145     }
146     return i;
147 }
148
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)
153 {
154     int i0 = i, i2;
155     double sgn=1, val;
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) {
161         *v = 0;
162         return i0; // don't successfully parse bare "+" or "-"
163     }
164     *v = sgn*val;
165     return i2;
166 }
167
168
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)
172 {
173     double v = 1;
174     int absexp;
175     if(exp < 0 || exp >= DIGITS)
176         return pow(10, exp);
177     else
178         absexp = exp < 0 ? -exp : exp;
179     while(absexp--) v *= 10.0;
180     return v;
181 }
182
183 static int tonum(unsigned char* s, int len, double* result)
184 {
185     int i=0, fraclen=0;
186     double sgn=1, val, frac=0, exp=0;
187
188     // Special case, "." is not a number, even though "1." and ".0" are.
189     if(len == 1 && s[0] == '.')
190         return 0;
191
192     // Strip off the leading negative sign first, so we can correctly
193     // parse things like -.xxx which would otherwise confuse
194     // readsigned.
195     if(len > 1 && s[0] == '-' && s[1] != '-') {
196         sgn = -1; s++; len--;
197     }
198
199     // Read the integer part
200     i = readsigned(s, len, i, &val);
201     if(val < 0) { sgn = -1; val = -val; }
202
203     // Read the fractional part, if any
204     if(i < len && s[i] == '.') {
205         i++;
206         fraclen = readdec(s, len, i, &frac) - i;
207         i += fraclen;
208     }
209
210     // Nothing so far?  Then the parse failed.
211     if(i == 0) return 0;
212
213     // Read the exponent, if any
214     if(i < len && (s[i] == 'e' || s[i] == 'E')) {
215         int i0 = i+1;
216         i = readsigned(s, len, i+1, &exp);
217         if(i == i0) return 0; // Must have a number after the "e"
218     }
219     
220     // compute the result
221     *result = sgn * (val + frac * decpow(-fraclen)) * decpow(exp);
222
223     // if we didn't use the whole string, return failure
224     if(i < len) return 0;
225     return 1;
226 }
227
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)
233 {
234     int p=1, i=0;
235     if(val == 0) { *s = '0'; return 1; }
236     while(p <= 999999999 && p*10 <= val) p *= 10;
237     while(p > 0) {
238         int count = 0;
239         while(val >= p) { val -= p; count++; }
240         s[i++] = '0' + count;
241         p /= 10;
242     }
243     return i;
244 }
245
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)
252 {
253     int exponent = (int)floor(log10(val));
254     double mantissa = val / pow(10, exponent);
255     int i, c;
256     for(i=0; i<DIGITS-1; i++) {
257         int digit = (int)floor(mantissa);
258         s[i] = '0' + digit;
259         mantissa -= digit;
260         mantissa *= 10.0;
261     }
262     // Round (i.e. don't floor) the last digit
263     c = (int)floor(mantissa);
264     if(mantissa - c >= 0.5) c++;
265     if(c < 0) c = 0;
266     if(c > 9) c = 9;
267     s[i] = '0' + c;
268     return exponent - DIGITS + 1;
269 }
270
271 static int fromnum(double val, unsigned char* s)
272 {
273     unsigned char raw[DIGITS];
274     unsigned char* ptr = s;
275     int exp, digs, i=0;
276
277     // Handle negatives
278     if(val < 0) { *ptr++ = '-'; val = -val; }
279
280     // Exactly an integer is a special case
281     if(val == (int)val) {
282         ptr += decprint(val, ptr);
283         *ptr = 0;
284         return ptr - s;
285     }
286
287     // Get the raw digits
288     exp = rawprint(val, raw);
289
290     // Examine trailing zeros to get a significant digit count
291     for(i=DIGITS-1; i>0; i--)
292         if(raw[i] != '0') break;
293     digs = i+1;
294
295     if(exp > 0 || exp < -(DIGITS+3)) {
296         // Standard scientific notation
297         exp += DIGITS-1;
298         *ptr++ = raw[0];
299         if(digs > 1) {
300             *ptr++ = '.';
301             for(i=1; i<digs; i++) *ptr++ = raw[i];
302         }
303         *ptr++ = 'e';
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];
313     } else {
314         // Integer part
315         for(i=0; i<DIGITS+exp; i++) *ptr++ = raw[i];
316         if(i < digs) {
317             // Fraction, if any
318             *ptr++ = '.';
319             while(i<digs) *ptr++ = raw[i++];
320         }
321     }
322     *ptr = 0;
323     return ptr - s;
324 }