1 /* Copyright (C) 2010-2020 The RetroArch team
3 * ---------------------------------------------------------------------------------------
4 * The following license statement only applies to this file (stdstring.c).
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.
27 #include <compat/strl.h>
28 #include <string/stdstring.h>
29 #include <encodings/utf.h>
31 const uint8_t lr_char_props[256] = {
32 /*x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xA xB xC xD xE xF */
33 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0x80,0x00,0x00,0x80,0x00,0x00, /* 0x */
34 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 1x */
35 0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 2x !"#$%&'()*+,-./ */
36 0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x00,0x00,0x00,0x00,0x00,0x00, /* 3x 0123456789:;<=>? */
37 0x00,0x23,0x23,0x23,0x23,0x23,0x23,0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x22, /* 4x @ABCDEFGHIJKLMNO */
38 0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x00,0x00,0x00,0x00,0x08, /* 5x PQRSTUVWXYZ[\]^_ */
39 0x00,0x25,0x25,0x25,0x25,0x25,0x25,0x24,0x24,0x24,0x24,0x24,0x24,0x24,0x24,0x24, /* 6x `abcdefghijklmno */
40 0x24,0x24,0x24,0x24,0x24,0x24,0x24,0x24,0x24,0x24,0x24,0x00,0x00,0x00,0x00,0x00, /* 7x pqrstuvwxyz{|}~ */
41 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 8x */
42 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 9x */
43 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* Ax */
44 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* Bx */
45 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* Cx */
46 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* Dx */
47 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* Ex */
48 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* Fx */
51 char *string_init(const char *src)
53 return src ? strdup(src) : NULL;
56 void string_set(char **string, const char *src)
59 *string = string_init(src);
63 char *string_to_upper(char *s)
66 for ( ; *cs != '\0'; cs++)
67 *cs = toupper((unsigned char)*cs);
71 char *string_to_lower(char *s)
74 for ( ; *cs != '\0'; cs++)
75 *cs = tolower((unsigned char)*cs);
79 char *string_ucwords(char *s)
82 for ( ; *cs != '\0'; cs++)
85 *(cs+1) = toupper((unsigned char)*(cs+1));
88 s[0] = toupper((unsigned char)s[0]);
92 char *string_replace_substring(const char *in,
93 const char *pattern, size_t pattern_len,
94 const char *replacement, size_t replacement_len)
98 const char *inat = NULL;
99 const char *inprev = NULL;
103 /* if either pattern or replacement is NULL,
104 * duplicate in and let caller handle it. */
105 if (!pattern || !replacement)
110 while ((inat = strstr(inat, pattern)))
116 outlen = strlen(in) - pattern_len*numhits + replacement_len*numhits;
118 if (!(out = (char *)malloc(outlen+1)))
125 while ((inat = strstr(inat, pattern)))
127 memcpy(outat, inprev, inat-inprev);
128 outat += inat-inprev;
129 memcpy(outat, replacement, replacement_len);
130 outat += replacement_len;
134 strcpy(outat, inprev);
140 * string_trim_whitespace_left:
142 * Remove leading whitespaces
144 char *string_trim_whitespace_left(char *const s)
148 size_t len = strlen(s);
151 while (*current && ISSPACE((unsigned char)*current))
158 memmove(s, current, len + 1);
165 * string_trim_whitespace_right:
167 * Remove trailing whitespaces
169 char *string_trim_whitespace_right(char *const s)
173 size_t len = strlen(s);
174 char *current = s + len - 1;
176 while (current != s && ISSPACE((unsigned char)*current))
182 current[ISSPACE((unsigned char)*current) ? 0 : 1] = '\0';
189 * string_trim_whitespace:
191 * Remove leading and trailing whitespaces
193 char *string_trim_whitespace(char *const s)
195 string_trim_whitespace_right(s); /* order matters */
196 string_trim_whitespace_left(s);
203 * @dst : pointer to destination buffer.
204 * @dst_size : size of destination buffer.
205 * @src : pointer to input string.
206 * @line_width : max number of characters per line.
207 * @wideglyph_width : not used, but is necessary to keep
208 * compatibility with word_wrap_wideglyph().
209 * @max_lines : max lines of destination string.
212 * Wraps string specified by 'src' to destination buffer
213 * specified by 'dst' and 'dst_size'.
214 * This function assumes that all glyphs in the string
215 * have an on-screen pixel width similar to that of
216 * regular Latin characters - i.e. it will not wrap
217 * correctly any text containing so-called 'wide' Unicode
218 * characters (e.g. CJK languages, emojis, etc.).
221 char *dst, size_t dst_size,
222 const char *src, size_t src_len,
223 int line_width, int wideglyph_width, unsigned max_lines)
225 char *lastspace = NULL;
226 unsigned counter = 0;
228 const char *src_end = src + src_len;
230 /* Prevent buffer overflow */
231 if (dst_size < src_len + 1)
234 /* Early return if src string length is less
236 if (src_len < (size_t)line_width)
238 strlcpy(dst, src, dst_size);
244 unsigned char_len = (unsigned)(utf8skip(src, 1) - src);
248 lastspace = dst; /* Remember the location of the whitespace */
249 else if (*src == '\n')
251 /* If newlines embedded in the input,
256 /* Early return if remaining src string
257 * length is less than line width */
258 if (src_end - src <= line_width)
260 strlcpy(dst, src, dst_size);
268 if (counter >= (unsigned)line_width)
272 if (lastspace && (max_lines == 0 || lines < max_lines))
274 /* Replace nearest (previous) whitespace
275 * with newline character */
279 src -= dst - lastspace - 1;
283 /* Early return if remaining src string
284 * length is less than line width */
285 if (src_end - src < line_width)
287 strlcpy(dst, src, dst_size);
298 * word_wrap_wideglyph:
299 * @dst : pointer to destination buffer.
300 * @dst_size : size of destination buffer.
301 * @src : pointer to input string.
302 * @line_width : max number of characters per line.
303 * @wideglyph_width : effective width of 'wide' Unicode glyphs.
304 * the value here is normalised relative to the
305 * typical on-screen pixel width of a regular
307 * - a regular Latin character is defined to
308 * have an effective width of 100
309 * - wideglyph_width = 100 * (wide_character_pixel_width / latin_character_pixel_width)
310 * - e.g. if 'wide' Unicode characters in 'src'
311 * have an on-screen pixel width twice that of
312 * regular Latin characters, wideglyph_width
314 * @max_lines : max lines of destination string.
317 * Wraps string specified by @src to destination buffer
318 * specified by @dst and @dst_size.
319 * This function assumes that all glyphs in the string
321 * - EITHER 'non-wide' Unicode glyphs, with an on-screen
322 * pixel width similar to that of regular Latin characters
323 * - OR 'wide' Unicode glyphs (e.g. CJK languages, emojis, etc.)
324 * with an on-screen pixel width defined by @wideglyph_width
325 * Note that wrapping may occur in inappropriate locations
326 * if @src string contains 'wide' Unicode characters whose
327 * on-screen pixel width deviates greatly from the set
328 * @wideglyph_width value.
330 void word_wrap_wideglyph(char *dst, size_t dst_size,
331 const char *src, size_t src_len, int line_width,
332 int wideglyph_width, unsigned max_lines)
334 char *lastspace = NULL;
335 char *lastwideglyph = NULL;
336 const char *src_end = src + src_len;
338 /* 'line_width' means max numbers of characters per line,
339 * but this metric is only meaningful when dealing with
340 * 'regular' glyphs that have an on-screen pixel width
341 * similar to that of regular Latin characters.
342 * When handing so-called 'wide' Unicode glyphs, it is
343 * necessary to consider the actual on-screen pixel width
345 * In order to do this, we create a distinction between
346 * regular Latin 'non-wide' glyphs and 'wide' glyphs, and
347 * normalise all values relative to the on-screen pixel
348 * width of regular Latin characters:
349 * - Regular 'non-wide' glyphs have a normalised width of 100
350 * - 'line_width' is therefore normalised to 100 * (width_in_characters)
351 * - 'wide' glyphs have a normalised width of
352 * 100 * (wide_character_pixel_width / latin_character_pixel_width)
353 * - When a character is detected, the position in the current
354 * line is incremented by the regular normalised width of 100
355 * - If that character is then determined to be a 'wide'
356 * glyph, the position in the current line is further incremented
357 * by the difference between the normalised 'wide' and 'non-wide'
359 unsigned counter_normalized = 0;
360 int line_width_normalized = line_width * 100;
361 int additional_counter_normalized = wideglyph_width - 100;
363 /* Early return if src string length is less
365 if (src_end - src < line_width)
367 strlcpy(dst, src, dst_size);
373 unsigned char_len = (unsigned)(utf8skip(src, 1) - src);
374 counter_normalized += 100;
376 /* Prevent buffer overflow */
377 if (char_len >= dst_size)
381 lastspace = dst; /* Remember the location of the whitespace */
382 else if (*src == '\n')
384 /* If newlines embedded in the input,
387 counter_normalized = 0;
389 /* Early return if remaining src string
390 * length is less than line width */
391 if (src_end - src <= line_width)
393 strlcpy(dst, src, dst_size);
397 else if (char_len >= 3)
399 /* Remember the location of the first byte
400 * whose length as UTF-8 >= 3*/
402 counter_normalized += additional_counter_normalized;
405 dst_size -= char_len;
409 if (counter_normalized >= (unsigned)line_width_normalized)
411 counter_normalized = 0;
413 if (max_lines != 0 && lines >= max_lines)
415 else if (lastwideglyph && (!lastspace || lastwideglyph > lastspace))
417 /* Insert newline character */
418 *lastwideglyph = '\n';
420 src -= dst - lastwideglyph;
421 dst = lastwideglyph + 1;
422 lastwideglyph = NULL;
424 /* Early return if remaining src string
425 * length is less than line width */
426 if (src_end - src <= line_width)
428 strlcpy(dst, src, dst_size);
434 /* Replace nearest (previous) whitespace
435 * with newline character */
438 src -= dst - lastspace - 1;
442 /* Early return if remaining src string
443 * length is less than line width */
444 if (src_end - src < line_width)
446 strlcpy(dst, src, dst_size);
459 * Splits string into tokens seperated by @delim
460 * > Returned token string must be free()'d
461 * > Returns NULL if token is not found
462 * > After each call, @str is set to the position after the
464 * > Tokens *include* empty strings
466 * char *str = "1,2,3,4,5,6,7,,,10,";
467 * char **str_ptr = &str;
468 * char *token = NULL;
469 * while ((token = string_tokenize(str_ptr, ",")))
471 * printf("%s\n", token);
476 char* string_tokenize(char **str, const char *delim)
478 /* Taken from https://codereview.stackexchange.com/questions/216956/strtok-function-thread-safe-supports-empty-tokens-doesnt-change-string# */
479 char *str_ptr = NULL;
480 char *delim_ptr = NULL;
482 size_t token_len = 0;
485 if (!str || string_is_empty(delim))
489 /* Note: we don't check string_is_empty() here,
490 * empty strings are valid */
491 if (!(str_ptr = *str))
494 /* Search for delimiter */
495 if ((delim_ptr = strstr(str_ptr, delim)))
496 token_len = delim_ptr - str_ptr;
498 token_len = strlen(str_ptr);
500 /* Allocate token string */
501 if (!(token = (char *)malloc((token_len + 1) * sizeof(char))))
505 strlcpy(token, str_ptr, (token_len + 1) * sizeof(char));
506 token[token_len] = '\0';
508 /* Update input string pointer */
509 *str = delim_ptr ? delim_ptr + strlen(delim) : NULL;
515 * string_remove_all_chars:
516 * @str : input string (must be non-NULL, otherwise UB)
520 * Removes every instance of character @c from @str
522 void string_remove_all_chars(char *str, char c)
524 char *read_ptr = str;
525 char *write_ptr = str;
527 while (*read_ptr != '\0')
529 *write_ptr = *read_ptr++;
538 * string_replace_all_chars:
539 * @str : input string (must be non-NULL, otherwise UB)
540 * @find : character to find
541 * @replace : character to replace @find with
543 * Replaces every instance of character @find in @str
544 * with character @replace
546 void string_replace_all_chars(char *str, char find, char replace)
549 while ((str_ptr = strchr(str_ptr, find)))
550 *str_ptr++ = replace;
554 * string_to_unsigned:
555 * @str : input string
557 * Converts string to unsigned integer.
559 * @return 0 if string is invalid, otherwise > 0
561 unsigned string_to_unsigned(const char *str)
563 const char *ptr = NULL;
565 if (string_is_empty(str))
568 for (ptr = str; *ptr != '\0'; ptr++)
570 if (!ISDIGIT((unsigned char)*ptr))
574 return (unsigned)strtoul(str, NULL, 10);
578 * string_hex_to_unsigned:
579 * @str : input string (must be non-NULL, otherwise UB)
581 * Converts hexadecimal string to unsigned integer.
582 * Handles optional leading '0x'.
584 * @return 0 if string is invalid, otherwise > 0
586 unsigned string_hex_to_unsigned(const char *str)
588 const char *hex_str = str;
589 const char *ptr = NULL;
591 /* Remove leading '0x', if required */
592 if (str[0] != '\0' && str[1] != '\0')
594 if ( (str[0] == '0') &&
599 if (string_is_empty(hex_str))
606 /* Check for valid characters */
607 for (ptr = hex_str; *ptr != '\0'; ptr++)
609 if (!isxdigit((unsigned char)*ptr))
613 return (unsigned)strtoul(hex_str, NULL, 16);
617 * string_count_occurrences_single_character:
621 * Get the total number of occurrences of character @c in @str.
623 * @return Total number of occurrences of character @c
625 int string_count_occurrences_single_character(const char *str, char c)
637 * string_replace_whitespace_with_single_character:
641 * Replaces all spaces with given character @c.
643 void string_replace_whitespace_with_single_character(char *str, char c)
651 * string_replace_multi_space_with_single_space:
655 * Replaces multiple spaces with a single space in a string.
657 void string_replace_multi_space_with_single_space(char *str)
659 char *str_trimmed = str;
660 bool prev_is_space = false;
661 bool curr_is_space = false;
665 curr_is_space = ISSPACE(*str);
666 if (prev_is_space && curr_is_space)
668 *str_trimmed++ = *str;
669 prev_is_space = curr_is_space;
675 * string_remove_all_whitespace:
679 * Remove all spaces from the given string.
681 void string_remove_all_whitespace(char *str_trimmed, const char *str)
685 *str_trimmed++ = *str;
690 * Retrieve the last occurance of the given character in a string.
692 int string_index_last_occurance(const char *str, char c)
694 const char *pos = strrchr(str, c);
696 return (int)(pos - str);
701 * string_find_index_substring_string:
702 * @str : input string (must be non-NULL, otherwise UB)
703 * @substr : substring to find in @str
705 * Find the position of substring @substr in string @str.
707 int string_find_index_substring_string(const char *str, const char *substr)
709 const char *pos = strstr(str, substr);
711 return (int)(pos - str);
716 * string_copy_only_ascii:
720 * Strips non-ASCII characters from a string.
722 void string_copy_only_ascii(char *str_stripped, const char *str)
725 if (*str > 0x1F && *str < 0x7F)
726 *str_stripped++ = *str;
727 *str_stripped = '\0';