226a5691 |
1 | /* Copyright (C) 2010-2020 The RetroArch team |
2 | * |
3 | * --------------------------------------------------------------------------------------- |
4 | * The following license statement only applies to this file (stdstring.h). |
5 | * --------------------------------------------------------------------------------------- |
6 | * |
7 | * Permission is hereby granted, free of charge, |
8 | * to any person obtaining a copy of this software and associated documentation files (the "Software"), |
9 | * to deal in the Software without restriction, including without limitation the rights to |
10 | * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, |
11 | * and to permit persons to whom the Software is furnished to do so, subject to the following conditions: |
12 | * |
13 | * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. |
14 | * |
15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, |
16 | * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
18 | * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
19 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
20 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
21 | */ |
22 | |
23 | #ifndef __LIBRETRO_SDK_STDSTRING_H |
24 | #define __LIBRETRO_SDK_STDSTRING_H |
25 | |
26 | #include <stdlib.h> |
27 | #include <stddef.h> |
28 | #include <ctype.h> |
29 | #include <string.h> |
30 | #include <boolean.h> |
31 | |
32 | #include <retro_common_api.h> |
33 | #include <retro_inline.h> |
34 | #include <compat/strl.h> |
35 | |
36 | RETRO_BEGIN_DECLS |
37 | |
38 | #define STRLEN_CONST(x) ((sizeof((x))-1)) |
39 | |
40 | #define strcpy_literal(a, b) strcpy(a, b) |
41 | |
42 | #define string_is_not_equal(a, b) !string_is_equal((a), (b)) |
43 | |
44 | #define string_is_not_equal_fast(a, b, size) (memcmp(a, b, size) != 0) |
45 | #define string_is_equal_fast(a, b, size) (memcmp(a, b, size) == 0) |
46 | |
47 | #define TOLOWER(c) (c | (lr_char_props[c] & 0x20)) |
48 | #define TOUPPER(c) (c & ~(lr_char_props[c] & 0x20)) |
49 | |
50 | /* C standard says \f \v are space, but this one disagrees */ |
51 | #define ISSPACE(c) (lr_char_props[c] & 0x80) |
52 | |
53 | #define ISDIGIT(c) (lr_char_props[c] & 0x40) |
54 | #define ISALPHA(c) (lr_char_props[c] & 0x20) |
55 | #define ISLOWER(c) (lr_char_props[c] & 0x04) |
56 | #define ISUPPER(c) (lr_char_props[c] & 0x02) |
57 | #define ISALNUM(c) (lr_char_props[c] & 0x60) |
58 | #define ISUALPHA(c) (lr_char_props[c] & 0x28) |
59 | #define ISUALNUM(c) (lr_char_props[c] & 0x68) |
60 | #define IS_XDIGIT(c) (lr_char_props[c] & 0x01) |
61 | |
62 | /* Deprecated alias, all callers should use string_is_equal_case_insensitive instead */ |
63 | #define string_is_equal_noncase string_is_equal_case_insensitive |
64 | |
65 | static INLINE bool string_is_empty(const char *data) |
66 | { |
67 | return !data || (*data == '\0'); |
68 | } |
69 | |
70 | static INLINE bool string_is_equal(const char *a, const char *b) |
71 | { |
72 | return (a && b) ? !strcmp(a, b) : false; |
73 | } |
74 | |
75 | static INLINE bool string_starts_with_size(const char *str, const char *prefix, |
76 | size_t size) |
77 | { |
78 | return (str && prefix) ? !strncmp(prefix, str, size) : false; |
79 | } |
80 | |
81 | static INLINE bool string_starts_with(const char *str, const char *prefix) |
82 | { |
83 | return (str && prefix) ? !strncmp(prefix, str, strlen(prefix)) : false; |
84 | } |
85 | |
86 | static INLINE bool string_ends_with_size(const char *str, const char *suffix, |
87 | size_t str_len, size_t suffix_len) |
88 | { |
89 | return (str_len < suffix_len) ? false : |
90 | !memcmp(suffix, str + (str_len - suffix_len), suffix_len); |
91 | } |
92 | |
93 | static INLINE bool string_ends_with(const char *str, const char *suffix) |
94 | { |
95 | if (!str || !suffix) |
96 | return false; |
97 | return string_ends_with_size(str, suffix, strlen(str), strlen(suffix)); |
98 | } |
99 | |
100 | /* Returns the length of 'str' (c.f. strlen()), but only |
101 | * checks the first 'size' characters |
102 | * - If 'str' is NULL, returns 0 |
103 | * - If 'str' is not NULL and no '\0' character is found |
104 | * in the first 'size' characters, returns 'size' */ |
105 | static INLINE size_t strlen_size(const char *str, size_t size) |
106 | { |
107 | size_t i = 0; |
108 | if (str) |
109 | while (i < size && str[i]) i++; |
110 | return i; |
111 | } |
112 | |
113 | |
114 | static INLINE bool string_is_equal_case_insensitive(const char *a, |
115 | const char *b) |
116 | { |
117 | int result = 0; |
118 | const unsigned char *p1 = (const unsigned char*)a; |
119 | const unsigned char *p2 = (const unsigned char*)b; |
120 | |
121 | if (!a || !b) |
122 | return false; |
123 | if (p1 == p2) |
124 | return true; |
125 | |
126 | while ((result = tolower (*p1) - tolower (*p2++)) == 0) |
127 | if (*p1++ == '\0') |
128 | break; |
129 | |
130 | return (result == 0); |
131 | } |
132 | |
133 | char *string_to_upper(char *s); |
134 | |
135 | char *string_to_lower(char *s); |
136 | |
137 | char *string_ucwords(char *s); |
138 | |
139 | char *string_replace_substring(const char *in, const char *pattern, |
140 | const char *by); |
141 | |
142 | /* Remove leading whitespaces */ |
143 | char *string_trim_whitespace_left(char *const s); |
144 | |
145 | /* Remove trailing whitespaces */ |
146 | char *string_trim_whitespace_right(char *const s); |
147 | |
148 | /* Remove leading and trailing whitespaces */ |
149 | char *string_trim_whitespace(char *const s); |
150 | |
151 | /* max_lines == 0 means no limit */ |
152 | char *word_wrap(char *buffer, const char *string, |
153 | int line_width, bool unicode, unsigned max_lines); |
154 | |
155 | /* Splits string into tokens seperated by 'delim' |
156 | * > Returned token string must be free()'d |
157 | * > Returns NULL if token is not found |
158 | * > After each call, 'str' is set to the position after the |
159 | * last found token |
160 | * > Tokens *include* empty strings |
161 | * Usage example: |
162 | * char *str = "1,2,3,4,5,6,7,,,10,"; |
163 | * char **str_ptr = &str; |
164 | * char *token = NULL; |
165 | * while ((token = string_tokenize(str_ptr, ","))) |
166 | * { |
167 | * printf("%s\n", token); |
168 | * free(token); |
169 | * token = NULL; |
170 | * } |
171 | */ |
172 | char* string_tokenize(char **str, const char *delim); |
173 | |
174 | /* Removes every instance of character 'c' from 'str' */ |
175 | void string_remove_all_chars(char *str, char c); |
176 | |
177 | /* Replaces every instance of character 'find' in 'str' |
178 | * with character 'replace' */ |
179 | void string_replace_all_chars(char *str, char find, char replace); |
180 | |
181 | /* Converts string to unsigned integer. |
182 | * Returns 0 if string is invalid */ |
183 | unsigned string_to_unsigned(const char *str); |
184 | |
185 | /* Converts hexadecimal string to unsigned integer. |
186 | * Handles optional leading '0x'. |
187 | * Returns 0 if string is invalid */ |
188 | unsigned string_hex_to_unsigned(const char *str); |
189 | |
190 | char *string_init(const char *src); |
191 | |
192 | void string_set(char **string, const char *src); |
193 | |
194 | extern const unsigned char lr_char_props[256]; |
195 | |
196 | RETRO_END_DECLS |
197 | |
198 | #endif |