blob: a58cdb14aa22474634a7351ca0196757414cb2a5 [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
Chris Angelicoc88987c2014-06-04 05:28:12 +1000110uint unichar_charlen(const char *str, uint len)
111{
Paul Sokolovsky1044c3d2014-06-14 06:39:20 +0300112#if MICROPY_PY_BUILTINS_STR_UNICODE
Chris Angelicoc88987c2014-06-04 05:28:12 +1000113 uint charlen = 0;
114 for (const char *top = str + len; str < top; ++str) {
115 if (!UTF8_IS_CONT(*str)) {
116 ++charlen;
117 }
118 }
119 return charlen;
Paul Sokolovsky1044c3d2014-06-14 06:39:20 +0300120#else
121 return len;
122#endif
Chris Angelicoc88987c2014-06-04 05:28:12 +1000123}
124
125// Be aware: These unichar_is* functions are actually ASCII-only!
Damien George8cc96a32013-12-30 18:23:50 +0000126bool unichar_isspace(unichar c) {
Damien429d7192013-10-04 19:53:11 +0100127 return c < 128 && (attr[c] & FL_SPACE) != 0;
128}
129
Damien George8cc96a32013-12-30 18:23:50 +0000130bool unichar_isalpha(unichar c) {
Damien429d7192013-10-04 19:53:11 +0100131 return c < 128 && (attr[c] & FL_ALPHA) != 0;
132}
133
Damien George8cc96a32013-12-30 18:23:50 +0000134bool unichar_isprint(unichar c) {
Damien429d7192013-10-04 19:53:11 +0100135 return c < 128 && (attr[c] & FL_PRINT) != 0;
136}
137
Damien George8cc96a32013-12-30 18:23:50 +0000138bool unichar_isdigit(unichar c) {
Damien429d7192013-10-04 19:53:11 +0100139 return c < 128 && (attr[c] & FL_DIGIT) != 0;
140}
141
Paul Sokolovsky0b7184d2014-01-22 22:40:02 +0200142bool unichar_isxdigit(unichar c) {
143 return unichar_isdigit(c) || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F');
144}
145
Damien429d7192013-10-04 19:53:11 +0100146/*
Damien Georgec59af522014-05-11 17:53:11 +0100147bool unichar_is_alpha_or_digit(unichar c) {
Damien429d7192013-10-04 19:53:11 +0100148 return c < 128 && (attr[c] & (FL_ALPHA | FL_DIGIT)) != 0;
149}
Paul Sokolovsky69135212014-05-10 19:47:41 +0300150*/
Damien429d7192013-10-04 19:53:11 +0100151
Damien Georgec59af522014-05-11 17:53:11 +0100152bool unichar_isupper(unichar c) {
Damien429d7192013-10-04 19:53:11 +0100153 return c < 128 && (attr[c] & FL_UPPER) != 0;
154}
155
Damien Georgec59af522014-05-11 17:53:11 +0100156bool unichar_islower(unichar c) {
Damien429d7192013-10-04 19:53:11 +0100157 return c < 128 && (attr[c] & FL_LOWER) != 0;
158}
Paul Sokolovsky69135212014-05-10 19:47:41 +0300159
160unichar unichar_tolower(unichar c) {
Damien Georgec59af522014-05-11 17:53:11 +0100161 if (unichar_isupper(c)) {
Paul Sokolovsky69135212014-05-10 19:47:41 +0300162 return c + 0x20;
163 }
164 return c;
165}
166
167unichar unichar_toupper(unichar c) {
Damien Georgec59af522014-05-11 17:53:11 +0100168 if (unichar_islower(c)) {
Paul Sokolovsky69135212014-05-10 19:47:41 +0300169 return c - 0x20;
170 }
171 return c;
172}