blob: 3951c2f8bbb757095678e16b751552dbeaf979dd [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
Paul Sokolovsky520e2f52014-02-12 18:31:30 +020029#include "mpconfig.h"
Paul Sokolovsky59c675a2014-06-21 22:43:22 +030030#include "misc.h"
Damien George5318cc02014-12-10 22:37:07 +000031#include "unicode.h"
Damien429d7192013-10-04 19:53:11 +010032
33// attribute flags
34#define FL_PRINT (0x01)
35#define FL_SPACE (0x02)
36#define FL_DIGIT (0x04)
37#define FL_ALPHA (0x08)
38#define FL_UPPER (0x10)
39#define FL_LOWER (0x20)
40
41// shorthand character attributes
42#define AT_PR (FL_PRINT)
43#define AT_SP (FL_SPACE | FL_PRINT)
44#define AT_DI (FL_DIGIT | FL_PRINT)
45#define AT_AL (FL_ALPHA | FL_PRINT)
46#define AT_UP (FL_UPPER | FL_ALPHA | FL_PRINT)
47#define AT_LO (FL_LOWER | FL_ALPHA | FL_PRINT)
48
49// table of attributes for ascii characters
Paul Sokolovsky520e2f52014-02-12 18:31:30 +020050STATIC const uint8_t attr[] = {
Damien429d7192013-10-04 19:53:11 +010051 0, 0, 0, 0, 0, 0, 0, 0,
Damien George175cecf2014-04-10 11:39:36 +010052 0, AT_SP, AT_SP, AT_SP, AT_SP, AT_SP, 0, 0,
Damien429d7192013-10-04 19:53:11 +010053 0, 0, 0, 0, 0, 0, 0, 0,
54 0, 0, 0, 0, 0, 0, 0, 0,
55 AT_SP, AT_PR, AT_PR, AT_PR, AT_PR, AT_PR, AT_PR, AT_PR,
56 AT_PR, AT_PR, AT_PR, AT_PR, AT_PR, AT_PR, AT_PR, AT_PR,
57 AT_DI, AT_DI, AT_DI, AT_DI, AT_DI, AT_DI, AT_DI, AT_DI,
58 AT_DI, AT_DI, AT_PR, AT_PR, AT_PR, AT_PR, AT_PR, AT_PR,
59 AT_PR, AT_UP, AT_UP, AT_UP, AT_UP, AT_UP, AT_UP, AT_UP,
60 AT_UP, AT_UP, AT_UP, AT_UP, AT_UP, AT_UP, AT_UP, AT_UP,
61 AT_UP, AT_UP, AT_UP, AT_UP, AT_UP, AT_UP, AT_UP, AT_UP,
62 AT_UP, AT_UP, AT_UP, AT_PR, AT_PR, AT_PR, AT_PR, AT_PR,
63 AT_PR, AT_LO, AT_LO, AT_LO, AT_LO, AT_LO, AT_LO, AT_LO,
64 AT_LO, AT_LO, AT_LO, AT_LO, AT_LO, AT_LO, AT_LO, AT_LO,
65 AT_LO, AT_LO, AT_LO, AT_LO, AT_LO, AT_LO, AT_LO, AT_LO,
66 AT_LO, AT_LO, AT_LO, AT_PR, AT_PR, AT_PR, AT_PR, 0
67};
68
Paul Sokolovsky1044c3d2014-06-14 06:39:20 +030069// TODO: Rename to str_get_char
70unichar utf8_get_char(const byte *s) {
71#if MICROPY_PY_BUILTINS_STR_UNICODE
Chris Angelicoc88987c2014-06-04 05:28:12 +100072 unichar ord = *s++;
73 if (!UTF8_IS_NONASCII(ord)) return ord;
74 ord &= 0x7F;
75 for (unichar mask = 0x40; ord & mask; mask >>= 1) {
76 ord &= ~mask;
77 }
78 while (UTF8_IS_CONT(*s)) {
79 ord = (ord << 6) | (*s++ & 0x3F);
80 }
81 return ord;
Paul Sokolovsky1044c3d2014-06-14 06:39:20 +030082#else
83 return *s;
84#endif
Damien429d7192013-10-04 19:53:11 +010085}
86
Paul Sokolovsky1044c3d2014-06-14 06:39:20 +030087// TODO: Rename to str_next_char
88const byte *utf8_next_char(const byte *s) {
89#if MICROPY_PY_BUILTINS_STR_UNICODE
Chris Angelicoc88987c2014-06-04 05:28:12 +100090 ++s;
91 while (UTF8_IS_CONT(*s)) {
92 ++s;
93 }
Paul Sokolovsky1044c3d2014-06-14 06:39:20 +030094 return s;
95#else
96 return s + 1;
97#endif
Damien429d7192013-10-04 19:53:11 +010098}
99
Damien George5318cc02014-12-10 22:37:07 +0000100mp_uint_t utf8_ptr_to_index(const byte *s, const byte *ptr) {
Damien George40f3c022014-07-03 13:25:24 +0100101 mp_uint_t i = 0;
Paul Sokolovsky46d31e92014-06-14 03:16:17 +0300102 while (ptr > s) {
103 if (!UTF8_IS_CONT(*--ptr)) {
104 i++;
105 }
106 }
107
108 return i;
109}
110
Paul Sokolovsky9e215fa2014-06-28 23:14:30 +0300111// TODO: Rename to str_charlen
Damien George40f3c022014-07-03 13:25:24 +0100112mp_uint_t unichar_charlen(const char *str, mp_uint_t len)
Chris Angelicoc88987c2014-06-04 05:28:12 +1000113{
Paul Sokolovsky1044c3d2014-06-14 06:39:20 +0300114#if MICROPY_PY_BUILTINS_STR_UNICODE
Damien George40f3c022014-07-03 13:25:24 +0100115 mp_uint_t charlen = 0;
Chris Angelicoc88987c2014-06-04 05:28:12 +1000116 for (const char *top = str + len; str < top; ++str) {
117 if (!UTF8_IS_CONT(*str)) {
118 ++charlen;
119 }
120 }
121 return charlen;
Paul Sokolovsky1044c3d2014-06-14 06:39:20 +0300122#else
123 return len;
124#endif
Chris Angelicoc88987c2014-06-04 05:28:12 +1000125}
126
127// Be aware: These unichar_is* functions are actually ASCII-only!
Damien George8cc96a32013-12-30 18:23:50 +0000128bool unichar_isspace(unichar c) {
Damien429d7192013-10-04 19:53:11 +0100129 return c < 128 && (attr[c] & FL_SPACE) != 0;
130}
131
Damien George8cc96a32013-12-30 18:23:50 +0000132bool unichar_isalpha(unichar c) {
Damien429d7192013-10-04 19:53:11 +0100133 return c < 128 && (attr[c] & FL_ALPHA) != 0;
134}
135
Damien George8cc96a32013-12-30 18:23:50 +0000136bool unichar_isprint(unichar c) {
Damien429d7192013-10-04 19:53:11 +0100137 return c < 128 && (attr[c] & FL_PRINT) != 0;
138}
139
Damien George8cc96a32013-12-30 18:23:50 +0000140bool unichar_isdigit(unichar c) {
Damien429d7192013-10-04 19:53:11 +0100141 return c < 128 && (attr[c] & FL_DIGIT) != 0;
142}
143
Paul Sokolovsky0b7184d2014-01-22 22:40:02 +0200144bool unichar_isxdigit(unichar c) {
145 return unichar_isdigit(c) || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F');
146}
147
Damien429d7192013-10-04 19:53:11 +0100148/*
Damien Georgec59af522014-05-11 17:53:11 +0100149bool unichar_is_alpha_or_digit(unichar c) {
Damien429d7192013-10-04 19:53:11 +0100150 return c < 128 && (attr[c] & (FL_ALPHA | FL_DIGIT)) != 0;
151}
Paul Sokolovsky69135212014-05-10 19:47:41 +0300152*/
Damien429d7192013-10-04 19:53:11 +0100153
Damien Georgec59af522014-05-11 17:53:11 +0100154bool unichar_isupper(unichar c) {
Damien429d7192013-10-04 19:53:11 +0100155 return c < 128 && (attr[c] & FL_UPPER) != 0;
156}
157
Damien Georgec59af522014-05-11 17:53:11 +0100158bool unichar_islower(unichar c) {
Damien429d7192013-10-04 19:53:11 +0100159 return c < 128 && (attr[c] & FL_LOWER) != 0;
160}
Paul Sokolovsky69135212014-05-10 19:47:41 +0300161
162unichar unichar_tolower(unichar c) {
Damien Georgec59af522014-05-11 17:53:11 +0100163 if (unichar_isupper(c)) {
Paul Sokolovsky69135212014-05-10 19:47:41 +0300164 return c + 0x20;
165 }
166 return c;
167}
168
169unichar unichar_toupper(unichar c) {
Damien Georgec59af522014-05-11 17:53:11 +0100170 if (unichar_islower(c)) {
Paul Sokolovsky69135212014-05-10 19:47:41 +0300171 return c - 0x20;
172 }
173 return c;
174}