Subversion Repositories HelenOS

Rev

Rev 4055 | Rev 4221 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 4055 Rev 4201
Line 40... Line 40...
40
#include <cpu.h>
40
#include <cpu.h>
41
#include <arch/asm.h>
41
#include <arch/asm.h>
42
#include <arch.h>
42
#include <arch.h>
43
#include <console/kconsole.h>
43
#include <console/kconsole.h>
44
 
44
 
-
 
45
char invalch = '?';
-
 
46
 
-
 
47
/** Byte mask consisting of lowest @n bits (out of eight). */
-
 
48
#define LO_MASK_8(n) ((uint8_t)((1 << (n)) - 1))
-
 
49
 
-
 
50
/** Byte mask consisting of lowest @n bits (out of 32). */
-
 
51
#define LO_MASK_32(n) ((uint32_t)((1 << (n)) - 1))
-
 
52
 
-
 
53
/** Byte mask consisting of highest @n bits (out of eight). */
-
 
54
#define HI_MASK_8(n) (~LO_MASK_8(8 - (n)))
-
 
55
 
-
 
56
/** Number of data bits in a UTF-8 continuation byte. */
-
 
57
#define CONT_BITS 6
-
 
58
 
45
/** Return number of characters in a string.
59
/** Decode a single character from a substring.
-
 
60
 *
-
 
61
 * Decode a single character from a substring of size @a sz. Decoding starts
-
 
62
 * at @a offset and this offset is moved to the beginning of the next
-
 
63
 * character. In case of decoding error, offset generally advances at least
-
 
64
 * by one. However, offset is never moved beyond (str + sz).
-
 
65
 *
-
 
66
 * @param str   String (not necessarily NULL-terminated).
-
 
67
 * @param index Index (counted in plain characters) where to start
-
 
68
 *              the decoding.
-
 
69
 * @param limit Size of the substring.
-
 
70
 *
-
 
71
 * @return  Value of decoded character or '?' on decoding error.
-
 
72
 *
-
 
73
 */
-
 
74
wchar_t chr_decode(const char *str, size_t *offset, size_t sz)
-
 
75
{
-
 
76
    uint8_t b0, b;          /* Bytes read from str. */
-
 
77
    wchar_t ch;
-
 
78
 
-
 
79
    int b0_bits;        /* Data bits in first byte. */
-
 
80
    int cbytes;     /* Number of continuation bytes. */
-
 
81
 
-
 
82
    if (*offset + 1 > sz)
-
 
83
        return invalch;
-
 
84
 
-
 
85
    b0 = (uint8_t) str[(*offset)++];
-
 
86
 
-
 
87
    /* Determine code length. */
-
 
88
 
-
 
89
    if ((b0 & 0x80) == 0) {
-
 
90
        /* 0xxxxxxx (Plain ASCII) */
-
 
91
        b0_bits = 7;
-
 
92
        cbytes = 0;
-
 
93
    } else if ((b0 & 0xe0) == 0xc0) {
-
 
94
        /* 110xxxxx 10xxxxxx */
-
 
95
        b0_bits = 5;
-
 
96
        cbytes = 1;
-
 
97
    } else if ((b0 & 0xf0) == 0xe0) {
-
 
98
        /* 1110xxxx 10xxxxxx 10xxxxxx */
-
 
99
        b0_bits = 4;
-
 
100
        cbytes = 2;
-
 
101
    } else if ((b0 & 0xf8) == 0xf0) {
-
 
102
        /* 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx */
-
 
103
        b0_bits = 3;
-
 
104
        cbytes = 3;
-
 
105
    } else {
-
 
106
        /* 10xxxxxx -- unexpected continuation byte. */
-
 
107
        return invalch;
-
 
108
    }
-
 
109
 
-
 
110
    if (*offset + cbytes > sz) {
-
 
111
        return invalch;
-
 
112
    }
-
 
113
 
-
 
114
    ch = b0 & LO_MASK_8(b0_bits);
-
 
115
 
-
 
116
    /* Decode continuation bytes. */
-
 
117
    while (cbytes > 0) {
-
 
118
        b = (uint8_t) str[(*offset)++];
-
 
119
 
-
 
120
        /* Must be 10xxxxxx. */
-
 
121
        if ((b & 0xc0) != 0x80) {
-
 
122
            return invalch;
-
 
123
        }
-
 
124
 
-
 
125
        /* Shift data bits to ch. */
-
 
126
        ch = (ch << CONT_BITS) | (wchar_t) (b & LO_MASK_8(CONT_BITS));
-
 
127
        --cbytes;
-
 
128
    }
-
 
129
 
-
 
130
    return ch;
-
 
131
}
-
 
132
 
-
 
133
/** Encode a single character to string representation.
-
 
134
 *
-
 
135
 * Encode a single character to string representation (i.e. UTF-8) and store
-
 
136
 * it into a buffer at @a offset. Encoding starts at @a offset and this offset
-
 
137
 * is moved to the position where the next character can be written to.
-
 
138
 *
-
 
139
 * @param ch        Input character.
-
 
140
 * @param str       Output buffer.
-
 
141
 * @param offset    Offset (in bytes) where to start writing.
-
 
142
 * @param sz        Size of the output buffer.
-
 
143
 *
-
 
144
 * @return True if the character was encoded successfully or false if there
-
 
145
 *     was not enough space in the output buffer or the character code
-
 
146
 *     was invalid.
-
 
147
 */
-
 
148
bool chr_encode(const wchar_t ch, char *str, size_t *offset, size_t sz)
-
 
149
{
-
 
150
    uint32_t cc;        /* Unsigned version of ch. */
-
 
151
 
-
 
152
    int cbytes;     /* Number of continuation bytes. */
-
 
153
    int b0_bits;        /* Number of data bits in first byte. */
-
 
154
    int i;
-
 
155
 
-
 
156
    if (*offset >= sz)
-
 
157
        return false;
-
 
158
 
-
 
159
    if (ch < 0)
-
 
160
        return false;
-
 
161
 
-
 
162
    /* Bit operations should only be done on unsigned numbers. */
-
 
163
    cc = (uint32_t) ch;
-
 
164
 
-
 
165
    /* Determine how many continuation bytes are needed. */
-
 
166
    if ((cc & ~LO_MASK_32(7)) == 0) {
-
 
167
        b0_bits = 7;
-
 
168
        cbytes = 0;
-
 
169
    } else if ((cc & ~LO_MASK_32(11)) == 0) {
-
 
170
        b0_bits = 5;
-
 
171
        cbytes = 1;
-
 
172
    } else if ((cc & ~LO_MASK_32(16)) == 0) {
-
 
173
        b0_bits = 4;
-
 
174
        cbytes = 2;
-
 
175
    } else if ((cc & ~LO_MASK_32(21)) == 0) {
-
 
176
        b0_bits = 3;
-
 
177
        cbytes = 3;
-
 
178
    } else {
-
 
179
        /* Codes longer than 21 bits are not supported. */
-
 
180
        return false;
-
 
181
    }
-
 
182
 
-
 
183
    /* Check for available space in buffer. */
-
 
184
    if (*offset + cbytes >= sz)
-
 
185
        return false;
-
 
186
 
-
 
187
    /* Encode continuation bytes. */
-
 
188
    for (i = cbytes; i > 0; --i) {
-
 
189
        str[*offset + i] = 0x80 | (cc & LO_MASK_32(CONT_BITS));
-
 
190
        cc = cc >> CONT_BITS;
-
 
191
    }
-
 
192
 
-
 
193
    /* Encode first byte. */
-
 
194
    str[*offset] = (cc & LO_MASK_32(b0_bits)) | HI_MASK_8(8 - b0_bits - 1);
-
 
195
 
-
 
196
    /* Advance offset. */
-
 
197
    *offset += (1 + cbytes);
-
 
198
   
-
 
199
    return true;
-
 
200
}
-
 
201
 
-
 
202
/** Get bytes used by UTF-8 characters.
46
 *
203
 *
-
 
204
 * Get the number of bytes (count of plain characters) which
-
 
205
 * are used by a given count of UTF-8 characters in a string.
-
 
206
 * As UTF-8 encoding is multibyte, there is no constant
-
 
207
 * correspondence between number of characters and used bytes.
-
 
208
 *
-
 
209
 * @param str   UTF-8 string to consider.
-
 
210
 * @param count Number of UTF-8 characters to count.
-
 
211
 *
-
 
212
 * @return Number of bytes used by the characters.
-
 
213
 *
-
 
214
 */
-
 
215
size_t utf8_count_bytes(const char *str, count_t count)
-
 
216
{
-
 
217
    size_t size = 0;
-
 
218
    index_t index = 0;
-
 
219
    index_t iprev;
-
 
220
    wchar_t ch;
-
 
221
   
-
 
222
    while (true) {
-
 
223
        iprev = index;
-
 
224
        if (size >= count)
-
 
225
            break;
-
 
226
        ch = chr_decode(str, &index, UTF8_NO_LIMIT);
-
 
227
        if (ch == '\0') break;
-
 
228
 
-
 
229
        size++;
-
 
230
    }
-
 
231
   
-
 
232
    return iprev;
-
 
233
}
-
 
234
 
-
 
235
/** Check whether character is plain ASCII.
-
 
236
 *
-
 
237
 * @return True if character is plain ASCII.
-
 
238
 *
-
 
239
 */
-
 
240
bool ascii_check(const wchar_t ch)
-
 
241
{
-
 
242
    if ((ch >= 0) && (ch <= 127))
-
 
243
        return true;
-
 
244
   
-
 
245
    return false;
-
 
246
}
-
 
247
 
-
 
248
/** Check whether character is Unicode.
-
 
249
 *
-
 
250
 * @return True if character is valid Unicode code point.
-
 
251
 *
-
 
252
 */
-
 
253
bool unicode_check(const wchar_t ch)
-
 
254
{
-
 
255
    if ((ch >= 0) && (ch <= 1114111))
-
 
256
        return true;
-
 
257
   
-
 
258
    return false;
-
 
259
}
-
 
260
 
-
 
261
/** Return number of plain characters in a string.
-
 
262
 *
47
 * @param str NULL terminated string.
263
 * @param str NULL-terminated string.
48
 *
264
 *
49
 * @return Number of characters in str.
265
 * @return Number of characters in str.
50
 *
266
 *
51
 */
267
 */
52
size_t strlen(const char *str)
268
size_t strlen(const char *str)
53
{
269
{
54
    int i;
270
    size_t size;
-
 
271
    for (size = 0; str[size]; size++);
55
   
272
   
-
 
273
    return size;
-
 
274
}
-
 
275
 
-
 
276
/** Return number of UTF-8 characters in a string.
-
 
277
 *
-
 
278
 * @param str NULL-terminated UTF-8 string.
-
 
279
 *
-
 
280
 * @return Number of UTF-8 characters in str.
-
 
281
 *
-
 
282
 */
-
 
283
size_t strlen_utf8(const char *str)
-
 
284
{
-
 
285
    size_t size = 0;
-
 
286
    index_t index = 0;
-
 
287
   
-
 
288
    while (chr_decode(str, &index, UTF8_NO_LIMIT) != 0) {
-
 
289
        size++;
-
 
290
    }
-
 
291
   
-
 
292
    return size;
-
 
293
}
-
 
294
 
-
 
295
/** Return number of UTF-32 characters in a string.
-
 
296
 *
-
 
297
 * @param str NULL-terminated UTF-32 string.
-
 
298
 *
-
 
299
 * @return Number of UTF-32 characters in str.
-
 
300
 *
-
 
301
 */
-
 
302
size_t strlen_utf32(const wchar_t *str)
-
 
303
{
-
 
304
    size_t size;
56
    for (i = 0; str[i]; i++);
305
    for (size = 0; str[size]; size++);
57
   
306
   
58
    return i;
307
    return size;
59
}
308
}
60
 
309
 
61
/** Compare two NULL terminated strings
310
/** Compare two NULL terminated strings
62
 *
311
 *
63
 * Do a char-by-char comparison of two NULL terminated strings.
312
 * Do a char-by-char comparison of two NULL terminated strings.