1 /* Copyright (C) 2010-2020 The RetroArch team
3 * ---------------------------------------------------------------------------------------
4 * The following license statement only applies to this file (stdstring.h).
5 * ---------------------------------------------------------------------------------------
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:
13 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
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.
23 #ifndef __LIBRETRO_SDK_STDSTRING_H
24 #define __LIBRETRO_SDK_STDSTRING_H
32 #include <retro_common_api.h>
33 #include <retro_inline.h>
34 #include <compat/strl.h>
38 #define STRLEN_CONST(x) ((sizeof((x))-1))
40 #define strcpy_literal(a, b) strcpy(a, b)
42 #define string_is_not_equal(a, b) !string_is_equal((a), (b))
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)
47 #define TOLOWER(c) (c | (lr_char_props[c] & 0x20))
48 #define TOUPPER(c) (c & ~(lr_char_props[c] & 0x20))
50 /* C standard says \f \v are space, but this one disagrees */
51 #define ISSPACE(c) (lr_char_props[c] & 0x80)
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)
62 /* Deprecated alias, all callers should use string_is_equal_case_insensitive instead */
63 #define string_is_equal_noncase string_is_equal_case_insensitive
65 static INLINE bool string_is_empty(const char *data)
67 return !data || (*data == '\0');
70 static INLINE bool string_is_equal(const char *a, const char *b)
72 return (a && b) ? !strcmp(a, b) : false;
75 static INLINE bool string_starts_with_size(const char *str, const char *prefix,
78 return (str && prefix) ? !strncmp(prefix, str, size) : false;
81 static INLINE bool string_starts_with(const char *str, const char *prefix)
83 return (str && prefix) ? !strncmp(prefix, str, strlen(prefix)) : false;
86 static INLINE bool string_ends_with_size(const char *str, const char *suffix,
87 size_t str_len, size_t suffix_len)
89 return (str_len < suffix_len) ? false :
90 !memcmp(suffix, str + (str_len - suffix_len), suffix_len);
93 static INLINE bool string_ends_with(const char *str, const char *suffix)
97 return string_ends_with_size(str, suffix, strlen(str), strlen(suffix));
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)
109 while (i < size && str[i]) i++;
114 static INLINE bool string_is_equal_case_insensitive(const char *a,
118 const unsigned char *p1 = (const unsigned char*)a;
119 const unsigned char *p2 = (const unsigned char*)b;
126 while ((result = tolower (*p1) - tolower (*p2++)) == 0)
130 return (result == 0);
133 char *string_to_upper(char *s);
135 char *string_to_lower(char *s);
137 char *string_ucwords(char *s);
139 char *string_replace_substring(const char *in, const char *pattern,
142 /* Remove leading whitespaces */
143 char *string_trim_whitespace_left(char *const s);
145 /* Remove trailing whitespaces */
146 char *string_trim_whitespace_right(char *const s);
148 /* Remove leading and trailing whitespaces */
149 char *string_trim_whitespace(char *const s);
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);
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
160 * > Tokens *include* empty strings
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, ",")))
167 * printf("%s\n", token);
172 char* string_tokenize(char **str, const char *delim);
174 /* Removes every instance of character 'c' from 'str' */
175 void string_remove_all_chars(char *str, char c);
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);
181 /* Converts string to unsigned integer.
182 * Returns 0 if string is invalid */
183 unsigned string_to_unsigned(const char *str);
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);
190 char *string_init(const char *src);
192 void string_set(char **string, const char *src);
194 extern const unsigned char lr_char_props[256];