This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Add a utility macro for reading hex digits.
authorYves Orton <demerphq@gmail.com>
Sat, 17 Nov 2012 13:04:38 +0000 (14:04 +0100)
committerYves Orton <demerphq@gmail.com>
Sat, 17 Nov 2012 13:04:38 +0000 (14:04 +0100)
Karl checked and it seems it actually works on EBCDIC as well
as on ASCII.

handy.h

diff --git a/handy.h b/handy.h
index 3b18a0f..6b9bca1 100644 (file)
--- a/handy.h
+++ b/handy.h
@@ -546,6 +546,10 @@ Returns a boolean indicating whether the specified character is a hexadecimal
 digit, [0-9A-Fa-f].  Variants C<isXDIGIT_A()> and C<isXDIGIT_L1()> are
 identical to C<isXDIGIT()>.
 
+=for apidoc Am|U8|READ_XDIGIT|char str*
+Returns the value of a hex digit and advances the string pointer.
+Behaviour is only well defined when isXDIGIT(*str) is true.
+
 =head1 Character case changing
 
 =for apidoc Am|char|toUPPER|char ch
@@ -1049,6 +1053,8 @@ typedef U32 line_t;
        } \
        return a;
 
+#define READ_XDIGIT(s) (isALPHA(*(s)) ? ((*(s)++ + 9) & 0xf) : (*(s)++ & 0xf))
+
 /*
 =head1 Memory Management