diff options
author | Paul Sokolovsky <pfalcon@users.sourceforge.net> | 2014-06-14 03:16:17 +0300 |
---|---|---|
committer | Paul Sokolovsky <pfalcon@users.sourceforge.net> | 2014-06-27 00:04:19 +0300 |
commit | 46d31e9ca90635707031afedf09da7aeed25a321 (patch) | |
tree | 2630f1bcc455de21cb5c6875be23572bdd423ced /py | |
parent | ded0fc77f7ee70b3f7935921bb9631e9763fb1fc (diff) | |
download | micropython-46d31e9ca90635707031afedf09da7aeed25a321.tar.gz micropython-46d31e9ca90635707031afedf09da7aeed25a321.zip |
unicode: Add utf8_ptr_to_index().
Useful when we have pointer to char inside string, but need to return char
index. (E.g. str.find()).
Diffstat (limited to 'py')
-rw-r--r-- | py/unicode.c | 11 |
1 files changed, 11 insertions, 0 deletions
diff --git a/py/unicode.c b/py/unicode.c index 0da247889e..c381200722 100644 --- a/py/unicode.c +++ b/py/unicode.c @@ -86,6 +86,17 @@ char *utf8_next_char(const char *s) { return (char *)s; } +machine_uint_t utf8_ptr_to_index(const char *s, const char *ptr) { + machine_uint_t i = 0; + while (ptr > s) { + if (!UTF8_IS_CONT(*--ptr)) { + i++; + } + } + + return i; +} + uint unichar_charlen(const char *str, uint len) { uint charlen = 0; |