stdstring.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /* Copyright (C) 2010-2019 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. #ifndef __LIBRETRO_SDK_STDSTRING_H
  23. #define __LIBRETRO_SDK_STDSTRING_H
  24. #include <stdlib.h>
  25. #include <stddef.h>
  26. #include <ctype.h>
  27. #include <string.h>
  28. #include <boolean.h>
  29. #include <retro_common_api.h>
  30. #include <retro_inline.h>
  31. #include <compat/strl.h>
  32. RETRO_BEGIN_DECLS
  33. static INLINE bool string_is_empty(const char *data)
  34. {
  35. return !data || (*data == '\0');
  36. }
  37. static INLINE bool string_is_equal(const char *a, const char *b)
  38. {
  39. return (a && b) ? !strcmp(a, b) : false;
  40. }
  41. #define STRLEN_CONST(x) ((sizeof((x))-1))
  42. #define string_is_not_equal(a, b) !string_is_equal((a), (b))
  43. #define string_is_not_equal_fast(a, b, size) (memcmp(a, b, size) != 0)
  44. #define string_is_equal_fast(a, b, size) (memcmp(a, b, size) == 0)
  45. static INLINE bool string_is_equal_case_insensitive(const char *a,
  46. const char *b)
  47. {
  48. int result = 0;
  49. const unsigned char *p1 = (const unsigned char*)a;
  50. const unsigned char *p2 = (const unsigned char*)b;
  51. if (!a || !b)
  52. return false;
  53. if (p1 == p2)
  54. return true;
  55. while ((result = tolower (*p1) - tolower (*p2++)) == 0)
  56. if (*p1++ == '\0')
  57. break;
  58. return (result == 0);
  59. }
  60. static INLINE bool string_is_equal_noncase(const char *a, const char *b)
  61. {
  62. int result = 0;
  63. const unsigned char *p1 = (const unsigned char*)a;
  64. const unsigned char *p2 = (const unsigned char*)b;
  65. if (!a || !b)
  66. return false;
  67. if (p1 == p2)
  68. return false;
  69. while ((result = tolower (*p1) - tolower (*p2++)) == 0)
  70. if (*p1++ == '\0')
  71. break;
  72. return (result == 0);
  73. }
  74. char *string_to_upper(char *s);
  75. char *string_to_lower(char *s);
  76. char *string_ucwords(char *s);
  77. char *string_replace_substring(const char *in, const char *pattern,
  78. const char *by);
  79. /* Remove leading whitespaces */
  80. char *string_trim_whitespace_left(char *const s);
  81. /* Remove trailing whitespaces */
  82. char *string_trim_whitespace_right(char *const s);
  83. /* Remove leading and trailing whitespaces */
  84. char *string_trim_whitespace(char *const s);
  85. /* max_lines == 0 means no limit */
  86. char *word_wrap(char *buffer, const char *string,
  87. int line_width, bool unicode, unsigned max_lines);
  88. /* Splits string into tokens seperated by 'delim'
  89. * > Returned token string must be free()'d
  90. * > Returns NULL if token is not found
  91. * > After each call, 'str' is set to the position after the
  92. * last found token
  93. * > Tokens *include* empty strings
  94. * Usage example:
  95. * char *str = "1,2,3,4,5,6,7,,,10,";
  96. * char **str_ptr = &str;
  97. * char *token = NULL;
  98. * while((token = string_tokenize(str_ptr, ",")))
  99. * {
  100. * printf("%s\n", token);
  101. * free(token);
  102. * token = NULL;
  103. * }
  104. */
  105. char* string_tokenize(char **str, const char *delim);
  106. /* Removes every instance of character 'c' from 'str' */
  107. void string_remove_all_chars(char *str, char c);
  108. /* Replaces every instance of character 'find' in 'str'
  109. * with character 'replace' */
  110. void string_replace_all_chars(char *str, char find, char replace);
  111. /* Converts string to unsigned integer.
  112. * Returns 0 if string is invalid */
  113. unsigned string_to_unsigned(const char *str);
  114. /* Converts hexadecimal string to unsigned integer.
  115. * Handles optional leading '0x'.
  116. * Returns 0 if string is invalid */
  117. unsigned string_hex_to_unsigned(const char *str);
  118. RETRO_END_DECLS
  119. #endif