// double.
#define DIGITS 16
-// The minimum size we'll allocate for a string. Since a string
-// structure is already 12 bytes, and each naRef that points to it is
-// 8, there isn't much point in being stingy.
-#define MINLEN 16
-
static int tonum(unsigned char* s, int len, double* result);
static int fromnum(double val, unsigned char* s);
int naStr_len(naRef s)
{
if(!IS_STR(s)) return 0;
- return s.ref.ptr.str->len;
+ return PTR(s).str->len;
}
char* naStr_data(naRef s)
{
if(!IS_STR(s)) return 0;
- return s.ref.ptr.str->data;
+ return (char*)PTR(s).str->data;
}
static void setlen(struct naStr* s, int sz)
{
- int currSz, waste;
- sz += 1; // Allow for an extra nul terminator
- currSz = s->len+1 < MINLEN ? MINLEN : s->len+1;
- waste = currSz - sz; // how much extra if we don't reallocate?
- if(s->data == 0 || waste < 0 || waste > MINLEN) {
- naFree(s->data);
- s->data = naAlloc(sz < MINLEN ? MINLEN : sz);
- }
- s->len = sz - 1;
- s->data[s->len] = 0; // nul terminate
+ if(s->data) naFree(s->data);
+ s->len = sz;
+ s->data = naAlloc(sz+1);
+ s->data[sz] = 0; // nul terminate
+}
+
+naRef naStr_buf(naRef dst, int len)
+{
+ setlen(PTR(dst).str, len);
+ naBZero(PTR(dst).str->data, len);
+ return dst;
}
naRef naStr_fromdata(naRef dst, char* data, int len)
{
if(!IS_STR(dst)) return naNil();
- setlen(dst.ref.ptr.str, len);
- memcpy(dst.ref.ptr.str->data, data, len);
+ setlen(PTR(dst).str, len);
+ memcpy(PTR(dst).str->data, data, len);
return dst;
}
naRef naStr_concat(naRef dest, naRef s1, naRef s2)
{
- struct naStr* dst = dest.ref.ptr.str;
- struct naStr* a = s1.ref.ptr.str;
- struct naStr* b = s2.ref.ptr.str;
+ struct naStr* dst = PTR(dest).str;
+ struct naStr* a = PTR(s1).str;
+ struct naStr* b = PTR(s2).str;
if(!(IS_STR(s1)&&IS_STR(s2)&&IS_STR(dest))) return naNil();
setlen(dst, a->len + b->len);
memcpy(dst->data, a->data, a->len);
naRef naStr_substr(naRef dest, naRef str, int start, int len)
{
- struct naStr* dst = dest.ref.ptr.str;
- struct naStr* s = str.ref.ptr.str;
+ struct naStr* dst = PTR(dest).str;
+ struct naStr* s = PTR(str).str;
if(!(IS_STR(dest)&&IS_STR(str))) return naNil();
if(start + len > s->len) { dst->len = 0; dst->data = 0; return naNil(); }
setlen(dst, len);
int naStr_equal(naRef s1, naRef s2)
{
- struct naStr* a = s1.ref.ptr.str;
- struct naStr* b = s2.ref.ptr.str;
+ struct naStr* a = PTR(s1).str;
+ struct naStr* b = PTR(s2).str;
if(a->data == b->data) return 1;
if(a->len != b->len) return 0;
if(memcmp(a->data, b->data, a->len) == 0) return 1;
naRef naStr_fromnum(naRef dest, double num)
{
- struct naStr* dst = dest.ref.ptr.str;
+ struct naStr* dst = PTR(dest).str;
unsigned char buf[DIGITS+8];
setlen(dst, fromnum(num, buf));
memcpy(dst->data, buf, dst->len);
int naStr_parsenum(char* str, int len, double* result)
{
- return tonum(str, len, result);
+ return tonum((unsigned char*)str, len, result);
}
int naStr_tonum(naRef str, double* out)
{
- return tonum(str.ref.ptr.str->data, str.ref.ptr.str->len, out);
+ return tonum(PTR(str).str->data, PTR(str).str->len, out);
}
int naStr_numeric(naRef str)
{
double dummy;
- return tonum(str.ref.ptr.str->data, str.ref.ptr.str->len, &dummy);
+ return tonum(PTR(str).str->data, PTR(str).str->len, &dummy);
}
void naStr_gcclean(struct naStr* str)
{
- if(str->len > MINLEN) {
- naFree(str->data);
- str->data = 0;
- }
+ naFree(str->data);
+ str->data = 0;
str->len = 0;
}
if(len == 1 && s[0] == '.')
return 0;
+ // Strip off the leading negative sign first, so we can correctly
+ // parse things like -.xxx which would otherwise confuse
+ // readsigned.
+ if(len > 1 && s[0] == '-' && s[1] != '-') {
+ sgn = -1; s++; len--;
+ }
+
// Read the integer part
i = readsigned(s, len, i, &val);
if(val < 0) { sgn = -1; val = -val; }
if(i == 0) return 0;
// Read the exponent, if any
- if(i < len && (s[i] == 'e' || s[i] == 'E'))
+ if(i < len && (s[i] == 'e' || s[i] == 'E')) {
+ int i0 = i+1;
i = readsigned(s, len, i+1, &exp);
+ if(i == i0) return 0; // Must have a number after the "e"
+ }
// compute the result
*result = sgn * (val + frac * decpow(-fraclen)) * decpow(exp);
if(raw[i] != '0') break;
digs = i+1;
- if(exp > 0 || exp < -(DIGITS+2)) {
+ if(exp > 0 || exp < -(DIGITS+3)) {
// Standard scientific notation
exp += DIGITS-1;
*ptr++ = raw[0];