handy.h: Add two macros
authorKarl Williamson <public@khwilliamson.com>
Wed, 1 Jan 2014 05:35:46 +0000 (22:35 -0700)
committerKarl Williamson <public@khwilliamson.com>
Wed, 1 Jan 2014 20:49:24 +0000 (13:49 -0700)
handy.h contains a macro that reads a hex digit and returns its value,
with fewer branches than a naive implementation would use.  This commit
just copies and modifies it to create two macros for
    1) just converting the hex value, without advancing the input; and
    2) doing the same for an octal value.

handy.h

diff --git a/handy.h b/handy.h
index c4a162d..6b74c87 100644 (file)
--- a/handy.h
+++ b/handy.h
@@ -1564,14 +1564,23 @@ typedef U32 line_t;
        } \
        return a;
 
-/* Converts a hex digit in a string to its numeric value, advancing the
- * pointer.  The input must be known to be 0-9, A-F, or a-f.  In both ASCII and
- * EBCDIC the last 4 bits of the digits are 0-9; and the last 4 bits of A-F and
- * a-f are 1-6, so adding 9 yields 10-15 */
+/* Converts a character known to represent a hexadecimal digit (0-9, A-F, or
+ * a-f) to its numeric value.  READ_XDIGIT's argument is a string pointer,
+ * which is advanced.  The input is validated only by an assert() in DEBUGGING
+ * builds.  In both ASCII and EBCDIC the last 4 bits of the digits are 0-9; and
+ * the last 4 bits of A-F and a-f are 1-6, so adding 9 yields 10-15 */
+#define XDIGIT_VALUE(c) (__ASSERT_(isXDIGIT(c)) (0xf & (isDIGIT(c)        \
+                                                        ? (c)             \
+                                                        : ((c) + 9))))
 #define READ_XDIGIT(s)  (__ASSERT_(isXDIGIT(*s)) (0xf & (isDIGIT(*(s))     \
                                                         ? (*(s)++)         \
                                                         : (*(s)++ + 9))))
 
+/* Converts a character known to represent an octal digit (0-7) to its numeric
+ * value.  The input is validated only by an assert() in DEBUGGING builds.  In
+ * both ASCII and EBCDIC the last 3 bits of the octal digits range from 0-7. */
+#define OCTAL_VALUE(c) (__ASSERT_(isOCTAL(c)) (7 & (c)))
+
 /*
 =head1 Memory Management