/* -*-c-*-
*
- * $Id: mptext.c,v 1.14 2002/10/09 00:33:44 mdw Exp $
+ * $Id: mptext.c,v 1.17 2002/10/19 11:59:04 mdw Exp $
*
* Textual representation of multiprecision numbers
*
/*----- Revision history --------------------------------------------------*
*
* $Log: mptext.c,v $
+ * Revision 1.17 2002/10/19 11:59:04 mdw
+ * Fix leftovers bug in reading.
+ *
+ * Revision 1.16 2002/10/15 22:57:43 mdw
+ * Bug fix: prevent negative zero.
+ *
+ * Revision 1.15 2002/10/15 19:18:15 mdw
+ * Fix fencepost bugs in binary radix writing.
+ *
* Revision 1.14 2002/10/09 00:33:44 mdw
* Allow `0o' and `0b' prefixes for octal and binary (from Haskell)
*
m->f &= ~MP_UNDEF;
m = mp_lsr(m, m, (unsigned long)n * MPW_BITS + b);
}
+ ops->unget(ch, p);
goto done;
}}
if (f & f_neg)
m->f |= MP_NEG;
+ MP_SHRINK(m);
return (m);
#undef f_start
/* --- Work out where to start --- */
n = mp_bits(m);
- n += bit - (n % bit);
+ if (n % bit)
+ n += bit - (n % bit);
b = n % MPW_BITS;
n /= MPW_BITS;
-
- if (n > MP_LEN(m)) {
+
+ if (n >= MP_LEN(m)) {
n--;
b += MPW_BITS;
}
{
int rc;
+ if (MP_EQ(m, MP_ZERO))
+ return (ops->put("0", 1, p));
+
/* --- Set various things up --- */
m = MP_COPY(m);
int ok = 1;
int ib = *(int *)v[0].buf, ob = *(int *)v[2].buf;
dstr d = DSTR_INIT;
- mp *m = mp_readdstr(MP_NEW, &v[1], 0, ib);
+ size_t off = 0;
+ mp *m = mp_readdstr(MP_NEW, &v[1], &off, ib);
if (m) {
if (!ob) {
fprintf(stderr, "*** unexpected successful parse\n"
} else {
if (ob) {
fprintf(stderr, "*** unexpected parse failure\n"
- "*** input [%i] = ", ib);
+ "*** input [%2i] = ", ib);
if (ib < 0)
type_hex.dump(&v[1], stderr);
else
fputs(v[1].buf, stderr);
- fprintf(stderr, "\n*** expected [%i] = ", ob);
+ fprintf(stderr, "\n*** expected [%2i] = ", ob);
if (ob < 0)
type_hex.dump(&v[3], stderr);
else
}
}
+ if (v[1].len - off != v[4].len ||
+ memcmp(v[1].buf + off, v[4].buf, v[4].len) != 0) {
+ fprintf(stderr, "*** leftovers incorrect\n"
+ "*** input [%2i] = ", ib);
+ if (ib < 0)
+ type_hex.dump(&v[1], stderr);
+ else
+ fputs(v[1].buf, stderr);
+ fprintf(stderr, "\n*** expected `%s'\n"
+ "*** found `%s'\n",
+ v[4].buf, v[1].buf + off);
+ ok = 0;
+ }
+
dstr_destroy(&d);
assert(mparena_count(MPARENA_GLOBAL) == 0);
return (ok);
static test_chunk tests[] = {
{ "mptext-ascii", verify,
- { &type_int, &type_string, &type_int, &type_string, 0 } },
+ { &type_int, &type_string, &type_int, &type_string, &type_string, 0 } },
{ "mptext-bin-in", verify,
- { &type_int, &type_hex, &type_int, &type_string, 0 } },
+ { &type_int, &type_hex, &type_int, &type_string, &type_string, 0 } },
{ "mptext-bin-out", verify,
- { &type_int, &type_string, &type_int, &type_hex, 0 } },
+ { &type_int, &type_string, &type_int, &type_hex, &type_string, 0 } },
{ 0, 0, { 0 } }
};