Subversion Repositories HelenOS

Rev

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

Rev 4208 Rev 4209
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 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.
-
 
41
 *
-
 
42
 * Strings have the following metrics:
-
 
43
 *
-
 
44
 *  Metric  Abbrev. Meaning
-
 
45
 *  ------  ------  -------
-
 
46
 *  size    n   Number of bytes the string is encoded into, excluding
-
 
47
 *          the null terminator.
-
 
48
 *  length  l   The number of characters in the string, excluding
-
 
49
 *          the null terminator.
-
 
50
 *  width   w   The number of character cells the string takes up on a
-
 
51
 *          monospace display.
-
 
52
 *
-
 
53
 * Naming scheme:
-
 
54
 *
-
 
55
 *  chr_xxx     operate on characters
-
 
56
 *  str_xxx     operate on strings
-
 
57
 *  wstr_xxx    operate on wide strings
-
 
58
 *
-
 
59
 *  [w]str_[n|l|w]xxx   operate on a prefix limited by size, length
-
 
60
 *              or width.
36
 */
61
 */
37
 
62
 
38
#include <string.h>
63
#include <string.h>
39
#include <print.h>
64
#include <print.h>
40
#include <cpu.h>
65
#include <cpu.h>
41
#include <arch/asm.h>
66
#include <arch/asm.h>
42
#include <arch.h>
67
#include <arch.h>
43
#include <errno.h>
68
#include <errno.h>
44
#include <console/kconsole.h>
69
#include <console/kconsole.h>
45
 
70
 
46
char invalch = '?';
71
char invalch = '?';
47
 
72
 
48
/** Byte mask consisting of lowest @n bits (out of eight). */
73
/** Byte mask consisting of lowest @n bits (out of eight). */
49
#define LO_MASK_8(n) ((uint8_t)((1 << (n)) - 1))
74
#define LO_MASK_8(n) ((uint8_t)((1 << (n)) - 1))
50
 
75
 
51
/** Byte mask consisting of lowest @n bits (out of 32). */
76
/** Byte mask consisting of lowest @n bits (out of 32). */
52
#define LO_MASK_32(n) ((uint32_t)((1 << (n)) - 1))
77
#define LO_MASK_32(n) ((uint32_t)((1 << (n)) - 1))
53
 
78
 
54
/** Byte mask consisting of highest @n bits (out of eight). */
79
/** Byte mask consisting of highest @n bits (out of eight). */
55
#define HI_MASK_8(n) (~LO_MASK_8(8 - (n)))
80
#define HI_MASK_8(n) (~LO_MASK_8(8 - (n)))
56
 
81
 
57
/** Number of data bits in a UTF-8 continuation byte. */
82
/** Number of data bits in a UTF-8 continuation byte. */
58
#define CONT_BITS 6
83
#define CONT_BITS 6
59
 
84
 
60
/** Decode a single character from a substring.
85
/** Decode a single character from a substring.
61
 *
86
 *
62
 * Decode a single character from a substring of size @a sz. Decoding starts
87
 * Decode a single character from a substring of size @a sz. Decoding starts
63
 * at @a offset and this offset is moved to the beginning of the next
88
 * at @a offset and this offset is moved to the beginning of the next
64
 * character. In case of decoding error, offset generally advances at least
89
 * character. In case of decoding error, offset generally advances at least
65
 * by one. However, offset is never moved beyond (str + sz).
90
 * by one. However, offset is never moved beyond (str + sz).
66
 *
91
 *
67
 * @param str   String (not necessarily NULL-terminated).
92
 * @param str   String (not necessarily NULL-terminated).
68
 * @param index Index (counted in plain characters) where to start
93
 * @param index Index (counted in plain characters) where to start
69
 *              the decoding.
94
 *              the decoding.
70
 * @param limit Size of the substring.
95
 * @param limit Size of the substring.
71
 *
96
 *
72
 * @return  Value of decoded character or '?' on decoding error.
97
 * @return  Value of decoded character or '?' on decoding error.
73
 */
98
 */
74
wchar_t chr_decode(const char *str, size_t *offset, size_t sz)
99
wchar_t chr_decode(const char *str, size_t *offset, size_t sz)
75
{
100
{
76
    uint8_t b0, b;          /* Bytes read from str. */
101
    uint8_t b0, b;          /* Bytes read from str. */
77
    wchar_t ch;
102
    wchar_t ch;
78
 
103
 
79
    int b0_bits;        /* Data bits in first byte. */
104
    int b0_bits;        /* Data bits in first byte. */
80
    int cbytes;     /* Number of continuation bytes. */
105
    int cbytes;     /* Number of continuation bytes. */
81
 
106
 
82
    if (*offset + 1 > sz)
107
    if (*offset + 1 > sz)
83
        return invalch;
108
        return invalch;
84
 
109
 
85
    b0 = (uint8_t) str[(*offset)++];
110
    b0 = (uint8_t) str[(*offset)++];
86
 
111
 
87
    /* Determine code length. */
112
    /* Determine code length. */
88
 
113
 
89
    if ((b0 & 0x80) == 0) {
114
    if ((b0 & 0x80) == 0) {
90
        /* 0xxxxxxx (Plain ASCII) */
115
        /* 0xxxxxxx (Plain ASCII) */
91
        b0_bits = 7;
116
        b0_bits = 7;
92
        cbytes = 0;
117
        cbytes = 0;
93
    } else if ((b0 & 0xe0) == 0xc0) {
118
    } else if ((b0 & 0xe0) == 0xc0) {
94
        /* 110xxxxx 10xxxxxx */
119
        /* 110xxxxx 10xxxxxx */
95
        b0_bits = 5;
120
        b0_bits = 5;
96
        cbytes = 1;
121
        cbytes = 1;
97
    } else if ((b0 & 0xf0) == 0xe0) {
122
    } else if ((b0 & 0xf0) == 0xe0) {
98
        /* 1110xxxx 10xxxxxx 10xxxxxx */
123
        /* 1110xxxx 10xxxxxx 10xxxxxx */
99
        b0_bits = 4;
124
        b0_bits = 4;
100
        cbytes = 2;
125
        cbytes = 2;
101
    } else if ((b0 & 0xf8) == 0xf0) {
126
    } else if ((b0 & 0xf8) == 0xf0) {
102
        /* 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx */
127
        /* 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx */
103
        b0_bits = 3;
128
        b0_bits = 3;
104
        cbytes = 3;
129
        cbytes = 3;
105
    } else {
130
    } else {
106
        /* 10xxxxxx -- unexpected continuation byte. */
131
        /* 10xxxxxx -- unexpected continuation byte. */
107
        return invalch;
132
        return invalch;
108
    }
133
    }
109
 
134
 
110
    if (*offset + cbytes > sz) {
135
    if (*offset + cbytes > sz) {
111
        return invalch;
136
        return invalch;
112
    }
137
    }
113
 
138
 
114
    ch = b0 & LO_MASK_8(b0_bits);
139
    ch = b0 & LO_MASK_8(b0_bits);
115
 
140
 
116
    /* Decode continuation bytes. */
141
    /* Decode continuation bytes. */
117
    while (cbytes > 0) {
142
    while (cbytes > 0) {
118
        b = (uint8_t) str[(*offset)++];
143
        b = (uint8_t) str[(*offset)++];
119
 
144
 
120
        /* Must be 10xxxxxx. */
145
        /* Must be 10xxxxxx. */
121
        if ((b & 0xc0) != 0x80) {
146
        if ((b & 0xc0) != 0x80) {
122
            return invalch;
147
            return invalch;
123
        }
148
        }
124
 
149
 
125
        /* Shift data bits to ch. */
150
        /* Shift data bits to ch. */
126
        ch = (ch << CONT_BITS) | (wchar_t) (b & LO_MASK_8(CONT_BITS));
151
        ch = (ch << CONT_BITS) | (wchar_t) (b & LO_MASK_8(CONT_BITS));
127
        --cbytes;
152
        --cbytes;
128
    }
153
    }
129
 
154
 
130
    return ch;
155
    return ch;
131
}
156
}
132
 
157
 
133
/** Encode a single character to string representation.
158
/** Encode a single character to string representation.
134
 *
159
 *
135
 * Encode a single character to string representation (i.e. UTF-8) and store
160
 * Encode a single character to string representation (i.e. UTF-8) and store
136
 * it into a buffer at @a offset. Encoding starts at @a offset and this offset
161
 * it into a buffer at @a offset. Encoding starts at @a offset and this offset
137
 * is moved to the position where the next character can be written to.
162
 * is moved to the position where the next character can be written to.
138
 *
163
 *
139
 * @param ch        Input character.
164
 * @param ch        Input character.
140
 * @param str       Output buffer.
165
 * @param str       Output buffer.
141
 * @param offset    Offset (in bytes) where to start writing.
166
 * @param offset    Offset (in bytes) where to start writing.
142
 * @param sz        Size of the output buffer.
167
 * @param sz        Size of the output buffer.
143
 *
168
 *
144
 * @return EOK if the character was encoded successfully, EOVERFLOW if there
169
 * @return EOK if the character was encoded successfully, EOVERFLOW if there
145
 *     was not enough space in the output buffer or EINVAL if the character
170
 *     was not enough space in the output buffer or EINVAL if the character
146
 *     code was invalid.
171
 *     code was invalid.
147
 */
172
 */
148
int chr_encode(const wchar_t ch, char *str, size_t *offset, size_t sz)
173
int chr_encode(wchar_t ch, char *str, size_t *offset, size_t sz)
149
{
174
{
150
    uint32_t cc;        /* Unsigned version of ch. */
175
    uint32_t cc;        /* Unsigned version of ch. */
151
 
176
 
152
    int cbytes;     /* Number of continuation bytes. */
177
    int cbytes;     /* Number of continuation bytes. */
153
    int b0_bits;        /* Number of data bits in first byte. */
178
    int b0_bits;        /* Number of data bits in first byte. */
154
    int i;
179
    int i;
155
 
180
 
156
    if (*offset >= sz)
181
    if (*offset >= sz)
157
        return EOVERFLOW;
182
        return EOVERFLOW;
158
 
183
 
159
    if (ch < 0)
184
    if (ch < 0)
160
        return EINVAL;
185
        return EINVAL;
161
 
186
 
162
    /* Bit operations should only be done on unsigned numbers. */
187
    /* Bit operations should only be done on unsigned numbers. */
163
    cc = (uint32_t) ch;
188
    cc = (uint32_t) ch;
164
 
189
 
165
    /* Determine how many continuation bytes are needed. */
190
    /* Determine how many continuation bytes are needed. */
166
    if ((cc & ~LO_MASK_32(7)) == 0) {
191
    if ((cc & ~LO_MASK_32(7)) == 0) {
167
        b0_bits = 7;
192
        b0_bits = 7;
168
        cbytes = 0;
193
        cbytes = 0;
169
    } else if ((cc & ~LO_MASK_32(11)) == 0) {
194
    } else if ((cc & ~LO_MASK_32(11)) == 0) {
170
        b0_bits = 5;
195
        b0_bits = 5;
171
        cbytes = 1;
196
        cbytes = 1;
172
    } else if ((cc & ~LO_MASK_32(16)) == 0) {
197
    } else if ((cc & ~LO_MASK_32(16)) == 0) {
173
        b0_bits = 4;
198
        b0_bits = 4;
174
        cbytes = 2;
199
        cbytes = 2;
175
    } else if ((cc & ~LO_MASK_32(21)) == 0) {
200
    } else if ((cc & ~LO_MASK_32(21)) == 0) {
176
        b0_bits = 3;
201
        b0_bits = 3;
177
        cbytes = 3;
202
        cbytes = 3;
178
    } else {
203
    } else {
179
        /* Codes longer than 21 bits are not supported. */
204
        /* Codes longer than 21 bits are not supported. */
180
        return EINVAL;
205
        return EINVAL;
181
    }
206
    }
182
 
207
 
183
    /* Check for available space in buffer. */
208
    /* Check for available space in buffer. */
184
    if (*offset + cbytes >= sz)
209
    if (*offset + cbytes >= sz)
185
        return EOVERFLOW;
210
        return EOVERFLOW;
186
 
211
 
187
    /* Encode continuation bytes. */
212
    /* Encode continuation bytes. */
188
    for (i = cbytes; i > 0; --i) {
213
    for (i = cbytes; i > 0; --i) {
189
        str[*offset + i] = 0x80 | (cc & LO_MASK_32(CONT_BITS));
214
        str[*offset + i] = 0x80 | (cc & LO_MASK_32(CONT_BITS));
190
        cc = cc >> CONT_BITS;
215
        cc = cc >> CONT_BITS;
191
    }
216
    }
192
 
217
 
193
    /* Encode first byte. */
218
    /* Encode first byte. */
194
    str[*offset] = (cc & LO_MASK_32(b0_bits)) | HI_MASK_8(8 - b0_bits - 1);
219
    str[*offset] = (cc & LO_MASK_32(b0_bits)) | HI_MASK_8(8 - b0_bits - 1);
195
 
220
 
196
    /* Advance offset. */
221
    /* Advance offset. */
197
    *offset += (1 + cbytes);
222
    *offset += (1 + cbytes);
198
   
223
   
199
    return EOK;
224
    return EOK;
200
}
225
}
201
 
226
 
-
 
227
/** Get display width of character.
-
 
228
 *
-
 
229
 * @param ch    The character.
-
 
230
 * @return  Character width in display cells.
-
 
231
 */
-
 
232
count_t chr_width(wchar_t ch)
-
 
233
{
-
 
234
    return 1;
-
 
235
}
-
 
236
 
202
/** Get size of string, with length limit.
237
/** Get size of string, with length limit.
203
 *
238
 *
204
 * Get the number of bytes which are used by up to @a max_len first
239
 * Get the number of bytes which are used by up to @a max_len first
205
 * characters in the string @a str. If @a max_len is greater than
240
 * characters in the string @a str. If @a max_len is greater than
206
 * the length of @a str, the entire string is measured.
241
 * the length of @a str, the entire string is measured.
207
 *
242
 *
208
 * @param str   String to consider.
243
 * @param str   String to consider.
209
 * @param count Maximum number of characters to measure.
244
 * @param count Maximum number of characters to measure.
210
 *
245
 *
211
 * @return Number of bytes used by the characters.
246
 * @return  Number of bytes used by the characters.
212
 */
247
 */
213
size_t str_lsize(const char *str, count_t max_len)
248
size_t str_lsize(const char *str, count_t max_len)
214
{
249
{
215
    count_t len = 0;
250
    count_t len = 0;
216
    size_t cur = 0;
251
    size_t cur = 0;
217
    size_t prev;
252
    size_t prev;
218
    wchar_t ch;
253
    wchar_t ch;
219
 
254
 
220
    while (true) {
255
    while (true) {
221
        prev = cur;
256
        prev = cur;
222
        if (len >= max_len)
257
        if (len >= max_len)
223
            break;
258
            break;
224
        ch = chr_decode(str, &cur, UTF8_NO_LIMIT);
259
        ch = chr_decode(str, &cur, UTF8_NO_LIMIT);
225
        if (ch == '\0') break;
260
        if (ch == '\0') break;
226
 
261
 
227
        len++;
262
        len++;
228
    }
263
    }
229
 
264
 
230
    return prev;
265
    return prev;
231
}
266
}
232
 
267
 
-
 
268
/** Get size of string, with width limit.
-
 
269
 *
-
 
270
 * Get the number of bytes which are used by the longest prefix of @a str
-
 
271
 * that can fit into @a max_width display cells.
-
 
272
 *
-
 
273
 * @param str   String to consider.
-
 
274
 * @param count Maximum number of display cells.
-
 
275
 *
-
 
276
 * @return  Number of bytes used by the characters that fit.
-
 
277
 */
-
 
278
size_t str_wsize(const char *str, count_t max_width)
-
 
279
{
-
 
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);
-
 
290
        if (ch == '\0') break;
-
 
291
 
-
 
292
        width += chr_width(ch);
-
 
293
    }
-
 
294
 
-
 
295
    return prev;
-
 
296
}
-
 
297
 
-
 
298
 
-
 
299
/** Get length of wide string, with width limit.
-
 
300
 *
-
 
301
 * Get the number of characters in a wide string that can fit into @a max_width
-
 
302
 * display cells.
-
 
303
 *
-
 
304
 * @param wstr   Wide string to consider.
-
 
305
 * @param count Maximum number of display cells.
-
 
306
 *
-
 
307
 * @return  Number of bytes used by the characters that fit.
-
 
308
 */
-
 
309
count_t wstr_wlength(const wchar_t *wstr, count_t max_width)
-
 
310
{
-
 
311
    count_t width = 0;
-
 
312
    index_t cur = 0;
-
 
313
 
-
 
314
    while (true) {
-
 
315
        if (width >= max_width)
-
 
316
            break;
-
 
317
        if (wstr[cur] == '\0') break;
-
 
318
 
-
 
319
        width += chr_width(wstr[cur]);
-
 
320
        ++cur;
-
 
321
    }
-
 
322
 
-
 
323
    return (count_t) cur;
-
 
324
}
-
 
325
 
233
/** Check whether character is plain ASCII.
326
/** Check whether character is plain ASCII.
234
 *
327
 *
235
 * @return True if character is plain ASCII.
328
 * @return True if character is plain ASCII.
236
 *
329
 *
237
 */
330
 */
238
bool ascii_check(const wchar_t ch)
331
bool ascii_check(const wchar_t ch)
239
{
332
{
240
    if ((ch >= 0) && (ch <= 127))
333
    if ((ch >= 0) && (ch <= 127))
241
        return true;
334
        return true;
242
   
335
   
243
    return false;
336
    return false;
244
}
337
}
245
 
338
 
246
/** Check whether character is Unicode.
339
/** Check whether character is Unicode.
247
 *
340
 *
248
 * @return True if character is valid Unicode code point.
341
 * @return True if character is valid Unicode code point.
249
 */
342
 */
250
bool unicode_check(const wchar_t ch)
343
bool unicode_check(const wchar_t ch)
251
{
344
{
252
    if ((ch >= 0) && (ch <= 1114111))
345
    if ((ch >= 0) && (ch <= 1114111))
253
        return true;
346
        return true;
254
   
347
   
255
    return false;
348
    return false;
256
}
349
}
257
 
350
 
258
/** Return number of bytes the string occupies.
351
/** Return number of bytes the string occupies.
259
 *
352
 *
260
 * @param str A string.
353
 * @param str A string.
261
 * @return Number of bytes in @a str excluding the null terminator.
354
 * @return Number of bytes in @a str excluding the null terminator.
262
 */
355
 */
263
size_t str_size(const char *str)
356
size_t str_size(const char *str)
264
{
357
{
265
    size_t size;
358
    size_t size;
266
 
359
 
267
    size = 0;
360
    size = 0;
268
    while (*str++ != '\0')
361
    while (*str++ != '\0')
269
        ++size;
362
        ++size;
270
 
363
 
271
    return size;
364
    return size;
272
}
365
}
273
 
366
 
274
/** Return number of characters in a string.
367
/** Return number of characters in a string.
275
 *
368
 *
276
 * @param str NULL-terminated string.
369
 * @param str NULL-terminated string.
277
 * @return Number of characters in string.
370
 * @return Number of characters in string.
278
 */
371
 */
279
count_t str_length(const char *str)
372
count_t str_length(const char *str)
280
{
373
{
281
    count_t len = 0;
374
    count_t len = 0;
282
    size_t offset = 0;
375
    size_t offset = 0;
283
 
376
 
284
    while (chr_decode(str, &offset, UTF8_NO_LIMIT) != 0) {
377
    while (chr_decode(str, &offset, UTF8_NO_LIMIT) != 0) {
285
        len++;
378
        len++;
286
    }
379
    }
287
 
380
 
288
    return len;
381
    return len;
289
}
382
}
290
 
383
 
291
/** Return number of characters in a wide string.
384
/** Return number of characters in a wide string.
292
 *
385
 *
293
 * @param str NULL-terminated wide string.
386
 * @param   str NULL-terminated wide string.
294
 * @return Number of characters in @a str.
387
 * @return  Number of characters in @a str.
295
 */
388
 */
296
count_t wstr_length(const wchar_t *wstr)
389
count_t wstr_length(const wchar_t *wstr)
297
{
390
{
298
    count_t len;
391
    count_t len;
299
 
392
 
300
    len = 0;
393
    len = 0;
301
    while (*wstr++ != '\0')
394
    while (*wstr++ != '\0')
302
        ++len;
395
        ++len;
303
 
396
 
304
    return len;
397
    return len;
305
}
398
}
306
 
399
 
307
/** Compare two NULL terminated strings
400
/** Compare two NULL terminated strings
308
 *
401
 *
309
 * Do a char-by-char comparison of two NULL terminated strings.
402
 * Do a char-by-char comparison of two NULL terminated strings.
310
 * The strings are considered equal iff they consist of the same
403
 * The strings are considered equal iff they consist of the same
311
 * characters on the minimum of their lengths.
404
 * characters on the minimum of their lengths.
312
 *
405
 *
313
 * @param src First string to compare.
406
 * @param src First string to compare.
314
 * @param dst Second string to compare.
407
 * @param dst Second string to compare.
315
 *
408
 *
316
 * @return 0 if the strings are equal, -1 if first is smaller, 1 if second smaller.
409
 * @return 0 if the strings are equal, -1 if first is smaller, 1 if second smaller.
317
 *
410
 *
318
 */
411
 */
319
int strcmp(const char *src, const char *dst)
412
int strcmp(const char *src, const char *dst)
320
{
413
{
321
    for (; *src && *dst; src++, dst++) {
414
    for (; *src && *dst; src++, dst++) {
322
        if (*src < *dst)
415
        if (*src < *dst)
323
            return -1;
416
            return -1;
324
        if (*src > *dst)
417
        if (*src > *dst)
325
            return 1;
418
            return 1;
326
    }
419
    }
327
    if (*src == *dst)
420
    if (*src == *dst)
328
        return 0;
421
        return 0;
329
   
422
   
330
    if (!*src)
423
    if (!*src)
331
        return -1;
424
        return -1;
332
   
425
   
333
    return 1;
426
    return 1;
334
}
427
}
335
 
428
 
336
 
429
 
337
/** Compare two NULL terminated strings
430
/** Compare two NULL terminated strings
338
 *
431
 *
339
 * Do a char-by-char comparison of two NULL terminated strings.
432
 * Do a char-by-char comparison of two NULL terminated strings.
340
 * The strings are considered equal iff they consist of the same
433
 * The strings are considered equal iff they consist of the same
341
 * characters on the minimum of their lengths and specified maximal
434
 * characters on the minimum of their lengths and specified maximal
342
 * length.
435
 * length.
343
 *
436
 *
344
 * @param src First string to compare.
437
 * @param src First string to compare.
345
 * @param dst Second string to compare.
438
 * @param dst Second string to compare.
346
 * @param len Maximal length for comparison.
439
 * @param len Maximal length for comparison.
347
 *
440
 *
348
 * @return 0 if the strings are equal, -1 if first is smaller, 1 if second smaller.
441
 * @return 0 if the strings are equal, -1 if first is smaller, 1 if second smaller.
349
 */
442
 */
350
int strncmp(const char *src, const char *dst, size_t len)
443
int strncmp(const char *src, const char *dst, size_t len)
351
{
444
{
352
    unsigned int i;
445
    unsigned int i;
353
   
446
   
354
    for (i = 0; (*src) && (*dst) && (i < len); src++, dst++, i++) {
447
    for (i = 0; (*src) && (*dst) && (i < len); src++, dst++, i++) {
355
        if (*src < *dst)
448
        if (*src < *dst)
356
            return -1;
449
            return -1;
357
       
450
       
358
        if (*src > *dst)
451
        if (*src > *dst)
359
            return 1;
452
            return 1;
360
    }
453
    }
361
   
454
   
362
    if (i == len || *src == *dst)
455
    if (i == len || *src == *dst)
363
        return 0;
456
        return 0;
364
   
457
   
365
    if (!*src)
458
    if (!*src)
366
        return -1;
459
        return -1;
367
   
460
   
368
    return 1;
461
    return 1;
369
}
462
}
370
 
463
 
371
 
464
 
372
 
465
 
373
/** Copy NULL terminated string.
466
/** Copy NULL terminated string.
374
 *
467
 *
375
 * Copy at most 'len' characters from string 'src' to 'dest'.
468
 * Copy at most 'len' characters from string 'src' to 'dest'.
376
 * If 'src' is shorter than 'len', '\0' is inserted behind the
469
 * If 'src' is shorter than 'len', '\0' is inserted behind the
377
 * last copied character.
470
 * last copied character.
378
 *
471
 *
379
 * @param src  Source string.
472
 * @param src  Source string.
380
 * @param dest Destination buffer.
473
 * @param dest Destination buffer.
381
 * @param len  Size of destination buffer.
474
 * @param len  Size of destination buffer.
382
 */
475
 */
383
void strncpy(char *dest, const char *src, size_t len)
476
void strncpy(char *dest, const char *src, size_t len)
384
{
477
{
385
    unsigned int i;
478
    unsigned int i;
386
   
479
   
387
    for (i = 0; i < len; i++) {
480
    for (i = 0; i < len; i++) {
388
        if (!(dest[i] = src[i]))
481
        if (!(dest[i] = src[i]))
389
            return;
482
            return;
390
    }
483
    }
391
   
484
   
392
    dest[i - 1] = '\0';
485
    dest[i - 1] = '\0';
393
}
486
}
394
 
487
 
395
/** Find first occurence of character in string.
488
/** Find first occurence of character in string.
396
 *
489
 *
397
 * @param s String to search.
490
 * @param s String to search.
398
 * @param i Character to look for.
491
 * @param i Character to look for.
399
 *
492
 *
400
 * @return Pointer to character in @a s or NULL if not found.
493
 * @return Pointer to character in @a s or NULL if not found.
401
 */
494
 */
402
extern char *strchr(const char *s, int i)
495
extern char *strchr(const char *s, int i)
403
{
496
{
404
    while (*s != '\0') {
497
    while (*s != '\0') {
405
        if (*s == i)
498
        if (*s == i)
406
            return (char *) s;
499
            return (char *) s;
407
        ++s;
500
        ++s;
408
    }
501
    }
409
   
502
   
410
    return NULL;
503
    return NULL;
411
}
504
}
412
 
505
 
413
/** @}
506
/** @}
414
 */
507
 */
415
 
508