Subversion Repositories HelenOS

Rev

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

Rev 4347 Rev 4348
Line 30... Line 30...
30
 * @{
30
 * @{
31
 */
31
 */
32
 
32
 
33
/**
33
/**
34
 * @file
34
 * @file
35
 * @brief Miscellaneous functions.
35
 * @brief String functions.
-
 
36
 *
-
 
37
 * Strings and characters use the Universal Character Set (UCS). The standard
-
 
38
 * strings, called just strings are encoded in UTF-8. Wide strings (encoded
-
 
39
 * in UTF-32) are supported to a limited degree. A single character is
-
 
40
 * represented as wchar_t.@n
-
 
41
 *
-
 
42
 * Overview of the terminology:@n
-
 
43
 *
-
 
44
 *  Term                  Meaning
-
 
45
 *  --------------------  ----------------------------------------------------
-
 
46
 *  byte                  8 bits stored in uint8_t (unsigned 8 bit integer)
-
 
47
 *
-
 
48
 *  character             UTF-32 encoded Unicode character, stored in wchar_t
-
 
49
 *                        (signed 32 bit integer), code points 0 .. 1114111
-
 
50
 *                        are valid
-
 
51
 *
-
 
52
 *  ASCII character       7 bit encoded ASCII character, stored in char
-
 
53
 *                        (usually signed 8 bit integer), code points 0 .. 127
-
 
54
 *                        are valid
-
 
55
 *
-
 
56
 *  string                UTF-8 encoded NULL-terminated Unicode string, char *
-
 
57
 *
-
 
58
 *  wide string           UTF-32 encoded NULL-terminated Unicode string,
-
 
59
 *                        wchar_t *
-
 
60
 *
-
 
61
 *  [wide] string size    number of BYTES in a [wide] string (excluding
-
 
62
 *                        the NULL-terminator), size_t
-
 
63
 *
-
 
64
 *  [wide] string length  number of CHARACTERS in a [wide] string (excluding
-
 
65
 *                        the NULL-terminator), count_t
-
 
66
 *
-
 
67
 *  [wide] string width   number of display cells on a monospace display taken
-
 
68
 *                        by a [wide] string, count_t
-
 
69
 *
-
 
70
 *
-
 
71
 * Overview of string metrics:@n
-
 
72
 *
-
 
73
 *  Metric  Abbrev.  Type     Meaning
-
 
74
 *  ------  ------   ------   -------------------------------------------------
-
 
75
 *  size    n        size_t   number of BYTES in a string (excluding the
-
 
76
 *                            NULL-terminator)
-
 
77
 *
-
 
78
 *  length  l        count_t  number of CHARACTERS in a string (excluding the
-
 
79
 *                            null terminator)
-
 
80
 *
-
 
81
 *  width  w         count_t  number of display cells on a monospace display
-
 
82
 *                            taken by a string
-
 
83
 *
-
 
84
 *
-
 
85
 * Function naming prefixes:@n
-
 
86
 *
-
 
87
 *  chr_    operate on characters
-
 
88
 *  ascii_  operate on ASCII characters
-
 
89
 *  str_    operate on strings
-
 
90
 *  wstr_   operate on wide strings
-
 
91
 *
-
 
92
 *  [w]str_[n|l|w]  operate on a prefix limited by size, length
-
 
93
 *                  or width
-
 
94
 *
-
 
95
 *
-
 
96
 * A specific character inside a [wide] string can be referred to by:@n
-
 
97
 *
-
 
98
 *  pointer (char *, wchar_t *)
-
 
99
 *  byte offset (size_t)
-
 
100
 *  character index (count_t)
-
 
101
 *
36
 */
102
 */
37
 
103
 
38
#include <string.h>
104
#include <string.h>
39
#include <print.h>
105
#include <print.h>
40
#include <cpu.h>
106
#include <cpu.h>
41
#include <arch/asm.h>
107
#include <arch/asm.h>
42
#include <arch.h>
108
#include <arch.h>
43
#include <console/kconsole.h>
109
#include <errno.h>
-
 
110
#include <align.h>
-
 
111
#include <debug.h>
-
 
112
 
-
 
113
/** Byte mask consisting of lowest @n bits (out of 8) */
-
 
114
#define LO_MASK_8(n)  ((uint8_t) ((1 << (n)) - 1))
44
 
115
 
45
char invalch = '?';
116
/** Byte mask consisting of lowest @n bits (out of 32) */
-
 
117
#define LO_MASK_32(n)  ((uint32_t) ((1 << (n)) - 1))
46
 
118
 
-
 
119
/** Byte mask consisting of highest @n bits (out of 8) */
-
 
120
#define HI_MASK_8(n)  (~LO_MASK_8(8 - (n)))
-
 
121
 
-
 
122
/** Number of data bits in a UTF-8 continuation byte */
-
 
123
#define CONT_BITS  6
-
 
124
 
47
/** Decode a single UTF-8 character from a NULL-terminated string.
125
/** Decode a single character from a string.
48
 *
126
 *
49
 * Decode a single UTF-8 character from a plain char NULL-terminated
127
 * Decode a single character from a string of size @a size. Decoding starts
50
 * string. Decoding starts at @index and this index is incremented
128
 * at @a offset and this offset is moved to the beginning of the next
-
 
129
 * character. In case of decoding error, offset generally advances at least
51
 * if the current UTF-8 string is encoded in more than a single byte.
130
 * by one. However, offset is never moved beyond size.
52
 *
131
 *
53
 * @param str   Plain character NULL-terminated string.
132
 * @param str    String (not necessarily NULL-terminated).
54
 * @param index Index (counted in plain characters) where to start
133
 * @param offset Byte offset in string where to start decoding.
55
 *              the decoding.
-
 
56
 * @param limit Maximal allowed value of index.
134
 * @param size   Size of the string (in bytes).
57
 *
135
 *
58
 * @return Decoded character in UTF-32 or '?' if the encoding is wrong.
136
 * @return Value of decoded character, U_SPECIAL on decoding error or
-
 
137
 *         NULL if attempt to decode beyond @a size.
59
 *
138
 *
60
 */
139
 */
61
wchar_t utf8_decode(const char *str, index_t *index, index_t limit)
140
wchar_t str_decode(const char *str, size_t *offset, size_t size)
62
{
141
{
63
    uint8_t c1;           /* First plain character from str */
142
    if (*offset + 1 > size)
64
    uint8_t c2;           /* Second plain character from str */
-
 
65
    uint8_t c3;           /* Third plain character from str */
-
 
66
    uint8_t c4;           /* Fourth plain character from str */
143
        return 0;
67
   
144
   
68
    if (*index > limit)
145
    /* First byte read from string */
69
        return invalch;
146
    uint8_t b0 = (uint8_t) str[(*offset)++];
70
   
147
   
71
    c1 = (uint8_t) str[*index];
148
    /* Determine code length */
72
   
149
   
73
    if ((c1 & 0x80) == 0) {
150
    unsigned int b0_bits;  /* Data bits in first byte */
74
        /* Plain ASCII (code points 0 .. 127) */
151
    unsigned int cbytes;   /* Number of continuation bytes */
75
        return (wchar_t) c1;
-
 
76
    }
-
 
77
   
152
   
78
    if ((c1 & 0xe0) == 0xc0) {
153
    if ((b0 & 0x80) == 0) {
-
 
154
        /* 0xxxxxxx (Plain ASCII) */
-
 
155
        b0_bits = 7;
-
 
156
        cbytes = 0;
-
 
157
    } else if ((b0 & 0xe0) == 0xc0) {
-
 
158
        /* 110xxxxx 10xxxxxx */
-
 
159
        b0_bits = 5;
-
 
160
        cbytes = 1;
-
 
161
    } else if ((b0 & 0xf0) == 0xe0) {
-
 
162
        /* 1110xxxx 10xxxxxx 10xxxxxx */
-
 
163
        b0_bits = 4;
-
 
164
        cbytes = 2;
-
 
165
    } else if ((b0 & 0xf8) == 0xf0) {
-
 
166
        /* 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx */
-
 
167
        b0_bits = 3;
-
 
168
        cbytes = 3;
-
 
169
    } else {
-
 
170
        /* 10xxxxxx -- unexpected continuation byte */
-
 
171
        return U_SPECIAL;
-
 
172
    }
-
 
173
   
-
 
174
    if (*offset + cbytes > size)
-
 
175
        return U_SPECIAL;
-
 
176
   
-
 
177
    wchar_t ch = b0 & LO_MASK_8(b0_bits);
-
 
178
   
79
        /* Code points 128 .. 2047 */
179
    /* Decode continuation bytes */
-
 
180
    while (cbytes > 0) {
-
 
181
        uint8_t b = (uint8_t) str[(*offset)++];
-
 
182
       
-
 
183
        /* Must be 10xxxxxx */
80
        if (*index + 1 > limit)
184
        if ((b & 0xc0) != 0x80)
81
            return invalch;
185
            return U_SPECIAL;
82
       
186
       
83
        c2 = (uint8_t) str[*index + 1];
-
 
84
        if ((c2 & 0xc0) == 0x80) {
187
        /* Shift data bits to ch */
85
            (*index)++;
-
 
86
            return ((wchar_t) ((c1 & 0x1f) << 6) | (c2 & 0x3f));
188
        ch = (ch << CONT_BITS) | (wchar_t) (b & LO_MASK_8(CONT_BITS));
87
        } else
189
        cbytes--;
88
            return invalch;
-
 
89
    }
190
    }
90
   
191
   
91
    if ((c1 & 0xf0) == 0xe0) {
-
 
92
        /* Code points 2048 .. 65535 */
-
 
93
        if (*index + 2 > limit)
-
 
94
            return invalch;
-
 
95
       
-
 
96
        c2 = (uint8_t) str[*index + 1];
-
 
97
        if ((c2 & 0xc0) == 0x80) {
-
 
98
            (*index)++;
-
 
99
            c3 = (uint8_t) str[*index + 1];
-
 
100
            if ((c3 & 0xc0) == 0x80) {
-
 
101
                (*index)++;
-
 
102
                return ((wchar_t) ((c1 & 0x0f) << 12) | ((c2 & 0x3f) << 6) | (c3 & 0x3f));
-
 
103
            } else
-
 
104
                return invalch;
-
 
105
        } else
-
 
106
            return invalch;
-
 
107
    }
-
 
108
   
-
 
109
    if ((c1 & 0xf8) == 0xf0) {
-
 
110
        /* Code points 65536 .. 1114111 */
-
 
111
        if (*index + 3 > limit)
-
 
112
            return invalch;
-
 
113
       
-
 
114
        c2 = (uint8_t) str[*index + 1];
-
 
115
        if ((c2 & 0xc0) == 0x80) {
-
 
116
            (*index)++;
-
 
117
            c3 = (uint8_t) str[*index + 1];
-
 
118
            if ((c3 & 0xc0) == 0x80) {
-
 
119
                (*index)++;
-
 
120
                c4 = (uint8_t) str[*index + 1];
-
 
121
                if ((c4 & 0xc0) == 0x80) {
-
 
122
                    (*index)++;
-
 
123
                    return ((wchar_t) ((c1 & 0x07) << 18) | ((c2 & 0x3f) << 12) | ((c3 & 0x3f) << 6) | (c4 & 0x3f));
-
 
124
                } else
-
 
125
                    return invalch;
-
 
126
            } else
-
 
127
                return invalch;
-
 
128
        } else
-
 
129
            return invalch;
-
 
130
    }
-
 
131
   
-
 
132
    return invalch;
192
    return ch;
133
}
193
}
134
 
194
 
135
/** Encode a single UTF-32 character as UTF-8
-
 
136
 *
-
 
137
 * Encode a single UTF-32 character as UTF-8 and store it into
195
/** Encode a single character to string representation.
138
 * the given buffer at @index. Encoding starts at @index and
-
 
139
 * this index is incremented if the UTF-8 character takes
-
 
140
 * more than a single byte.
-
 
141
 *
-
 
142
 * @param ch    Input UTF-32 character.
-
 
143
 * @param str   Output buffer.
-
 
144
 * @param index Index (counted in plain characters) where to start
-
 
145
 *              the encoding
-
 
146
 * @param limit Maximal allowed value of index.
-
 
147
 *
-
 
148
 * @return True if the character was encoded or false if there is not
-
 
149
 *         enought space in the output buffer or the character is invalid
-
 
150
 *         Unicode code point.
-
 
151
 *
196
 *
-
 
197
 * Encode a single character to string representation (i.e. UTF-8) and store
-
 
198
 * it into a buffer at @a offset. Encoding starts at @a offset and this offset
-
 
199
 * is moved to the position where the next character can be written to.
-
 
200
 *
-
 
201
 * @param ch     Input character.
-
 
202
 * @param str    Output buffer.
-
 
203
 * @param offset Byte offset where to start writing.
-
 
204
 * @param size   Size of the output buffer (in bytes).
-
 
205
 *
-
 
206
 * @return EOK if the character was encoded successfully, EOVERFLOW if there
-
 
207
 *     was not enough space in the output buffer or EINVAL if the character
-
 
208
 *     code was invalid.
152
 */
209
 */
153
bool utf8_encode(const wchar_t ch, char *str, index_t *index, index_t limit)
210
int chr_encode(wchar_t ch, char *str, size_t *offset, size_t size)
154
{
211
{
155
    if (*index > limit)
212
    if (*offset >= size)
156
        return false;
213
        return EOVERFLOW;
157
   
214
   
158
    if ((ch >= 0) && (ch <= 127)) {
215
    if (!chr_check(ch))
159
        /* Plain ASCII (code points 0 .. 127) */
-
 
160
        str[*index] = ch & 0x7f;
-
 
161
        return true;
216
        return EINVAL;
162
    }
-
 
163
   
217
   
164
    if ((ch >= 128) && (ch <= 2047)) {
218
    /* Unsigned version of ch (bit operations should only be done
165
        /* Code points 128 .. 2047 */
219
       on unsigned types). */
166
        if (*index + 1 > limit)
220
    uint32_t cc = (uint32_t) ch;
167
            return false;
-
 
168
       
-
 
169
        str[*index] = 0xc0 | ((ch >> 6) & 0x1f);
-
 
170
        (*index)++;
-
 
171
        str[*index] = 0x80 | (ch & 0x3f);
-
 
172
        return true;
-
 
173
    }
-
 
174
   
221
   
-
 
222
    /* Determine how many continuation bytes are needed */
-
 
223
   
-
 
224
    unsigned int b0_bits;  /* Data bits in first byte */
-
 
225
    unsigned int cbytes;   /* Number of continuation bytes */
-
 
226
   
175
    if ((ch >= 2048) && (ch <= 65535)) {
227
    if ((cc & ~LO_MASK_32(7)) == 0) {
176
        /* Code points 2048 .. 65535 */
228
        b0_bits = 7;
177
        if (*index + 2 > limit)
229
        cbytes = 0;
-
 
230
    } else if ((cc & ~LO_MASK_32(11)) == 0) {
178
            return false;
231
        b0_bits = 5;
179
       
232
        cbytes = 1;
180
        str[*index] = 0xe0 | ((ch >> 12) & 0x0f);
233
    } else if ((cc & ~LO_MASK_32(16)) == 0) {
-
 
234
        b0_bits = 4;
181
        (*index)++;
235
        cbytes = 2;
182
        str[*index] = 0x80 | ((ch >> 6) & 0x3f);
236
    } else if ((cc & ~LO_MASK_32(21)) == 0) {
-
 
237
        b0_bits = 3;
183
        (*index)++;
238
        cbytes = 3;
-
 
239
    } else {
184
        str[*index] = 0x80 | (ch & 0x3f);
240
        /* Codes longer than 21 bits are not supported */
185
        return true;
241
        return EINVAL;
186
    }
242
    }
187
   
243
   
188
    if ((ch >= 65536) && (ch <= 1114111)) {
-
 
189
        /* Code points 65536 .. 1114111 */
244
    /* Check for available space in buffer */
190
        if (*index + 3 > limit)
245
    if (*offset + cbytes >= size)
191
            return false;
246
        return EOVERFLOW;
192
       
247
   
193
        str[*index] = 0xf0 | ((ch >> 18) & 0x07);
-
 
194
        (*index)++;
-
 
195
        str[*index] = 0x80 | ((ch >> 12) & 0x3f);
248
    /* Encode continuation bytes */
196
        (*index)++;
249
    unsigned int i;
197
        str[*index] = 0x80 | ((ch >> 6) & 0x3f);
250
    for (i = cbytes; i > 0; i--) {
198
        (*index)++;
-
 
199
        str[*index] = 0x80 | (ch & 0x3f);
251
        str[*offset + i] = 0x80 | (cc & LO_MASK_32(CONT_BITS));
200
        return true;
252
        cc = cc >> CONT_BITS;
201
    }
253
    }
202
   
254
   
-
 
255
    /* Encode first byte */
-
 
256
    str[*offset] = (cc & LO_MASK_32(b0_bits)) | HI_MASK_8(8 - b0_bits - 1);
-
 
257
   
-
 
258
    /* Advance offset */
-
 
259
    *offset += cbytes + 1;
-
 
260
   
203
    return false;
261
    return EOK;
204
}
262
}
205
 
263
 
206
/** Get bytes used by UTF-8 characters.
264
/** Get size of string.
207
 *
265
 *
208
 * Get the number of bytes (count of plain characters) which
266
 * Get the number of bytes which are used by the string @a str (excluding the
209
 * are used by a given count of UTF-8 characters in a string.
-
 
210
 * As UTF-8 encoding is multibyte, there is no constant
267
 * NULL-terminator).
211
 * correspondence between number of characters and used bytes.
-
 
212
 *
268
 *
213
 * @param str   UTF-8 string to consider.
269
 * @param str String to consider.
214
 * @param count Number of UTF-8 characters to count.
-
 
215
 *
270
 *
216
 * @return Number of bytes used by the characters.
271
 * @return Number of bytes used by the string
217
 *
272
 *
218
 */
273
 */
219
size_t utf8_count_bytes(const char *str, count_t count)
274
size_t str_size(const char *str)
220
{
275
{
221
    size_t size = 0;
276
    size_t size = 0;
222
    index_t index = 0;
-
 
223
   
277
   
224
    while ((utf8_decode(str, &index, UTF8_NO_LIMIT) != 0) && (size < count)) {
278
    while (*str++ != 0)
225
        size++;
279
        size++;
-
 
280
   
-
 
281
    return size;
-
 
282
}
-
 
283
 
-
 
284
/** Get size of wide string.
-
 
285
 *
-
 
286
 * Get the number of bytes which are used by the wide string @a str (excluding the
-
 
287
 * NULL-terminator).
-
 
288
 *
-
 
289
 * @param str Wide string to consider.
-
 
290
 *
-
 
291
 * @return Number of bytes used by the wide string
-
 
292
 *
-
 
293
 */
-
 
294
size_t wstr_size(const wchar_t *str)
-
 
295
{
-
 
296
    return (wstr_length(str) * sizeof(wchar_t));
-
 
297
}
-
 
298
 
-
 
299
/** Get size of string with length limit.
-
 
300
 *
-
 
301
 * Get the number of bytes which are used by up to @a max_len first
-
 
302
 * characters in the string @a str. If @a max_len is greater than
-
 
303
 * the length of @a str, the entire string is measured (excluding the
-
 
304
 * NULL-terminator).
-
 
305
 *
-
 
306
 * @param str     String to consider.
-
 
307
 * @param max_len Maximum number of characters to measure.
-
 
308
 *
-
 
309
 * @return Number of bytes used by the characters.
-
 
310
 *
-
 
311
 */
-
 
312
size_t str_lsize(const char *str, count_t max_len)
-
 
313
{
-
 
314
    count_t len = 0;
-
 
315
    size_t offset = 0;
-
 
316
   
-
 
317
    while (len < max_len) {
-
 
318
        if (str_decode(str, &offset, STR_NO_LIMIT) == 0)
-
 
319
            break;
-
 
320
       
226
        index++;
321
        len++;
227
    }
322
    }
228
   
323
   
229
    return index;
324
    return offset;
230
}
325
}
231
 
326
 
232
/** Check whether character is plain ASCII.
327
/** Get size of wide string with length limit.
233
 *
328
 *
-
 
329
 * Get the number of bytes which are used by up to @a max_len first
-
 
330
 * wide characters in the wide string @a str. If @a max_len is greater than
-
 
331
 * the length of @a str, the entire wide string is measured (excluding the
-
 
332
 * NULL-terminator).
-
 
333
 *
-
 
334
 * @param str     Wide string to consider.
-
 
335
 * @param max_len Maximum number of wide characters to measure.
-
 
336
 *
234
 * @return True if character is plain ASCII.
337
 * @return Number of bytes used by the wide characters.
235
 *
338
 *
236
 */
339
 */
237
bool ascii_check(const wchar_t ch)
340
size_t wstr_lsize(const wchar_t *str, count_t max_len)
238
{
341
{
-
 
342
    return (wstr_nlength(str, max_len * sizeof(wchar_t)) * sizeof(wchar_t));
-
 
343
}
-
 
344
 
-
 
345
/** Get number of characters in a string.
-
 
346
 *
-
 
347
 * @param str NULL-terminated string.
-
 
348
 *
-
 
349
 * @return Number of characters in string.
-
 
350
 *
-
 
351
 */
239
    if ((ch >= 0) && (ch <= 127))
352
count_t str_length(const char *str)
-
 
353
{
240
        return true;
354
    count_t len = 0;
-
 
355
    size_t offset = 0;
241
   
356
   
-
 
357
    while (str_decode(str, &offset, STR_NO_LIMIT) != 0)
-
 
358
        len++;
-
 
359
   
242
    return false;
360
    return len;
243
}
361
}
244
 
362
 
245
/** Check whether character is Unicode.
363
/** Get number of characters in a wide string.
246
 *
364
 *
-
 
365
 * @param str NULL-terminated wide string.
-
 
366
 *
247
 * @return True if character is valid Unicode code point.
367
 * @return Number of characters in @a str.
248
 *
368
 *
249
 */
369
 */
250
bool unicode_check(const wchar_t ch)
370
count_t wstr_length(const wchar_t *wstr)
251
{
371
{
252
    if ((ch >= 0) && (ch <= 1114111))
-
 
253
        return true;
372
    count_t len = 0;
254
   
373
   
-
 
374
    while (*wstr++ != 0)
-
 
375
        len++;
-
 
376
   
255
    return false;
377
    return len;
256
}
378
}
257
 
379
 
258
/** Return number of plain characters in a string.
380
/** Get number of characters in a string with size limit.
259
 *
381
 *
260
 * @param str NULL-terminated string.
382
 * @param str  NULL-terminated string.
-
 
383
 * @param size Maximum number of bytes to consider.
261
 *
384
 *
262
 * @return Number of characters in str.
385
 * @return Number of characters in string.
263
 *
386
 *
264
 */
387
 */
265
size_t strlen(const char *str)
388
count_t str_nlength(const char *str, size_t size)
266
{
389
{
267
    size_t size;
390
    count_t len = 0;
268
    for (size = 0; str[size]; size++);
391
    size_t offset = 0;
269
   
392
   
-
 
393
    while (str_decode(str, &offset, size) != 0)
-
 
394
        len++;
-
 
395
   
270
    return size;
396
    return len;
271
}
397
}
272
 
398
 
273
/** Return number of UTF-8 characters in a string.
399
/** Get number of characters in a string with size limit.
274
 *
400
 *
275
 * @param str NULL-terminated UTF-8 string.
401
 * @param str  NULL-terminated string.
-
 
402
 * @param size Maximum number of bytes to consider.
276
 *
403
 *
277
 * @return Number of UTF-8 characters in str.
404
 * @return Number of characters in string.
278
 *
405
 *
279
 */
406
 */
280
size_t strlen_utf8(const char *str)
407
count_t wstr_nlength(const wchar_t *str, size_t size)
281
{
408
{
282
    size_t size = 0;
409
    count_t len = 0;
-
 
410
    count_t limit = ALIGN_DOWN(size, sizeof(wchar_t));
283
    index_t index = 0;
411
    count_t offset = 0;
284
   
412
   
285
    while (utf8_decode(str, &index, UTF8_NO_LIMIT) != 0) {
413
    while ((offset < limit) && (*str++ != 0)) {
286
        size++;
414
        len++;
287
        index++;
415
        offset += sizeof(wchar_t);
288
    }
416
    }
289
   
417
   
290
    return size;
418
    return len;
291
}
419
}
292
 
420
 
-
 
421
/** Check whether character is plain ASCII.
-
 
422
 *
293
/** Return number of UTF-32 characters in a string.
423
 * @return True if character is plain ASCII.
294
 *
424
 *
-
 
425
 */
-
 
426
bool ascii_check(wchar_t ch)
-
 
427
{
-
 
428
    if ((ch >= 0) && (ch <= 127))
-
 
429
        return true;
-
 
430
   
-
 
431
    return false;
-
 
432
}
-
 
433
 
295
 * @param str NULL-terminated UTF-32 string.
434
/** Check whether character is valid
296
 *
435
 *
297
 * @return Number of UTF-32 characters in str.
436
 * @return True if character is a valid Unicode code point.
298
 *
437
 *
299
 */
438
 */
300
size_t strlen_utf32(const wchar_t *str)
439
bool chr_check(wchar_t ch)
301
{
440
{
-
 
441
    if ((ch >= 0) && (ch <= 1114111))
302
    size_t size;
442
        return true;
303
    for (size = 0; str[size]; size++);
-
 
304
   
443
   
305
    return size;
444
    return false;
306
}
445
}
307
 
446
 
308
/** Compare two NULL terminated strings
447
/** Compare two NULL terminated strings.
309
 *
448
 *
310
 * Do a char-by-char comparison of two NULL terminated strings.
449
 * Do a char-by-char comparison of two NULL-terminated strings.
311
 * The strings are considered equal iff they consist of the same
450
 * The strings are considered equal iff they consist of the same
312
 * characters on the minimum of their lengths.
451
 * characters on the minimum of their lengths.
313
 *
452
 *
314
 * @param src First string to compare.
453
 * @param s1 First string to compare.
315
 * @param dst Second string to compare.
454
 * @param s2 Second string to compare.
316
 *
455
 *
317
 * @return 0 if the strings are equal, -1 if first is smaller, 1 if second smaller.
456
 * @return 0 if the strings are equal, -1 if first is smaller,
-
 
457
 *         1 if second smaller.
318
 *
458
 *
319
 */
459
 */
320
int strcmp(const char *src, const char *dst)
460
int str_cmp(const char *s1, const char *s2)
321
{
461
{
-
 
462
    wchar_t c1 = 0;
-
 
463
    wchar_t c2 = 0;
-
 
464
   
-
 
465
    size_t off1 = 0;
-
 
466
    size_t off2 = 0;
-
 
467
 
-
 
468
    while (true) {
-
 
469
        c1 = str_decode(s1, &off1, STR_NO_LIMIT);
322
    for (; *src && *dst; src++, dst++) {
470
        c2 = str_decode(s2, &off2, STR_NO_LIMIT);
-
 
471
 
323
        if (*src < *dst)
472
        if (c1 < c2)
324
            return -1;
473
            return -1;
-
 
474
       
325
        if (*src > *dst)
475
        if (c1 > c2)
326
            return 1;
476
            return 1;
-
 
477
 
-
 
478
        if (c1 == 0 || c2 == 0)
-
 
479
            break;     
327
    }
480
    }
328
    if (*src == *dst)
-
 
329
        return 0;
-
 
330
   
-
 
331
    if (!*src)
-
 
332
        return -1;
-
 
333
   
-
 
334
    return 1;
-
 
335
}
-
 
336
 
481
 
-
 
482
    return 0;
-
 
483
}
337
 
484
 
338
/** Compare two NULL terminated strings
485
/** Compare two NULL terminated strings with length limit.
339
 *
486
 *
340
 * Do a char-by-char comparison of two NULL terminated strings.
487
 * Do a char-by-char comparison of two NULL-terminated strings.
341
 * The strings are considered equal iff they consist of the same
488
 * The strings are considered equal iff they consist of the same
342
 * characters on the minimum of their lengths and specified maximal
489
 * characters on the minimum of their lengths and the length limit.
343
 * length.
-
 
344
 *
490
 *
345
 * @param src First string to compare.
491
 * @param s1      First string to compare.
346
 * @param dst Second string to compare.
492
 * @param s2      Second string to compare.
347
 * @param len Maximal length for comparison.
493
 * @param max_len Maximum number of characters to consider.
348
 *
494
 *
349
 * @return 0 if the strings are equal, -1 if first is smaller, 1 if second smaller.
495
 * @return 0 if the strings are equal, -1 if first is smaller,
-
 
496
 *         1 if second smaller.
350
 *
497
 *
351
 */
498
 */
352
int strncmp(const char *src, const char *dst, size_t len)
499
int str_lcmp(const char *s1, const char *s2, count_t max_len)
353
{
500
{
-
 
501
    wchar_t c1 = 0;
-
 
502
    wchar_t c2 = 0;
-
 
503
   
354
    unsigned int i;
504
    size_t off1 = 0;
-
 
505
    size_t off2 = 0;
355
   
506
   
-
 
507
    count_t len = 0;
-
 
508
 
-
 
509
    while (true) {
-
 
510
        if (len >= max_len)
-
 
511
            break;
-
 
512
 
-
 
513
        c1 = str_decode(s1, &off1, STR_NO_LIMIT);
356
    for (i = 0; (*src) && (*dst) && (i < len); src++, dst++, i++) {
514
        c2 = str_decode(s2, &off2, STR_NO_LIMIT);
-
 
515
 
357
        if (*src < *dst)
516
        if (c1 < c2)
358
            return -1;
517
            return -1;
359
       
518
 
360
        if (*src > *dst)
519
        if (c1 > c2)
361
            return 1;
520
            return 1;
-
 
521
 
-
 
522
        if (c1 == 0 || c2 == 0)
-
 
523
            break;
-
 
524
 
-
 
525
        ++len; 
362
    }
526
    }
-
 
527
 
-
 
528
    return 0;
-
 
529
 
-
 
530
}
-
 
531
 
-
 
532
/** Copy string.
-
 
533
 *
-
 
534
 * Copy source string @a src to destination buffer @a dest.
-
 
535
 * No more than @a size bytes are written. If the size of the output buffer
-
 
536
 * is at least one byte, the output string will always be well-formed, i.e.
-
 
537
 * null-terminated and containing only complete characters.
-
 
538
 *
-
 
539
 * @param dst   Destination buffer.
-
 
540
 * @param count Size of the destination buffer (must be > 0).
-
 
541
 * @param src   Source string.
-
 
542
 */
-
 
543
void str_cpy(char *dest, size_t size, const char *src)
-
 
544
{
-
 
545
    wchar_t ch;
-
 
546
    size_t src_off;
-
 
547
    size_t dest_off;
-
 
548
 
-
 
549
    /* There must be space for a null terminator in the buffer. */
-
 
550
    ASSERT(size > 0);
363
   
551
   
364
    if (i == len || *src == *dst)
552
    src_off = 0;
365
        return 0;
553
    dest_off = 0;
366
   
554
 
-
 
555
    while ((ch = str_decode(src, &src_off, STR_NO_LIMIT)) != 0) {
367
    if (!*src)
556
        if (chr_encode(ch, dest, &dest_off, size - 1) != EOK)
368
        return -1;
557
            break;
369
   
558
    }
-
 
559
 
370
    return 1;
560
    dest[dest_off] = '\0';
371
}
561
}
372
 
562
 
-
 
563
/** Copy size-limited substring.
-
 
564
 *
-
 
565
 * Copy prefix of string @a src of max. size @a size to destination buffer
-
 
566
 * @a dest. No more than @a size bytes are written. The output string will
-
 
567
 * always be well-formed, i.e. null-terminated and containing only complete
-
 
568
 * characters.
-
 
569
 *
-
 
570
 * No more than @a n bytes are read from the input string, so it does not
-
 
571
 * have to be null-terminated.
-
 
572
 *
-
 
573
 * @param dst   Destination buffer.
-
 
574
 * @param count Size of the destination buffer (must be > 0).
-
 
575
 * @param src   Source string.
-
 
576
 * @param n Maximum number of bytes to read from @a src.
-
 
577
 */
-
 
578
void str_ncpy(char *dest, size_t size, const char *src, size_t n)
-
 
579
{
-
 
580
    wchar_t ch;
-
 
581
    size_t src_off;
-
 
582
    size_t dest_off;
-
 
583
 
-
 
584
    /* There must be space for a null terminator in the buffer. */
-
 
585
    ASSERT(size > 0);
-
 
586
   
-
 
587
    src_off = 0;
-
 
588
    dest_off = 0;
-
 
589
 
-
 
590
    while ((ch = str_decode(src, &src_off, n)) != 0) {
-
 
591
        if (chr_encode(ch, dest, &dest_off, size - 1) != EOK)
-
 
592
            break;
-
 
593
    }
373
 
594
 
-
 
595
    dest[dest_off] = '\0';
-
 
596
}
374
 
597
 
375
/** Copy NULL terminated string.
598
/** Copy NULL-terminated wide string to string
376
 *
599
 *
377
 * Copy at most 'len' characters from string 'src' to 'dest'.
600
 * Copy source wide string @a src to destination buffer @a dst.
-
 
601
 * No more than @a size bytes are written. NULL-terminator is always
378
 * If 'src' is shorter than 'len', '\0' is inserted behind the
602
 * written after the last succesfully copied character (i.e. if the
-
 
603
 * destination buffer is has at least 1 byte, it will be always
379
 * last copied character.
604
 * NULL-terminated).
380
 *
605
 *
381
 * @param src  Source string.
606
 * @param src   Source wide string.
382
 * @param dest Destination buffer.
607
 * @param dst   Destination buffer.
383
 * @param len  Size of destination buffer.
608
 * @param count Size of the destination buffer.
384
 *
609
 *
385
 */
610
 */
386
void strncpy(char *dest, const char *src, size_t len)
611
void wstr_nstr(char *dst, const wchar_t *src, size_t size)
387
{
612
{
-
 
613
    /* No space for the NULL-terminator in the buffer */
388
    unsigned int i;
614
    if (size == 0)
-
 
615
        return;
-
 
616
   
-
 
617
    wchar_t ch;
-
 
618
    count_t src_idx = 0;
-
 
619
    size_t dst_off = 0;
389
   
620
   
390
    for (i = 0; i < len; i++) {
621
    while ((ch = src[src_idx++]) != 0) {
391
        if (!(dest[i] = src[i]))
622
        if (chr_encode(ch, dst, &dst_off, size) != EOK)
392
            return;
623
            break;
393
    }
624
    }
394
   
625
   
-
 
626
    if (dst_off >= size)
395
    dest[i - 1] = '\0';
627
        dst[size - 1] = 0;
-
 
628
    else
-
 
629
        dst[dst_off] = 0;
396
}
630
}
397
 
631
 
398
/** Find first occurence of character in string.
632
/** Find first occurence of character in string.
399
 *
633
 *
400
 * @param s String to search.
634
 * @param str String to search.
401
 * @param i Character to look for.
635
 * @param ch  Character to look for.
-
 
636
 *
-
 
637
 * @return Pointer to character in @a str or NULL if not found.
402
 *
638
 *
403
 * @return Pointer to character in @a s or NULL if not found.
-
 
404
 */
639
 */
405
extern char *strchr(const char *s, int i)
640
const char *str_chr(const char *str, wchar_t ch)
406
{
641
{
-
 
642
    wchar_t acc;
-
 
643
    size_t off = 0;
407
    while (*s != '\0') {
644
    size_t last = 0;
-
 
645
   
-
 
646
    while ((acc = str_decode(str, &off, STR_NO_LIMIT)) != 0) {
408
        if (*s == i)
647
        if (acc == ch)
409
            return (char *) s;
648
            return (str + last);
410
        ++s;
649
        last = off;
411
    }
650
    }
412
   
651
   
413
    return NULL;
652
    return NULL;
414
}
653
}
415
 
654
 
-
 
655
/** Insert a wide character into a wide string.
-
 
656
 *
-
 
657
 * Insert a wide character into a wide string at position
-
 
658
 * @a pos. The characters after the position are shifted.
-
 
659
 *
-
 
660
 * @param str     String to insert to.
-
 
661
 * @param ch      Character to insert to.
-
 
662
 * @param pos     Character index where to insert.
-
 
663
 @ @param max_pos Characters in the buffer.
-
 
664
 *
-
 
665
 * @return True if the insertion was sucessful, false if the position
-
 
666
 *         is out of bounds.
-
 
667
 *
-
 
668
 */
-
 
669
bool wstr_linsert(wchar_t *str, wchar_t ch, count_t pos, count_t max_pos)
-
 
670
{
-
 
671
    count_t len = wstr_length(str);
-
 
672
   
-
 
673
    if ((pos > len) || (pos + 1 > max_pos))
-
 
674
        return false;
-
 
675
   
-
 
676
    count_t i;
-
 
677
    for (i = len; i + 1 > pos; i--)
-
 
678
        str[i + 1] = str[i];
-
 
679
   
-
 
680
    str[pos] = ch;
-
 
681
   
-
 
682
    return true;
-
 
683
}
-
 
684
 
-
 
685
/** Remove a wide character from a wide string.
-
 
686
 *
-
 
687
 * Remove a wide character from a wide string at position
-
 
688
 * @a pos. The characters after the position are shifted.
-
 
689
 *
-
 
690
 * @param str String to remove from.
-
 
691
 * @param pos Character index to remove.
-
 
692
 *
-
 
693
 * @return True if the removal was sucessful, false if the position
-
 
694
 *         is out of bounds.
-
 
695
 *
-
 
696
 */
-
 
697
bool wstr_remove(wchar_t *str, count_t pos)
-
 
698
{
-
 
699
    count_t len = wstr_length(str);
-
 
700
   
-
 
701
    if (pos >= len)
-
 
702
        return false;
-
 
703
   
-
 
704
    count_t i;
-
 
705
    for (i = pos + 1; i <= len; i++)
-
 
706
        str[i - 1] = str[i];
-
 
707
   
-
 
708
    return true;
-
 
709
}
-
 
710
 
416
/** @}
711
/** @}
417
 */
712
 */