Subversion Repositories HelenOS

Rev

Rev 4212 | Rev 4224 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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