extmod: Add ubinascii.unhexlify
This also pulls out hex_digit from py/lexer.c and makes unichar_hex_digit
diff --git a/py/lexer.c b/py/lexer.c
index 536208e..12cb5ae 100644
--- a/py/lexer.c
+++ b/py/lexer.c
@@ -261,16 +261,6 @@
"__debug__",
};
-STATIC mp_uint_t hex_digit(unichar c) {
- // c is assumed to be hex digit
- mp_uint_t n = c - '0';
- if (n > 9) {
- n &= ~('a' - 'A');
- n -= ('A' - ('9' + 1));
- }
- return n;
-}
-
// This is called with CUR_CHAR() before first hex digit, and should return with
// it pointing to last hex digit
// num_digits must be greater than zero
@@ -282,7 +272,7 @@
if (!unichar_isxdigit(c)) {
return false;
}
- num = (num << 4) + hex_digit(c);
+ num = (num << 4) + unichar_xdigit_value(c);
}
*result = num;
return true;
diff --git a/py/misc.h b/py/misc.h
index 2c00b68..91bd000 100644
--- a/py/misc.h
+++ b/py/misc.h
@@ -127,6 +127,7 @@
bool unichar_islower(unichar c);
unichar unichar_tolower(unichar c);
unichar unichar_toupper(unichar c);
+mp_uint_t unichar_xdigit_value(unichar c);
mp_uint_t unichar_charlen(const char *str, mp_uint_t len);
#define UTF8_IS_NONASCII(ch) ((ch) & 0x80)
#define UTF8_IS_CONT(ch) (((ch) & 0xC0) == 0x80)
diff --git a/py/qstrdefs.h b/py/qstrdefs.h
index 5d0dc9d..a5e543b 100644
--- a/py/qstrdefs.h
+++ b/py/qstrdefs.h
@@ -584,6 +584,7 @@
#if MICROPY_PY_UBINASCII
Q(ubinascii)
Q(hexlify)
+Q(unhexlify)
#endif
#if MICROPY_PY_MACHINE
diff --git a/py/unicode.c b/py/unicode.c
index db4aa43..63e9601 100644
--- a/py/unicode.c
+++ b/py/unicode.c
@@ -169,3 +169,13 @@
}
return c;
}
+
+mp_uint_t unichar_xdigit_value(unichar c) {
+ // c is assumed to be hex digit
+ mp_uint_t n = c - '0';
+ if (n > 9) {
+ n &= ~('a' - 'A');
+ n -= ('A' - ('9' + 1));
+ }
+ return n;
+}