source: mainline/kernel/generic/src/lib/str.c@ 88dea9d

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 88dea9d was abf09311, checked in by Martin Decky <martin@…>, 15 years ago

much safer implementation of str_dup() and str_ndup()
port str_dup() and str_ndup() into the kernel

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