blob: c3812007226e58e34402bffcc0f0fdaaf3557d00 [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
Chris Angelicoc88987c2014-06-04 05:28:12 +100068unichar utf8_get_char(const char *s) {
69 unichar ord = *s++;
70 if (!UTF8_IS_NONASCII(ord)) return ord;
71 ord &= 0x7F;
72 for (unichar mask = 0x40; ord & mask; mask >>= 1) {
73 ord &= ~mask;
74 }
75 while (UTF8_IS_CONT(*s)) {
76 ord = (ord << 6) | (*s++ & 0x3F);
77 }
78 return ord;
Damien429d7192013-10-04 19:53:11 +010079}
80
Chris Angelicoc88987c2014-06-04 05:28:12 +100081char *utf8_next_char(const char *s) {
82 ++s;
83 while (UTF8_IS_CONT(*s)) {
84 ++s;
85 }
86 return (char *)s;
Damien429d7192013-10-04 19:53:11 +010087}
88
Paul Sokolovsky46d31e92014-06-14 03:16:17 +030089machine_uint_t utf8_ptr_to_index(const char *s, const char *ptr) {
90 machine_uint_t i = 0;
91 while (ptr > s) {
92 if (!UTF8_IS_CONT(*--ptr)) {
93 i++;
94 }
95 }
96
97 return i;
98}
99
Chris Angelicoc88987c2014-06-04 05:28:12 +1000100uint unichar_charlen(const char *str, uint len)
101{
102 uint charlen = 0;
103 for (const char *top = str + len; str < top; ++str) {
104 if (!UTF8_IS_CONT(*str)) {
105 ++charlen;
106 }
107 }
108 return charlen;
109}
110
111// Be aware: These unichar_is* functions are actually ASCII-only!
Damien George8cc96a32013-12-30 18:23:50 +0000112bool unichar_isspace(unichar c) {
Damien429d7192013-10-04 19:53:11 +0100113 return c < 128 && (attr[c] & FL_SPACE) != 0;
114}
115
Damien George8cc96a32013-12-30 18:23:50 +0000116bool unichar_isalpha(unichar c) {
Damien429d7192013-10-04 19:53:11 +0100117 return c < 128 && (attr[c] & FL_ALPHA) != 0;
118}
119
Damien George8cc96a32013-12-30 18:23:50 +0000120bool unichar_isprint(unichar c) {
Damien429d7192013-10-04 19:53:11 +0100121 return c < 128 && (attr[c] & FL_PRINT) != 0;
122}
123
Damien George8cc96a32013-12-30 18:23:50 +0000124bool unichar_isdigit(unichar c) {
Damien429d7192013-10-04 19:53:11 +0100125 return c < 128 && (attr[c] & FL_DIGIT) != 0;
126}
127
Paul Sokolovsky0b7184d2014-01-22 22:40:02 +0200128bool unichar_isxdigit(unichar c) {
129 return unichar_isdigit(c) || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F');
130}
131
Damien429d7192013-10-04 19:53:11 +0100132/*
Damien Georgec59af522014-05-11 17:53:11 +0100133bool unichar_is_alpha_or_digit(unichar c) {
Damien429d7192013-10-04 19:53:11 +0100134 return c < 128 && (attr[c] & (FL_ALPHA | FL_DIGIT)) != 0;
135}
Paul Sokolovsky69135212014-05-10 19:47:41 +0300136*/
Damien429d7192013-10-04 19:53:11 +0100137
Damien Georgec59af522014-05-11 17:53:11 +0100138bool unichar_isupper(unichar c) {
Damien429d7192013-10-04 19:53:11 +0100139 return c < 128 && (attr[c] & FL_UPPER) != 0;
140}
141
Damien Georgec59af522014-05-11 17:53:11 +0100142bool unichar_islower(unichar c) {
Damien429d7192013-10-04 19:53:11 +0100143 return c < 128 && (attr[c] & FL_LOWER) != 0;
144}
Paul Sokolovsky69135212014-05-10 19:47:41 +0300145
146unichar unichar_tolower(unichar c) {
Damien Georgec59af522014-05-11 17:53:11 +0100147 if (unichar_isupper(c)) {
Paul Sokolovsky69135212014-05-10 19:47:41 +0300148 return c + 0x20;
149 }
150 return c;
151}
152
153unichar unichar_toupper(unichar c) {
Damien Georgec59af522014-05-11 17:53:11 +0100154 if (unichar_islower(c)) {
Paul Sokolovsky69135212014-05-10 19:47:41 +0300155 return c - 0x20;
156 }
157 return c;
158}