From 631673a10ee252166ac213b56c489d06aa876783 Mon Sep 17 00:00:00 2001 From: mdw Date: Sun, 13 Jan 2002 19:51:28 +0000 Subject: [PATCH] Extend the textual format to bases up to 62 by distinguishing case. --- mptext.c | 31 +++++++++++++++++++++++-------- tests/mptext | 12 +++++++++++- 2 files changed, 34 insertions(+), 9 deletions(-) diff --git a/mptext.c b/mptext.c index f6bc2e3..c479d78 100644 --- a/mptext.c +++ b/mptext.c @@ -1,6 +1,6 @@ /* -*-c-*- * - * $Id: mptext.c,v 1.11 2001/06/16 23:42:17 mdw Exp $ + * $Id: mptext.c,v 1.12 2002/01/13 19:51:18 mdw Exp $ * * Textual representation of multiprecision numbers * @@ -30,6 +30,9 @@ /*----- Revision history --------------------------------------------------* * * $Log: mptext.c,v $ + * Revision 1.12 2002/01/13 19:51:18 mdw + * Extend the textual format to bases up to 62 by distinguishing case. + * * Revision 1.11 2001/06/16 23:42:17 mdw * Typesetting fixes. * @@ -192,7 +195,7 @@ mp *mp_read(mp *m, int radix, const mptext_ops *ops, void *p) /* --- If the radix is zero, look for leading zeros --- */ if (radix > 0) { - assert(((void)"ascii radix must be <= 36", radix <= 36)); + assert(((void)"ascii radix must be <= 62", radix <= 62)); rd = radix; r = -1; } else if (radix < 0) { @@ -270,9 +273,12 @@ restart: if (ch >= '0' && ch <= '9') x = ch - '0'; else { - ch = tolower(ch); + if (rd <= 36) + ch = tolower(ch); if (ch >= 'a' && ch <= 'z') /* ASCII dependent! */ x = ch - 'a' + 10; + else if (ch >= 'A' && ch <= 'Z') + x = ch - 'A' + 36; else break; } @@ -364,9 +370,12 @@ restart: if (ch >= '0' && ch <= '9') x = ch - '0'; else { - ch = tolower(ch); + if (rd <= 36) + ch = tolower(ch); if (ch >= 'a' && ch <= 'z') /* ASCII dependent! */ x = ch - 'a' + 10; + else if (ch >= 'A' && ch <= 'Z') + x = ch - 'A' + 36; else break; } @@ -519,8 +528,10 @@ static int simple(mpw n, int radix, unsigned z, ch = x; else if (x < 10) ch = '0' + x; - else + else if (x < 36) /* Ascii specific */ ch = 'a' + x - 10; + else + ch = 'A' + x - 36; buf[--i] = ch; if (z) z--; @@ -639,8 +650,10 @@ static int binary(mp *m, int bit, int radix, const mptext_ops *ops, void *p) ch = x; else if (x < 10) ch = '0' + x; + else if (x < 36) + ch = 'a' + x - 10; /* Ascii specific */ else - ch = 'a' + x - 10; + ch = 'A' + x - 36; *q++ = ch; if (q >= buf + sizeof(buf)) { if ((rc = ops->put(buf, sizeof(buf), p)) != 0) @@ -655,8 +668,10 @@ static int binary(mp *m, int bit, int radix, const mptext_ops *ops, void *p) ch = x; else if (x < 10) ch = '0' + x; + else if (x < 36) + ch = 'a' + x - 10; /* Ascii specific */ else - ch = 'a' + x - 10; + ch = 'A' + x - 36; *q++ = ch; rc = ops->put(buf, q - buf, p); @@ -681,7 +696,7 @@ int mp_write(mp *m, int radix, const mptext_ops *ops, void *p) /* --- Check the radix for sensibleness --- */ if (radix > 0) - assert(((void)"ascii radix must be <= 36", radix <= 36)); + assert(((void)"ascii radix must be <= 62", radix <= 62)); else if (radix < 0) assert(((void)"binary radix must fit in a byte", -radix < UCHAR_MAX)); else diff --git a/tests/mptext b/tests/mptext index 1d9168a..a81cfa9 100644 --- a/tests/mptext +++ b/tests/mptext @@ -1,6 +1,6 @@ # Test vectors for MP textual I/O # -# $Id: mptext,v 1.7 2001/06/16 13:22:40 mdw Exp $ +# $Id: mptext,v 1.8 2002/01/13 19:51:28 mdw Exp $ mptext-ascii { # --- Perfectly valid things --- @@ -28,6 +28,16 @@ mptext-ascii { 0 37_ 10 37; # 37 is an invalid base, so stop at `_' 0 36_ 0 0; # 36 is a valid base, so restart and fail + # --- Big bases --- + + 62 0 10 0; + 10 0 62 0; + 36 A 10 10; + 36 Z 10 35; + 37 A 10 36; + 10 36 37 A; + 62 Z 10 61; + # --- Word-boundary tests for binary-radix translation --- 8 1234567012 8 1234567012; -- 2.11.0