Changeset d066259 in mainline
- Timestamp:
- 2019-02-05T17:42:58Z (6 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- 08e103d4, bb97118, d80fa05
- Parents:
- cca2d93b
- Files:
-
- 7 edited
Legend:
- Unmodified
- Added
- Removed
-
boot/generic/include/errno.h
rcca2d93b rd066259 40 40 #define EOVERFLOW -16 /* The result does not fit its size. */ 41 41 42 typedef int errno_t; 43 42 44 #endif 43 45 -
boot/generic/include/str.h
rcca2d93b rd066259 1 1 /* 2 * Copyright (c) 2010 Martin Decky 2 * Copyright (c) 2001-2004 Jakub Jermar 3 * Copyright (c) 2005 Martin Decky 4 * Copyright (c) 2011 Oleg Romanenko 3 5 * All rights reserved. 4 6 * … … 33 35 #define BOOT_STR_H_ 34 36 37 #include <errno.h> 35 38 #include <stdbool.h> 36 39 #include <stddef.h> 37 40 38 /* *<Common Unicode characters */39 #define U_SPECIAL '?'41 /* Common Unicode characters */ 42 #define U_SPECIAL '?' 40 43 41 /** <No size limit constant */44 /** No size limit constant */ 42 45 #define STR_NO_LIMIT ((size_t) -1) 43 46 44 extern wchar_t str_decode(const char * , size_t *, size_t);45 extern int chr_encode(wchar_t, char *, size_t *, size_t);47 extern wchar_t str_decode(const char *str, size_t *offset, size_t sz); 48 extern errno_t chr_encode(wchar_t ch, char *str, size_t *offset, size_t sz); 46 49 47 extern size_t str_size(const char * );48 extern size_t str_lsize(const char * , size_t);49 extern size_t str_length(const char * );50 extern size_t str_size(const char *str); 51 extern size_t str_lsize(const char *str, size_t max_len); 52 extern size_t str_length(const char *str); 50 53 51 extern bool ascii_check(wchar_t );52 extern bool chr_check(wchar_t );54 extern bool ascii_check(wchar_t ch); 55 extern bool chr_check(wchar_t ch); 53 56 54 extern int str_cmp(const char * , const char *);55 extern void str_cpy(char * , size_t, const char *);57 extern int str_cmp(const char *s1, const char *s2); 58 extern void str_cpy(char *dest, size_t size, const char *src); 56 59 57 60 #endif -
boot/generic/src/str.c
rcca2d93b rd066259 1 1 /* 2 2 * Copyright (c) 2001-2004 Jakub Jermar 3 * Copyright (c) 2005 Martin Decky 4 * Copyright (c) 2008 Jiri Svoboda 5 * Copyright (c) 2011 Martin Sucha 6 * Copyright (c) 2011 Oleg Romanenko 3 7 * All rights reserved. 4 8 * … … 98 102 */ 99 103 104 #include <str.h> 105 100 106 #include <errno.h> 101 107 #include <stdbool.h> 102 108 #include <stddef.h> 103 109 #include <stdint.h> 104 #include <str.h>105 110 106 111 /** Check the condition if wchar_t is signed */ … … 208 213 * code was invalid. 209 214 */ 210 int chr_encode(const wchar_t ch, char *str, size_t *offset, size_t size)215 errno_t chr_encode(const wchar_t ch, char *str, size_t *offset, size_t size) 211 216 { 212 217 if (*offset >= size) … … 392 397 return 1; 393 398 394 if ( (c1 == 0) || (c2 == 0))399 if (c1 == 0 || c2 == 0) 395 400 break; 396 401 } -
kernel/generic/include/str.h
rcca2d93b rd066259 1 1 /* 2 2 * Copyright (c) 2001-2004 Jakub Jermar 3 * Copyright (c) 2005 Martin Decky 4 * Copyright (c) 2011 Oleg Romanenko 3 5 * All rights reserved. 4 6 * … … 36 38 #define KERN_STR_H_ 37 39 40 #include <errno.h> 38 41 #include <stdbool.h> 39 42 #include <stddef.h> 40 43 #include <stdint.h> 41 #include <errno.h>42 44 43 /* *<Common Unicode characters */45 /* Common Unicode characters */ 44 46 #define U_SPECIAL '?' 45 47 … … 61 63 #define U_CURSOR 0x2588 62 64 63 /** <No size limit constant */65 /** No size limit constant */ 64 66 #define STR_NO_LIMIT ((size_t) -1) 65 67 66 /** < Maximum size of a string containing cntcharacters */67 #define STR_BOUNDS( cnt) ((cnt) << 2)68 /** Maximum size of a string containing @c length characters */ 69 #define STR_BOUNDS(length) ((length) << 2) 68 70 69 71 extern wchar_t str_decode(const char *str, size_t *offset, size_t sz); … … 92 94 extern void wstr_to_str(char *dest, size_t size, const wchar_t *src); 93 95 94 extern char *str_dup(const char *src);95 extern char *str_ndup(const char *src, size_t n);96 97 96 extern char *str_chr(const char *str, wchar_t ch); 98 97 … … 100 99 extern bool wstr_remove(wchar_t *str, size_t pos); 101 100 102 extern errno_t str_uint64_t(const char *, char **, unsigned int, bool, uint64_t *); 101 extern char *str_dup(const char *src); 102 extern char *str_ndup(const char *src, size_t n); 103 104 extern errno_t str_uint64_t(const char *, char **, unsigned int, bool, 105 uint64_t *); 103 106 104 107 extern void order_suffix(const uint64_t, uint64_t *, char *); -
kernel/generic/src/lib/str.c
rcca2d93b rd066259 1 1 /* 2 2 * Copyright (c) 2001-2004 Jakub Jermar 3 * Copyright (c) 2005 Martin Decky 4 * Copyright (c) 2008 Jiri Svoboda 5 * Copyright (c) 2011 Martin Sucha 6 * Copyright (c) 2011 Oleg Romanenko 3 7 * All rights reserved. 4 8 * … … 103 107 104 108 #include <str.h> 105 #include <cpu.h> 106 #include <arch/asm.h> 107 #include <arch.h> 109 110 #include <assert.h> 108 111 #include <errno.h> 112 #include <stdbool.h> 113 #include <stddef.h> 114 #include <stdint.h> 115 #include <stdlib.h> 116 109 117 #include <align.h> 110 #include <assert.h>111 118 #include <macros.h> 112 #include <stdlib.h>113 119 114 120 /** Check the condition if wchar_t is signed */ … … 616 622 } 617 623 624 /** Convert wide string to string. 625 * 626 * Convert wide string @a src to string. The output is written to the buffer 627 * specified by @a dest and @a size. @a size must be non-zero and the string 628 * written will always be well-formed. 629 * 630 * @param dest Destination buffer. 631 * @param size Size of the destination buffer. 632 * @param src Source wide string. 633 */ 634 void wstr_to_str(char *dest, size_t size, const wchar_t *src) 635 { 636 wchar_t ch; 637 size_t src_idx; 638 size_t dest_off; 639 640 /* There must be space for a null terminator in the buffer. */ 641 assert(size > 0); 642 643 src_idx = 0; 644 dest_off = 0; 645 646 while ((ch = src[src_idx++]) != 0) { 647 if (chr_encode(ch, dest, &dest_off, size - 1) != EOK) 648 break; 649 } 650 651 dest[dest_off] = '\0'; 652 } 653 654 /** Find first occurence of character in string. 655 * 656 * @param str String to search. 657 * @param ch Character to look for. 658 * 659 * @return Pointer to character in @a str or NULL if not found. 660 */ 661 char *str_chr(const char *str, wchar_t ch) 662 { 663 wchar_t acc; 664 size_t off = 0; 665 size_t last = 0; 666 667 while ((acc = str_decode(str, &off, STR_NO_LIMIT)) != 0) { 668 if (acc == ch) 669 return (char *) (str + last); 670 last = off; 671 } 672 673 return NULL; 674 } 675 676 /** Insert a wide character into a wide string. 677 * 678 * Insert a wide character into a wide string at position 679 * @a pos. The characters after the position are shifted. 680 * 681 * @param str String to insert to. 682 * @param ch Character to insert to. 683 * @param pos Character index where to insert. 684 * @param max_pos Characters in the buffer. 685 * 686 * @return True if the insertion was sucessful, false if the position 687 * is out of bounds. 688 * 689 */ 690 bool wstr_linsert(wchar_t *str, wchar_t ch, size_t pos, size_t max_pos) 691 { 692 size_t len = wstr_length(str); 693 694 if ((pos > len) || (pos + 1 > max_pos)) 695 return false; 696 697 size_t i; 698 for (i = len; i + 1 > pos; i--) 699 str[i + 1] = str[i]; 700 701 str[pos] = ch; 702 703 return true; 704 } 705 706 /** Remove a wide character from a wide string. 707 * 708 * Remove a wide character from a wide string at position 709 * @a pos. The characters after the position are shifted. 710 * 711 * @param str String to remove from. 712 * @param pos Character index to remove. 713 * 714 * @return True if the removal was sucessful, false if the position 715 * is out of bounds. 716 * 717 */ 718 bool wstr_remove(wchar_t *str, size_t pos) 719 { 720 size_t len = wstr_length(str); 721 722 if (pos >= len) 723 return false; 724 725 size_t i; 726 for (i = pos + 1; i <= len; i++) 727 str[i - 1] = str[i]; 728 729 return true; 730 } 731 618 732 /** Duplicate string. 619 733 * … … 675 789 str_ncpy(dest, size + 1, src, size); 676 790 return dest; 677 }678 679 /** Convert wide string to string.680 *681 * Convert wide string @a src to string. The output is written to the buffer682 * specified by @a dest and @a size. @a size must be non-zero and the string683 * written will always be well-formed.684 *685 * @param dest Destination buffer.686 * @param size Size of the destination buffer.687 * @param src Source wide string.688 */689 void wstr_to_str(char *dest, size_t size, const wchar_t *src)690 {691 wchar_t ch;692 size_t src_idx;693 size_t dest_off;694 695 /* There must be space for a null terminator in the buffer. */696 assert(size > 0);697 698 src_idx = 0;699 dest_off = 0;700 701 while ((ch = src[src_idx++]) != 0) {702 if (chr_encode(ch, dest, &dest_off, size - 1) != EOK)703 break;704 }705 706 dest[dest_off] = '\0';707 }708 709 /** Find first occurence of character in string.710 *711 * @param str String to search.712 * @param ch Character to look for.713 *714 * @return Pointer to character in @a str or NULL if not found.715 *716 */717 char *str_chr(const char *str, wchar_t ch)718 {719 wchar_t acc;720 size_t off = 0;721 size_t last = 0;722 723 while ((acc = str_decode(str, &off, STR_NO_LIMIT)) != 0) {724 if (acc == ch)725 return (char *) (str + last);726 last = off;727 }728 729 return NULL;730 }731 732 /** Insert a wide character into a wide string.733 *734 * Insert a wide character into a wide string at position735 * @a pos. The characters after the position are shifted.736 *737 * @param str String to insert to.738 * @param ch Character to insert to.739 * @param pos Character index where to insert.740 * @param max_pos Characters in the buffer.741 *742 * @return True if the insertion was sucessful, false if the position743 * is out of bounds.744 *745 */746 bool wstr_linsert(wchar_t *str, wchar_t ch, size_t pos, size_t max_pos)747 {748 size_t len = wstr_length(str);749 750 if ((pos > len) || (pos + 1 > max_pos))751 return false;752 753 size_t i;754 for (i = len; i + 1 > pos; i--)755 str[i + 1] = str[i];756 757 str[pos] = ch;758 759 return true;760 }761 762 /** Remove a wide character from a wide string.763 *764 * Remove a wide character from a wide string at position765 * @a pos. The characters after the position are shifted.766 *767 * @param str String to remove from.768 * @param pos Character index to remove.769 *770 * @return True if the removal was sucessful, false if the position771 * is out of bounds.772 *773 */774 bool wstr_remove(wchar_t *str, size_t pos)775 {776 size_t len = wstr_length(str);777 778 if (pos >= len)779 return false;780 781 size_t i;782 for (i = pos + 1; i <= len; i++)783 str[i - 1] = str[i];784 785 return true;786 791 } 787 792 -
uspace/lib/c/generic/str.c
rcca2d93b rd066259 1 1 /* 2 * Copyright (c) 2001-2004 Jakub Jermar 2 3 * Copyright (c) 2005 Martin Decky 3 4 * Copyright (c) 2008 Jiri Svoboda … … 33 34 * @{ 34 35 */ 35 /** @file 36 37 /** 38 * @file 39 * @brief String functions. 40 * 41 * Strings and characters use the Universal Character Set (UCS). The standard 42 * strings, called just strings are encoded in UTF-8. Wide strings (encoded 43 * in UTF-32) are supported to a limited degree. A single character is 44 * represented as wchar_t.@n 45 * 46 * Overview of the terminology:@n 47 * 48 * Term Meaning 49 * -------------------- ---------------------------------------------------- 50 * byte 8 bits stored in uint8_t (unsigned 8 bit integer) 51 * 52 * character UTF-32 encoded Unicode character, stored in wchar_t 53 * (signed 32 bit integer), code points 0 .. 1114111 54 * are valid 55 * 56 * ASCII character 7 bit encoded ASCII character, stored in char 57 * (usually signed 8 bit integer), code points 0 .. 127 58 * are valid 59 * 60 * string UTF-8 encoded NULL-terminated Unicode string, char * 61 * 62 * wide string UTF-32 encoded NULL-terminated Unicode string, 63 * wchar_t * 64 * 65 * [wide] string size number of BYTES in a [wide] string (excluding 66 * the NULL-terminator), size_t 67 * 68 * [wide] string length number of CHARACTERS in a [wide] string (excluding 69 * the NULL-terminator), size_t 70 * 71 * [wide] string width number of display cells on a monospace display taken 72 * by a [wide] string, size_t 73 * 74 * 75 * Overview of string metrics:@n 76 * 77 * Metric Abbrev. Type Meaning 78 * ------ ------ ------ ------------------------------------------------- 79 * size n size_t number of BYTES in a string (excluding the 80 * NULL-terminator) 81 * 82 * length l size_t number of CHARACTERS in a string (excluding the 83 * null terminator) 84 * 85 * width w size_t number of display cells on a monospace display 86 * taken by a string 87 * 88 * 89 * Function naming prefixes:@n 90 * 91 * chr_ operate on characters 92 * ascii_ operate on ASCII characters 93 * str_ operate on strings 94 * wstr_ operate on wide strings 95 * 96 * [w]str_[n|l|w] operate on a prefix limited by size, length 97 * or width 98 * 99 * 100 * A specific character inside a [wide] string can be referred to by:@n 101 * 102 * pointer (char *, wchar_t *) 103 * byte offset (size_t) 104 * character index (size_t) 105 * 36 106 */ 37 107 38 108 #include <str.h> 109 110 #include <assert.h> 111 #include <ctype.h> 112 #include <errno.h> 113 #include <stdbool.h> 39 114 #include <stddef.h> 40 115 #include <stdint.h> 41 116 #include <stdlib.h> 42 #include <assert.h> 43 #include <ctype.h> 44 #include <errno.h> 117 45 118 #include <align.h> 46 119 #include <mem.h> 47 #include <limits.h>48 120 49 121 /** Check the condition if wchar_t is signed */ … … 747 819 /* There must be space for a null terminator in the buffer. */ 748 820 assert(size > 0); 821 assert(src != NULL); 749 822 750 823 size_t src_off = 0; … … 1311 1384 { 1312 1385 size_t size = str_size(src) + 1; 1313 char *dest = (char *)malloc(size);1314 if ( dest == NULL)1315 return (char *)NULL;1386 char *dest = malloc(size); 1387 if (!dest) 1388 return NULL; 1316 1389 1317 1390 str_cpy(dest, size, src); … … 1345 1418 size = n; 1346 1419 1347 char *dest = (char *)malloc(size + 1);1348 if ( dest == NULL)1349 return (char *)NULL;1420 char *dest = malloc(size + 1); 1421 if (!dest) 1422 return NULL; 1350 1423 1351 1424 str_ncpy(dest, size + 1, src, size); -
uspace/lib/c/include/str.h
rcca2d93b rd066259 1 1 /* 2 * Copyright (c) 2001-2004 Jakub Jermar 2 3 * Copyright (c) 2005 Martin Decky 3 4 * Copyright (c) 2011 Oleg Romanenko … … 42 43 43 44 #include <errno.h> 44 #include < mem.h>45 #include <stdbool.h> 45 46 #include <stddef.h> 46 47 #include <stdint.h> 47 #include <stdbool.h>48 48 49 #define U_SPECIAL '?' 49 #include <mem.h> 50 51 /* Common Unicode characters */ 52 #define U_SPECIAL '?' 50 53 51 54 /** No size limit constant */ … … 63 66 extern wchar_t str_decode(const char *str, size_t *offset, size_t sz); 64 67 extern wchar_t str_decode_reverse(const char *str, size_t *offset, size_t sz); 65 extern errno_t chr_encode( constwchar_t ch, char *str, size_t *offset, size_t sz);68 extern errno_t chr_encode(wchar_t ch, char *str, size_t *offset, size_t sz); 66 69 67 70 extern size_t str_size(const char *str); … … 116 119 extern bool wstr_remove(wchar_t *str, size_t pos); 117 120 118 extern char *str_dup(const char * );119 extern char *str_ndup(const char * , size_t max_size);121 extern char *str_dup(const char *src); 122 extern char *str_ndup(const char *src, size_t n); 120 123 121 124 extern char *str_tok(char *, const char *, char **);
Note:
See TracChangeset
for help on using the changeset viewer.