Subversion Repositories HelenOS

Rev

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

Rev 4198 Rev 4199
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 Miscellaneous functions.
36
 */
36
 */
37
 
37
 
38
#include <string.h>
38
#include <string.h>
39
#include <print.h>
39
#include <print.h>
40
#include <cpu.h>
40
#include <cpu.h>
41
#include <arch/asm.h>
41
#include <arch/asm.h>
42
#include <arch.h>
42
#include <arch.h>
43
#include <console/kconsole.h>
43
#include <console/kconsole.h>
44
 
44
 
45
char invalch = '?';
45
char invalch = '?';
46
 
46
 
47
/** Byte mask consisting of lowest @n bits (out of eight). */
47
/** Byte mask consisting of lowest @n bits (out of eight). */
48
#define LO_MASK_8(n) ((uint8_t)((1 << (n)) - 1))
48
#define LO_MASK_8(n) ((uint8_t)((1 << (n)) - 1))
49
 
49
 
50
/** Byte mask consisting of lowest @n bits (out of 32). */
50
/** Byte mask consisting of lowest @n bits (out of 32). */
51
#define LO_MASK_32(n) ((uint32_t)((1 << (n)) - 1))
51
#define LO_MASK_32(n) ((uint32_t)((1 << (n)) - 1))
52
 
52
 
53
/** Byte mask consisting of highest @n bits (out of eight). */
53
/** Byte mask consisting of highest @n bits (out of eight). */
54
#define HI_MASK_8(n) (~LO_MASK_8(8 - (n)))
54
#define HI_MASK_8(n) (~LO_MASK_8(8 - (n)))
55
 
55
 
56
/** Number of data bits in a UTF-8 continuation byte. */
56
/** Number of data bits in a UTF-8 continuation byte. */
57
#define CONT_BITS 6
57
#define CONT_BITS 6
58
 
58
 
59
/** Decode a single UTF-8 character from a NULL-terminated string.
59
/** Decode a single UTF-8 character from a NULL-terminated string.
60
 *
60
 *
61
 * Decode a single UTF-8 character from a plain char NULL-terminated
61
 * Decode a single UTF-8 character from a plain char NULL-terminated
62
 * string. Decoding starts at @index and this index is incremented
62
 * string. Decoding starts at @index and this index is moved to the
-
 
63
 * beginning of the next character. In case of decoding error,
63
 * if the current UTF-8 string is encoded in more than a single byte.
64
 * index advances. However, index is never moved beyond (str+limit).
64
 *
65
 *
65
 * @param str   Plain character NULL-terminated string.
66
 * @param str   Plain character NULL-terminated string.
66
 * @param index Index (counted in plain characters) where to start
67
 * @param index Index (counted in plain characters) where to start
67
 *              the decoding.
68
 *              the decoding.
68
 * @param limit Maximal allowed value of index.
69
 * @param limit Maximal allowed value of index.
69
 *
70
 *
70
 * @return Decoded character in UTF-32 or '?' if the encoding is wrong.
71
 * @return Decoded character in UTF-32 or '?' if the encoding is wrong.
71
 *
72
 *
72
 */
73
 */
73
wchar_t utf8_decode(const char *str, index_t *index, index_t limit)
74
wchar_t utf8_decode(const char *str, index_t *index, index_t limit)
74
{
75
{
75
    uint8_t b0, b;          /* Bytes read from str. */
76
    uint8_t b0, b;          /* Bytes read from str. */
76
    wchar_t ch;
77
    wchar_t ch;
77
 
78
 
78
    int b0_bits;        /* Data bits in first byte. */
79
    int b0_bits;        /* Data bits in first byte. */
79
    int cbytes;     /* Number of continuation bytes. */
80
    int cbytes;     /* Number of continuation bytes. */
80
 
81
 
81
    if (*index > limit)
82
    if (*index + 1 > limit)
82
        return invalch;
83
        return invalch;
83
 
84
 
84
    b0 = (uint8_t) str[*index];
85
    b0 = (uint8_t) str[(*index)++];
85
 
86
 
86
    /* Determine code length. */
87
    /* Determine code length. */
87
 
88
 
88
    if ((b0 & 0x80) == 0) {
89
    if ((b0 & 0x80) == 0) {
89
        /* 0xxxxxxx (Plain ASCII) */
90
        /* 0xxxxxxx (Plain ASCII) */
90
        b0_bits = 7;
91
        b0_bits = 7;
91
        cbytes = 0;
92
        cbytes = 0;
92
    } else if ((b0 & 0xe0) == 0xc0) {
93
    } else if ((b0 & 0xe0) == 0xc0) {
93
        /* 110xxxxx 10xxxxxx */
94
        /* 110xxxxx 10xxxxxx */
94
        b0_bits = 5;
95
        b0_bits = 5;
95
        cbytes = 1;
96
        cbytes = 1;
96
    } else if ((b0 & 0xf0) == 0xe0) {
97
    } else if ((b0 & 0xf0) == 0xe0) {
97
        /* 1110xxxx 10xxxxxx 10xxxxxx */
98
        /* 1110xxxx 10xxxxxx 10xxxxxx */
98
        b0_bits = 4;
99
        b0_bits = 4;
99
        cbytes = 2;
100
        cbytes = 2;
100
    } else if ((b0 & 0xf8) == 0xf0) {
101
    } else if ((b0 & 0xf8) == 0xf0) {
101
        /* 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx */
102
        /* 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx */
102
        b0_bits = 3;
103
        b0_bits = 3;
103
        cbytes = 3;
104
        cbytes = 3;
104
    } else {
105
    } else {
105
        /* 10xxxxxx -- unexpected continuation byte. */
106
        /* 10xxxxxx -- unexpected continuation byte. */
106
        return invalch;
107
        return invalch;
107
    }
108
    }
108
 
109
 
109
    if (*index + cbytes > limit) {
110
    if (*index + cbytes > limit) {
110
        return invalch;
111
        return invalch;
111
    }
112
    }
112
 
113
 
113
    ch = b0 & LO_MASK_8(b0_bits);
114
    ch = b0 & LO_MASK_8(b0_bits);
114
 
115
 
115
    /* Decode continuation bytes. */
116
    /* Decode continuation bytes. */
116
    while (cbytes > 0) {
117
    while (cbytes > 0) {
117
        b = (uint8_t) str[*index + 1];
118
        b = (uint8_t) str[(*index)++];
118
        ++(*index);
-
 
119
 
119
 
120
        /* Must be 10xxxxxx. */
120
        /* Must be 10xxxxxx. */
121
        if ((b & 0xc0) != 0x80) {
121
        if ((b & 0xc0) != 0x80) {
122
            return invalch;
122
            return invalch;
123
        }
123
        }
124
 
124
 
125
        /* Shift data bits to ch. */
125
        /* Shift data bits to ch. */
126
        ch = (ch << CONT_BITS) | (wchar_t) (b & LO_MASK_8(CONT_BITS));
126
        ch = (ch << CONT_BITS) | (wchar_t) (b & LO_MASK_8(CONT_BITS));
127
        --cbytes;
127
        --cbytes;
128
    }
128
    }
129
 
129
 
130
    return ch;
130
    return ch;
131
}
131
}
132
 
132
 
133
/** Encode a single UTF-32 character as UTF-8
133
/** Encode a single UTF-32 character as UTF-8
134
 *
134
 *
135
 * Encode a single UTF-32 character as UTF-8 and store it into
135
 * Encode a single UTF-32 character as UTF-8 and store it into
136
 * the given buffer at @index. Encoding starts at @index and
136
 * the given buffer at @index. Encoding starts at @index and
137
 * this index is incremented if the UTF-8 character takes
137
 * this index is moved at the position where the next character
138
 * more than a single byte.
138
 * can be written to.
139
 *
139
 *
140
 * @param ch    Input UTF-32 character.
140
 * @param ch    Input UTF-32 character.
141
 * @param str   Output buffer.
141
 * @param str   Output buffer.
142
 * @param index Index (counted in plain characters) where to start
142
 * @param index Index (counted in plain characters) where to start
143
 *              the encoding
143
 *              the encoding
144
 * @param limit Maximal allowed value of index.
144
 * @param limit Maximal allowed value of index.
145
 *
145
 *
146
 * @return True if the character was encoded or false if there is not
146
 * @return True if the character was encoded or false if there is not
147
 *         enought space in the output buffer or the character is invalid
147
 *         enought space in the output buffer or the character is invalid
148
 *         Unicode code point.
148
 *         Unicode code point.
149
 *
149
 *
150
 */
150
 */
151
bool utf8_encode(const wchar_t ch, char *str, index_t *index, index_t limit)
151
bool utf8_encode(const wchar_t ch, char *str, index_t *index, index_t limit)
152
{
152
{
153
    uint32_t cc;        /* Unsigned version of ch. */
153
    uint32_t cc;        /* Unsigned version of ch. */
154
 
154
 
155
    int cbytes;     /* Number of continuation bytes. */
155
    int cbytes;     /* Number of continuation bytes. */
156
    int b0_bits;        /* Number of data bits in first byte. */
156
    int b0_bits;        /* Number of data bits in first byte. */
157
    int i;
157
    int i;
158
 
158
 
159
    if (*index > limit)
159
    if (*index >= limit)
160
        return false;
160
        return false;
161
 
161
 
162
    if (ch < 0)
162
    if (ch < 0)
163
        return false;
163
        return false;
164
 
164
 
165
    /* Bit operations should only be done on unsigned numbers. */
165
    /* Bit operations should only be done on unsigned numbers. */
166
    cc = (uint32_t) ch;
166
    cc = (uint32_t) ch;
167
 
167
 
168
    /* Determine how many continuation bytes are needed. */
168
    /* Determine how many continuation bytes are needed. */
169
    if ((cc & ~LO_MASK_32(7)) == 0) {
169
    if ((cc & ~LO_MASK_32(7)) == 0) {
170
        b0_bits = 7;
170
        b0_bits = 7;
171
        cbytes = 0;
171
        cbytes = 0;
172
    } else if ((cc & ~LO_MASK_32(11)) == 0) {
172
    } else if ((cc & ~LO_MASK_32(11)) == 0) {
173
        b0_bits = 5;
173
        b0_bits = 5;
174
        cbytes = 1;
174
        cbytes = 1;
175
    } else if ((cc & ~LO_MASK_32(16)) == 0) {
175
    } else if ((cc & ~LO_MASK_32(16)) == 0) {
176
        b0_bits = 4;
176
        b0_bits = 4;
177
        cbytes = 2;
177
        cbytes = 2;
178
    } else if ((cc & ~LO_MASK_32(21)) == 0) {
178
    } else if ((cc & ~LO_MASK_32(21)) == 0) {
179
        b0_bits = 3;
179
        b0_bits = 3;
180
        cbytes = 3;
180
        cbytes = 3;
181
    } else {
181
    } else {
182
        /* Codes longer than 21 bits are not supported. */
182
        /* Codes longer than 21 bits are not supported. */
183
        return false;
183
        return false;
184
    }
184
    }
185
 
185
 
186
    /* Check for available space in buffer. */
186
    /* Check for available space in buffer. */
187
    if (*index + cbytes > limit)
187
    if (*index + cbytes >= limit)
188
        return false;
188
        return false;
189
 
189
 
190
    /* Encode continuation bytes. */
190
    /* Encode continuation bytes. */
191
    for (i = cbytes; i > 0; --i) {
191
    for (i = cbytes; i > 0; --i) {
192
        str[*index + i] = 0x80 | (cc & LO_MASK_32(CONT_BITS));
192
        str[*index + i] = 0x80 | (cc & LO_MASK_32(CONT_BITS));
193
        cc = cc >> CONT_BITS;
193
        cc = cc >> CONT_BITS;
194
    }
194
    }
195
 
195
 
196
    /* Encode first byte. */
196
    /* Encode first byte. */
197
    str[*index] = (cc & LO_MASK_32(b0_bits)) | HI_MASK_8(8 - b0_bits - 1);
197
    str[*index] = (cc & LO_MASK_32(b0_bits)) | HI_MASK_8(8 - b0_bits - 1);
198
 
198
 
199
    /* Advance index. */
199
    /* Advance index. */
200
    *index += cbytes;
200
    *index += (1 + cbytes);
201
   
201
   
202
    return true;
202
    return true;
203
}
203
}
204
 
204
 
205
/** Get bytes used by UTF-8 characters.
205
/** Get bytes used by UTF-8 characters.
206
 *
206
 *
207
 * Get the number of bytes (count of plain characters) which
207
 * Get the number of bytes (count of plain characters) which
208
 * are used by a given count of UTF-8 characters in a string.
208
 * are used by a given count of UTF-8 characters in a string.
209
 * As UTF-8 encoding is multibyte, there is no constant
209
 * As UTF-8 encoding is multibyte, there is no constant
210
 * correspondence between number of characters and used bytes.
210
 * correspondence between number of characters and used bytes.
211
 *
211
 *
212
 * @param str   UTF-8 string to consider.
212
 * @param str   UTF-8 string to consider.
213
 * @param count Number of UTF-8 characters to count.
213
 * @param count Number of UTF-8 characters to count.
214
 *
214
 *
215
 * @return Number of bytes used by the characters.
215
 * @return Number of bytes used by the characters.
216
 *
216
 *
217
 */
217
 */
218
size_t utf8_count_bytes(const char *str, count_t count)
218
size_t utf8_count_bytes(const char *str, count_t count)
219
{
219
{
220
    size_t size = 0;
220
    size_t size = 0;
221
    index_t index = 0;
221
    index_t index = 0;
-
 
222
    index_t iprev;
-
 
223
    wchar_t ch;
222
   
224
   
-
 
225
    while (true) {
-
 
226
        iprev = index;
-
 
227
        if (size >= count)
-
 
228
            break;
223
    while ((utf8_decode(str, &index, UTF8_NO_LIMIT) != 0) && (size < count)) {
229
        ch = utf8_decode(str, &index, UTF8_NO_LIMIT);
-
 
230
        if (ch == '\0') break;
-
 
231
 
224
        size++;
232
        size++;
225
        index++;
-
 
226
    }
233
    }
227
   
234
   
228
    return index;
235
    return iprev;
229
}
236
}
230
 
237
 
231
/** Check whether character is plain ASCII.
238
/** Check whether character is plain ASCII.
232
 *
239
 *
233
 * @return True if character is plain ASCII.
240
 * @return True if character is plain ASCII.
234
 *
241
 *
235
 */
242
 */
236
bool ascii_check(const wchar_t ch)
243
bool ascii_check(const wchar_t ch)
237
{
244
{
238
    if ((ch >= 0) && (ch <= 127))
245
    if ((ch >= 0) && (ch <= 127))
239
        return true;
246
        return true;
240
   
247
   
241
    return false;
248
    return false;
242
}
249
}
243
 
250
 
244
/** Check whether character is Unicode.
251
/** Check whether character is Unicode.
245
 *
252
 *
246
 * @return True if character is valid Unicode code point.
253
 * @return True if character is valid Unicode code point.
247
 *
254
 *
248
 */
255
 */
249
bool unicode_check(const wchar_t ch)
256
bool unicode_check(const wchar_t ch)
250
{
257
{
251
    if ((ch >= 0) && (ch <= 1114111))
258
    if ((ch >= 0) && (ch <= 1114111))
252
        return true;
259
        return true;
253
   
260
   
254
    return false;
261
    return false;
255
}
262
}
256
 
263
 
257
/** Return number of plain characters in a string.
264
/** Return number of plain characters in a string.
258
 *
265
 *
259
 * @param str NULL-terminated string.
266
 * @param str NULL-terminated string.
260
 *
267
 *
261
 * @return Number of characters in str.
268
 * @return Number of characters in str.
262
 *
269
 *
263
 */
270
 */
264
size_t strlen(const char *str)
271
size_t strlen(const char *str)
265
{
272
{
266
    size_t size;
273
    size_t size;
267
    for (size = 0; str[size]; size++);
274
    for (size = 0; str[size]; size++);
268
   
275
   
269
    return size;
276
    return size;
270
}
277
}
271
 
278
 
272
/** Return number of UTF-8 characters in a string.
279
/** Return number of UTF-8 characters in a string.
273
 *
280
 *
274
 * @param str NULL-terminated UTF-8 string.
281
 * @param str NULL-terminated UTF-8 string.
275
 *
282
 *
276
 * @return Number of UTF-8 characters in str.
283
 * @return Number of UTF-8 characters in str.
277
 *
284
 *
278
 */
285
 */
279
size_t strlen_utf8(const char *str)
286
size_t strlen_utf8(const char *str)
280
{
287
{
281
    size_t size = 0;
288
    size_t size = 0;
282
    index_t index = 0;
289
    index_t index = 0;
283
   
290
   
284
    while (utf8_decode(str, &index, UTF8_NO_LIMIT) != 0) {
291
    while (utf8_decode(str, &index, UTF8_NO_LIMIT) != 0) {
285
        size++;
292
        size++;
286
        index++;
-
 
287
    }
293
    }
288
   
294
   
289
    return size;
295
    return size;
290
}
296
}
291
 
297
 
292
/** Return number of UTF-32 characters in a string.
298
/** Return number of UTF-32 characters in a string.
293
 *
299
 *
294
 * @param str NULL-terminated UTF-32 string.
300
 * @param str NULL-terminated UTF-32 string.
295
 *
301
 *
296
 * @return Number of UTF-32 characters in str.
302
 * @return Number of UTF-32 characters in str.
297
 *
303
 *
298
 */
304
 */
299
size_t strlen_utf32(const wchar_t *str)
305
size_t strlen_utf32(const wchar_t *str)
300
{
306
{
301
    size_t size;
307
    size_t size;
302
    for (size = 0; str[size]; size++);
308
    for (size = 0; str[size]; size++);
303
   
309
   
304
    return size;
310
    return size;
305
}
311
}
306
 
312
 
307
/** Compare two NULL terminated strings
313
/** Compare two NULL terminated strings
308
 *
314
 *
309
 * Do a char-by-char comparison of two NULL terminated strings.
315
 * Do a char-by-char comparison of two NULL terminated strings.
310
 * The strings are considered equal iff they consist of the same
316
 * The strings are considered equal iff they consist of the same
311
 * characters on the minimum of their lengths.
317
 * characters on the minimum of their lengths.
312
 *
318
 *
313
 * @param src First string to compare.
319
 * @param src First string to compare.
314
 * @param dst Second string to compare.
320
 * @param dst Second string to compare.
315
 *
321
 *
316
 * @return 0 if the strings are equal, -1 if first is smaller, 1 if second smaller.
322
 * @return 0 if the strings are equal, -1 if first is smaller, 1 if second smaller.
317
 *
323
 *
318
 */
324
 */
319
int strcmp(const char *src, const char *dst)
325
int strcmp(const char *src, const char *dst)
320
{
326
{
321
    for (; *src && *dst; src++, dst++) {
327
    for (; *src && *dst; src++, dst++) {
322
        if (*src < *dst)
328
        if (*src < *dst)
323
            return -1;
329
            return -1;
324
        if (*src > *dst)
330
        if (*src > *dst)
325
            return 1;
331
            return 1;
326
    }
332
    }
327
    if (*src == *dst)
333
    if (*src == *dst)
328
        return 0;
334
        return 0;
329
   
335
   
330
    if (!*src)
336
    if (!*src)
331
        return -1;
337
        return -1;
332
   
338
   
333
    return 1;
339
    return 1;
334
}
340
}
335
 
341
 
336
 
342
 
337
/** Compare two NULL terminated strings
343
/** Compare two NULL terminated strings
338
 *
344
 *
339
 * Do a char-by-char comparison of two NULL terminated strings.
345
 * Do a char-by-char comparison of two NULL terminated strings.
340
 * The strings are considered equal iff they consist of the same
346
 * The strings are considered equal iff they consist of the same
341
 * characters on the minimum of their lengths and specified maximal
347
 * characters on the minimum of their lengths and specified maximal
342
 * length.
348
 * length.
343
 *
349
 *
344
 * @param src First string to compare.
350
 * @param src First string to compare.
345
 * @param dst Second string to compare.
351
 * @param dst Second string to compare.
346
 * @param len Maximal length for comparison.
352
 * @param len Maximal length for comparison.
347
 *
353
 *
348
 * @return 0 if the strings are equal, -1 if first is smaller, 1 if second smaller.
354
 * @return 0 if the strings are equal, -1 if first is smaller, 1 if second smaller.
349
 *
355
 *
350
 */
356
 */
351
int strncmp(const char *src, const char *dst, size_t len)
357
int strncmp(const char *src, const char *dst, size_t len)
352
{
358
{
353
    unsigned int i;
359
    unsigned int i;
354
   
360
   
355
    for (i = 0; (*src) && (*dst) && (i < len); src++, dst++, i++) {
361
    for (i = 0; (*src) && (*dst) && (i < len); src++, dst++, i++) {
356
        if (*src < *dst)
362
        if (*src < *dst)
357
            return -1;
363
            return -1;
358
       
364
       
359
        if (*src > *dst)
365
        if (*src > *dst)
360
            return 1;
366
            return 1;
361
    }
367
    }
362
   
368
   
363
    if (i == len || *src == *dst)
369
    if (i == len || *src == *dst)
364
        return 0;
370
        return 0;
365
   
371
   
366
    if (!*src)
372
    if (!*src)
367
        return -1;
373
        return -1;
368
   
374
   
369
    return 1;
375
    return 1;
370
}
376
}
371
 
377
 
372
 
378
 
373
 
379
 
374
/** Copy NULL terminated string.
380
/** Copy NULL terminated string.
375
 *
381
 *
376
 * Copy at most 'len' characters from string 'src' to 'dest'.
382
 * Copy at most 'len' characters from string 'src' to 'dest'.
377
 * If 'src' is shorter than 'len', '\0' is inserted behind the
383
 * If 'src' is shorter than 'len', '\0' is inserted behind the
378
 * last copied character.
384
 * last copied character.
379
 *
385
 *
380
 * @param src  Source string.
386
 * @param src  Source string.
381
 * @param dest Destination buffer.
387
 * @param dest Destination buffer.
382
 * @param len  Size of destination buffer.
388
 * @param len  Size of destination buffer.
383
 *
389
 *
384
 */
390
 */
385
void strncpy(char *dest, const char *src, size_t len)
391
void strncpy(char *dest, const char *src, size_t len)
386
{
392
{
387
    unsigned int i;
393
    unsigned int i;
388
   
394
   
389
    for (i = 0; i < len; i++) {
395
    for (i = 0; i < len; i++) {
390
        if (!(dest[i] = src[i]))
396
        if (!(dest[i] = src[i]))
391
            return;
397
            return;
392
    }
398
    }
393
   
399
   
394
    dest[i - 1] = '\0';
400
    dest[i - 1] = '\0';
395
}
401
}
396
 
402
 
397
/** Find first occurence of character in string.
403
/** Find first occurence of character in string.
398
 *
404
 *
399
 * @param s String to search.
405
 * @param s String to search.
400
 * @param i Character to look for.
406
 * @param i Character to look for.
401
 *
407
 *
402
 * @return Pointer to character in @a s or NULL if not found.
408
 * @return Pointer to character in @a s or NULL if not found.
403
 */
409
 */
404
extern char *strchr(const char *s, int i)
410
extern char *strchr(const char *s, int i)
405
{
411
{
406
    while (*s != '\0') {
412
    while (*s != '\0') {
407
        if (*s == i)
413
        if (*s == i)
408
            return (char *) s;
414
            return (char *) s;
409
        ++s;
415
        ++s;
410
    }
416
    }
411
   
417
   
412
    return NULL;
418
    return NULL;
413
}
419
}
414
 
420
 
415
/** @}
421
/** @}
416
 */
422
 */
417
 
423