blob: db4aa438ac4a8c409353dffb76a0a1c45cd349f3 [file] [log] [blame]
Damien George04b91472014-05-03 23:27:38 +01001/*
2 * This file is part of the Micro Python project, http://micropython.org/
3 *
4 * The MIT License (MIT)
5 *
6 * Copyright (c) 2013, 2014 Damien P. George
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 * THE SOFTWARE.
25 */
26
Damien429d7192013-10-04 19:53:11 +010027#include <stdint.h>
Damien429d7192013-10-04 19:53:11 +010028
Damien George51dfcb42015-01-01 20:27:54 +000029#include "py/unicode.h"
Damien429d7192013-10-04 19:53:11 +010030
31// attribute flags
32#define FL_PRINT (0x01)
33#define FL_SPACE (0x02)
34#define FL_DIGIT (0x04)
35#define FL_ALPHA (0x08)
36#define FL_UPPER (0x10)
37#define FL_LOWER (0x20)
38
39// shorthand character attributes
40#define AT_PR (FL_PRINT)
41#define AT_SP (FL_SPACE | FL_PRINT)
42#define AT_DI (FL_DIGIT | FL_PRINT)
43#define AT_AL (FL_ALPHA | FL_PRINT)
44#define AT_UP (FL_UPPER | FL_ALPHA | FL_PRINT)
45#define AT_LO (FL_LOWER | FL_ALPHA | FL_PRINT)
46
47// table of attributes for ascii characters
Paul Sokolovsky520e2f52014-02-12 18:31:30 +020048STATIC const uint8_t attr[] = {
Damien429d7192013-10-04 19:53:11 +010049 0, 0, 0, 0, 0, 0, 0, 0,
Damien George175cecf2014-04-10 11:39:36 +010050 0, AT_SP, AT_SP, AT_SP, AT_SP, AT_SP, 0, 0,
Damien429d7192013-10-04 19:53:11 +010051 0, 0, 0, 0, 0, 0, 0, 0,
52 0, 0, 0, 0, 0, 0, 0, 0,
53 AT_SP, AT_PR, AT_PR, AT_PR, AT_PR, AT_PR, AT_PR, AT_PR,
54 AT_PR, AT_PR, AT_PR, AT_PR, AT_PR, AT_PR, AT_PR, AT_PR,
55 AT_DI, AT_DI, AT_DI, AT_DI, AT_DI, AT_DI, AT_DI, AT_DI,
56 AT_DI, AT_DI, AT_PR, AT_PR, AT_PR, AT_PR, AT_PR, AT_PR,
57 AT_PR, AT_UP, AT_UP, AT_UP, AT_UP, AT_UP, AT_UP, AT_UP,
58 AT_UP, AT_UP, AT_UP, AT_UP, AT_UP, AT_UP, AT_UP, AT_UP,
59 AT_UP, AT_UP, AT_UP, AT_UP, AT_UP, AT_UP, AT_UP, AT_UP,
60 AT_UP, AT_UP, AT_UP, AT_PR, AT_PR, AT_PR, AT_PR, AT_PR,
61 AT_PR, AT_LO, AT_LO, AT_LO, AT_LO, AT_LO, AT_LO, AT_LO,
62 AT_LO, AT_LO, AT_LO, AT_LO, AT_LO, AT_LO, AT_LO, AT_LO,
63 AT_LO, AT_LO, AT_LO, AT_LO, AT_LO, AT_LO, AT_LO, AT_LO,
64 AT_LO, AT_LO, AT_LO, AT_PR, AT_PR, AT_PR, AT_PR, 0
65};
66
Paul Sokolovsky1044c3d2014-06-14 06:39:20 +030067// TODO: Rename to str_get_char
68unichar utf8_get_char(const byte *s) {
69#if MICROPY_PY_BUILTINS_STR_UNICODE
Chris Angelicoc88987c2014-06-04 05:28:12 +100070 unichar ord = *s++;
71 if (!UTF8_IS_NONASCII(ord)) return ord;
72 ord &= 0x7F;
73 for (unichar mask = 0x40; ord & mask; mask >>= 1) {
74 ord &= ~mask;
75 }
76 while (UTF8_IS_CONT(*s)) {
77 ord = (ord << 6) | (*s++ & 0x3F);
78 }
79 return ord;
Paul Sokolovsky1044c3d2014-06-14 06:39:20 +030080#else
81 return *s;
82#endif
Damien429d7192013-10-04 19:53:11 +010083}
84
Paul Sokolovsky1044c3d2014-06-14 06:39:20 +030085// TODO: Rename to str_next_char
86const byte *utf8_next_char(const byte *s) {
87#if MICROPY_PY_BUILTINS_STR_UNICODE
Chris Angelicoc88987c2014-06-04 05:28:12 +100088 ++s;
89 while (UTF8_IS_CONT(*s)) {
90 ++s;
91 }
Paul Sokolovsky1044c3d2014-06-14 06:39:20 +030092 return s;
93#else
94 return s + 1;
95#endif
Damien429d7192013-10-04 19:53:11 +010096}
97
Damien George5318cc02014-12-10 22:37:07 +000098mp_uint_t utf8_ptr_to_index(const byte *s, const byte *ptr) {
Damien George40f3c022014-07-03 13:25:24 +010099 mp_uint_t i = 0;
Paul Sokolovsky46d31e92014-06-14 03:16:17 +0300100 while (ptr > s) {
101 if (!UTF8_IS_CONT(*--ptr)) {
102 i++;
103 }
104 }
105
106 return i;
107}
108
Paul Sokolovsky9e215fa2014-06-28 23:14:30 +0300109// TODO: Rename to str_charlen
Damien George4dea9222015-04-09 15:29:54 +0000110mp_uint_t unichar_charlen(const char *str, mp_uint_t len) {
Paul Sokolovsky1044c3d2014-06-14 06:39:20 +0300111#if MICROPY_PY_BUILTINS_STR_UNICODE
Damien George40f3c022014-07-03 13:25:24 +0100112 mp_uint_t charlen = 0;
Chris Angelicoc88987c2014-06-04 05:28:12 +1000113 for (const char *top = str + len; str < top; ++str) {
114 if (!UTF8_IS_CONT(*str)) {
115 ++charlen;
116 }
117 }
118 return charlen;
Paul Sokolovsky1044c3d2014-06-14 06:39:20 +0300119#else
120 return len;
121#endif
Chris Angelicoc88987c2014-06-04 05:28:12 +1000122}
123
124// Be aware: These unichar_is* functions are actually ASCII-only!
Damien George8cc96a32013-12-30 18:23:50 +0000125bool unichar_isspace(unichar c) {
Damien429d7192013-10-04 19:53:11 +0100126 return c < 128 && (attr[c] & FL_SPACE) != 0;
127}
128
Damien George8cc96a32013-12-30 18:23:50 +0000129bool unichar_isalpha(unichar c) {
Damien429d7192013-10-04 19:53:11 +0100130 return c < 128 && (attr[c] & FL_ALPHA) != 0;
131}
132
Damien George8cc96a32013-12-30 18:23:50 +0000133bool unichar_isprint(unichar c) {
Damien429d7192013-10-04 19:53:11 +0100134 return c < 128 && (attr[c] & FL_PRINT) != 0;
135}
136
Damien George8cc96a32013-12-30 18:23:50 +0000137bool unichar_isdigit(unichar c) {
Damien429d7192013-10-04 19:53:11 +0100138 return c < 128 && (attr[c] & FL_DIGIT) != 0;
139}
140
Paul Sokolovsky0b7184d2014-01-22 22:40:02 +0200141bool unichar_isxdigit(unichar c) {
142 return unichar_isdigit(c) || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F');
143}
144
Damien429d7192013-10-04 19:53:11 +0100145/*
Damien Georgec59af522014-05-11 17:53:11 +0100146bool unichar_is_alpha_or_digit(unichar c) {
Damien429d7192013-10-04 19:53:11 +0100147 return c < 128 && (attr[c] & (FL_ALPHA | FL_DIGIT)) != 0;
148}
Paul Sokolovsky69135212014-05-10 19:47:41 +0300149*/
Damien429d7192013-10-04 19:53:11 +0100150
Damien Georgec59af522014-05-11 17:53:11 +0100151bool unichar_isupper(unichar c) {
Damien429d7192013-10-04 19:53:11 +0100152 return c < 128 && (attr[c] & FL_UPPER) != 0;
153}
154
Damien Georgec59af522014-05-11 17:53:11 +0100155bool unichar_islower(unichar c) {
Damien429d7192013-10-04 19:53:11 +0100156 return c < 128 && (attr[c] & FL_LOWER) != 0;
157}
Paul Sokolovsky69135212014-05-10 19:47:41 +0300158
159unichar unichar_tolower(unichar c) {
Damien Georgec59af522014-05-11 17:53:11 +0100160 if (unichar_isupper(c)) {
Paul Sokolovsky69135212014-05-10 19:47:41 +0300161 return c + 0x20;
162 }
163 return c;
164}
165
166unichar unichar_toupper(unichar c) {
Damien Georgec59af522014-05-11 17:53:11 +0100167 if (unichar_islower(c)) {
Paul Sokolovsky69135212014-05-10 19:47:41 +0300168 return c - 0x20;
169 }
170 return c;
171}