blob: d69e81c8e057cb731ed9d9a97ed56fe7799f8721 [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"
Damien429d7192013-10-04 19:53:11 +010031
32// attribute flags
33#define FL_PRINT (0x01)
34#define FL_SPACE (0x02)
35#define FL_DIGIT (0x04)
36#define FL_ALPHA (0x08)
37#define FL_UPPER (0x10)
38#define FL_LOWER (0x20)
39
40// shorthand character attributes
41#define AT_PR (FL_PRINT)
42#define AT_SP (FL_SPACE | FL_PRINT)
43#define AT_DI (FL_DIGIT | FL_PRINT)
44#define AT_AL (FL_ALPHA | FL_PRINT)
45#define AT_UP (FL_UPPER | FL_ALPHA | FL_PRINT)
46#define AT_LO (FL_LOWER | FL_ALPHA | FL_PRINT)
47
48// table of attributes for ascii characters
Paul Sokolovsky520e2f52014-02-12 18:31:30 +020049STATIC const uint8_t attr[] = {
Damien429d7192013-10-04 19:53:11 +010050 0, 0, 0, 0, 0, 0, 0, 0,
Damien George175cecf2014-04-10 11:39:36 +010051 0, AT_SP, AT_SP, AT_SP, AT_SP, AT_SP, 0, 0,
Damien429d7192013-10-04 19:53:11 +010052 0, 0, 0, 0, 0, 0, 0, 0,
53 0, 0, 0, 0, 0, 0, 0, 0,
54 AT_SP, AT_PR, AT_PR, AT_PR, AT_PR, AT_PR, AT_PR, AT_PR,
55 AT_PR, AT_PR, AT_PR, AT_PR, AT_PR, AT_PR, AT_PR, AT_PR,
56 AT_DI, AT_DI, AT_DI, AT_DI, AT_DI, AT_DI, AT_DI, AT_DI,
57 AT_DI, AT_DI, AT_PR, AT_PR, AT_PR, AT_PR, AT_PR, AT_PR,
58 AT_PR, 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_UP, AT_UP, AT_UP, AT_UP, AT_UP,
61 AT_UP, AT_UP, AT_UP, AT_PR, AT_PR, AT_PR, AT_PR, AT_PR,
62 AT_PR, 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_LO, AT_LO, AT_LO, AT_LO, AT_LO,
65 AT_LO, AT_LO, AT_LO, AT_PR, AT_PR, AT_PR, AT_PR, 0
66};
67
Paul Sokolovsky1044c3d2014-06-14 06:39:20 +030068// TODO: Rename to str_get_char
69unichar utf8_get_char(const byte *s) {
70#if MICROPY_PY_BUILTINS_STR_UNICODE
Chris Angelicoc88987c2014-06-04 05:28:12 +100071 unichar ord = *s++;
72 if (!UTF8_IS_NONASCII(ord)) return ord;
73 ord &= 0x7F;
74 for (unichar mask = 0x40; ord & mask; mask >>= 1) {
75 ord &= ~mask;
76 }
77 while (UTF8_IS_CONT(*s)) {
78 ord = (ord << 6) | (*s++ & 0x3F);
79 }
80 return ord;
Paul Sokolovsky1044c3d2014-06-14 06:39:20 +030081#else
82 return *s;
83#endif
Damien429d7192013-10-04 19:53:11 +010084}
85
Paul Sokolovsky1044c3d2014-06-14 06:39:20 +030086// TODO: Rename to str_next_char
87const byte *utf8_next_char(const byte *s) {
88#if MICROPY_PY_BUILTINS_STR_UNICODE
Chris Angelicoc88987c2014-06-04 05:28:12 +100089 ++s;
90 while (UTF8_IS_CONT(*s)) {
91 ++s;
92 }
Paul Sokolovsky1044c3d2014-06-14 06:39:20 +030093 return s;
94#else
95 return s + 1;
96#endif
Damien429d7192013-10-04 19:53:11 +010097}
98
Paul Sokolovsky46d31e92014-06-14 03:16:17 +030099machine_uint_t utf8_ptr_to_index(const char *s, const char *ptr) {
100 machine_uint_t i = 0;
101 while (ptr > s) {
102 if (!UTF8_IS_CONT(*--ptr)) {
103 i++;
104 }
105 }
106
107 return i;
108}
109
Paul Sokolovsky9e215fa2014-06-28 23:14:30 +0300110// TODO: Rename to str_charlen
111machine_uint_t unichar_charlen(const char *str, machine_uint_t len)
Chris Angelicoc88987c2014-06-04 05:28:12 +1000112{
Paul Sokolovsky1044c3d2014-06-14 06:39:20 +0300113#if MICROPY_PY_BUILTINS_STR_UNICODE
Paul Sokolovsky9e215fa2014-06-28 23:14:30 +0300114 machine_uint_t charlen = 0;
Chris Angelicoc88987c2014-06-04 05:28:12 +1000115 for (const char *top = str + len; str < top; ++str) {
116 if (!UTF8_IS_CONT(*str)) {
117 ++charlen;
118 }
119 }
120 return charlen;
Paul Sokolovsky1044c3d2014-06-14 06:39:20 +0300121#else
122 return len;
123#endif
Chris Angelicoc88987c2014-06-04 05:28:12 +1000124}
125
126// Be aware: These unichar_is* functions are actually ASCII-only!
Damien George8cc96a32013-12-30 18:23:50 +0000127bool unichar_isspace(unichar c) {
Damien429d7192013-10-04 19:53:11 +0100128 return c < 128 && (attr[c] & FL_SPACE) != 0;
129}
130
Damien George8cc96a32013-12-30 18:23:50 +0000131bool unichar_isalpha(unichar c) {
Damien429d7192013-10-04 19:53:11 +0100132 return c < 128 && (attr[c] & FL_ALPHA) != 0;
133}
134
Damien George8cc96a32013-12-30 18:23:50 +0000135bool unichar_isprint(unichar c) {
Damien429d7192013-10-04 19:53:11 +0100136 return c < 128 && (attr[c] & FL_PRINT) != 0;
137}
138
Damien George8cc96a32013-12-30 18:23:50 +0000139bool unichar_isdigit(unichar c) {
Damien429d7192013-10-04 19:53:11 +0100140 return c < 128 && (attr[c] & FL_DIGIT) != 0;
141}
142
Paul Sokolovsky0b7184d2014-01-22 22:40:02 +0200143bool unichar_isxdigit(unichar c) {
144 return unichar_isdigit(c) || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F');
145}
146
Damien429d7192013-10-04 19:53:11 +0100147/*
Damien Georgec59af522014-05-11 17:53:11 +0100148bool unichar_is_alpha_or_digit(unichar c) {
Damien429d7192013-10-04 19:53:11 +0100149 return c < 128 && (attr[c] & (FL_ALPHA | FL_DIGIT)) != 0;
150}
Paul Sokolovsky69135212014-05-10 19:47:41 +0300151*/
Damien429d7192013-10-04 19:53:11 +0100152
Damien Georgec59af522014-05-11 17:53:11 +0100153bool unichar_isupper(unichar c) {
Damien429d7192013-10-04 19:53:11 +0100154 return c < 128 && (attr[c] & FL_UPPER) != 0;
155}
156
Damien Georgec59af522014-05-11 17:53:11 +0100157bool unichar_islower(unichar c) {
Damien429d7192013-10-04 19:53:11 +0100158 return c < 128 && (attr[c] & FL_LOWER) != 0;
159}
Paul Sokolovsky69135212014-05-10 19:47:41 +0300160
161unichar unichar_tolower(unichar c) {
Damien Georgec59af522014-05-11 17:53:11 +0100162 if (unichar_isupper(c)) {
Paul Sokolovsky69135212014-05-10 19:47:41 +0300163 return c + 0x20;
164 }
165 return c;
166}
167
168unichar unichar_toupper(unichar c) {
Damien Georgec59af522014-05-11 17:53:11 +0100169 if (unichar_islower(c)) {
Paul Sokolovsky69135212014-05-10 19:47:41 +0300170 return c - 0x20;
171 }
172 return c;
173}