unicode: Make get_char()/next_char()/charlen() be 8-bit compatible.

Based on config define.
This commit is contained in:
Paul Sokolovsky 2014-06-14 06:39:20 +03:00
parent b1949e4c09
commit 1044c3dfe6
1 changed files with 17 additions and 3 deletions

View File

@ -65,7 +65,9 @@ STATIC const uint8_t attr[] = {
AT_LO, AT_LO, AT_LO, AT_PR, AT_PR, AT_PR, AT_PR, 0 AT_LO, AT_LO, AT_LO, AT_PR, AT_PR, AT_PR, AT_PR, 0
}; };
unichar utf8_get_char(const char *s) { // TODO: Rename to str_get_char
unichar utf8_get_char(const byte *s) {
#if MICROPY_PY_BUILTINS_STR_UNICODE
unichar ord = *s++; unichar ord = *s++;
if (!UTF8_IS_NONASCII(ord)) return ord; if (!UTF8_IS_NONASCII(ord)) return ord;
ord &= 0x7F; ord &= 0x7F;
@ -76,14 +78,22 @@ unichar utf8_get_char(const char *s) {
ord = (ord << 6) | (*s++ & 0x3F); ord = (ord << 6) | (*s++ & 0x3F);
} }
return ord; return ord;
#else
return *s;
#endif
} }
char *utf8_next_char(const char *s) { // TODO: Rename to str_next_char
const byte *utf8_next_char(const byte *s) {
#if MICROPY_PY_BUILTINS_STR_UNICODE
++s; ++s;
while (UTF8_IS_CONT(*s)) { while (UTF8_IS_CONT(*s)) {
++s; ++s;
} }
return (char *)s; return s;
#else
return s + 1;
#endif
} }
machine_uint_t utf8_ptr_to_index(const char *s, const char *ptr) { machine_uint_t utf8_ptr_to_index(const char *s, const char *ptr) {
@ -99,6 +109,7 @@ machine_uint_t utf8_ptr_to_index(const char *s, const char *ptr) {
uint unichar_charlen(const char *str, uint len) uint unichar_charlen(const char *str, uint len)
{ {
#if MICROPY_PY_BUILTINS_STR_UNICODE
uint charlen = 0; uint charlen = 0;
for (const char *top = str + len; str < top; ++str) { for (const char *top = str + len; str < top; ++str) {
if (!UTF8_IS_CONT(*str)) { if (!UTF8_IS_CONT(*str)) {
@ -106,6 +117,9 @@ uint unichar_charlen(const char *str, uint len)
} }
} }
return charlen; return charlen;
#else
return len;
#endif
} }
// Be aware: These unichar_is* functions are actually ASCII-only! // Be aware: These unichar_is* functions are actually ASCII-only!