Subversion Repositories HelenOS

Rev

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

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