string.inl - Engine C API Reference

string.inl
  1. #include <ctype.h>
  2. namespace stingray_plugin_foundation {
  3. // ----------------------------------------------------------------------
  4. // C string methods
  5. // ----------------------------------------------------------------------
  6. inline unsigned strlen32(const char *s)
  7. {
  8. return (unsigned)strlen(s);
  9. }
  10. inline unsigned strlenw32(const wchar_t *s)
  11. {
  12. const wchar_t *e = s;
  13. while (*e) ++e;
  14. return (unsigned)(e-s);
  15. }
  16. inline bool strequal(const char *s1, const char *s2)
  17. {
  18. return strcmp(s1, s2) == 0;
  19. }
  20. inline bool strequali(const char *s1, const char *s2)
  21. {
  22. while (*s1 && *s2) {
  23. if (*s1 != *s2) {
  24. if (tolower(*s1) != tolower(*s2))
  25. return false;
  26. }
  27. ++s1; ++s2;
  28. }
  29. return *s1 == *s2;
  30. }
  31. inline bool strempty(const char *s)
  32. {
  33. return *s == 0;
  34. }
  35. inline int to_int(const char *s)
  36. {
  37. return (int)strtol(s, nullptr, 10);
  38. }
  39. inline float to_float(const char *s)
  40. {
  41. return float(strtod(s, nullptr));
  42. }
  43. inline unsigned to_unsigned(const char *s)
  44. {
  45. return (unsigned)strtoul(s, nullptr, 10);
  46. }
  47. inline int to_int(const char *s, bool &error)
  48. {
  49. char *e;
  50. int i = (int)strtol(s, &e, 10);
  51. error = *s == 0 || *e != 0;
  52. return i;
  53. }
  54. inline float to_float(const char *s, bool &error)
  55. {
  56. char *e;
  57. float f = float(strtod(s, &e));
  58. error = *s == 0 || *e != 0;
  59. return f;
  60. }
  61. inline unsigned to_unsigned(const char *s, bool &error)
  62. {
  63. char *e;
  64. unsigned u = (unsigned)strtoul(s, &e, 10);
  65. error = *s == 0 || *e != 0;
  66. return u;
  67. }
  68. // ----------------------------------------------------------------------
  69. // ConstString
  70. // ----------------------------------------------------------------------
  71. // ----------------------------------------------------------------------
  72. // DynamicString
  73. // ----------------------------------------------------------------------
  74. inline void DynamicString::swap(DynamicString &other)
  75. {
  76. _buffer.swap(other._buffer);
  77. }
  78. inline void append(DynamicString &str, const char *s)
  79. {
  80. str.extend(strlen32(s));
  81. strcat(str.c_str(), s);
  82. }
  83. inline void append(DynamicString &str, const char *s, unsigned len)
  84. {
  85. str.extend(len);
  86. strncat(str.c_str(), s, len);
  87. }
  88. inline void append(DynamicString &str, char c)
  89. {
  90. str.extend(1);
  91. str.end()[-1] = c;
  92. }
  93. inline void prefix(DynamicString &str, const char *s)
  94. {
  95. unsigned len = strlen32(s);
  96. str.extend(len);
  97. memmove(str.buffer().begin() + len, str.buffer().begin(), str.buffer().size() - len);
  98. memcpy(str.buffer().begin(), s, len);
  99. }
  100. // ----------------------------------------------------------------------
  101. // string methods
  102. // ----------------------------------------------------------------------
  103. inline bool string::is_whitespace(char c)
  104. {
  105. return c == '\t' || c == ' ' || c == '\n' || c == '\r';
  106. }
  107. inline const char *string::spaces(int n)
  108. {
  109. const char *s =
  110. " " " " " " " " " "
  111. " " " " " " " " " "
  112. " " " " " " " " " "
  113. " " " " " " " " " ";
  114. if (n > 200)
  115. n = 200;
  116. return s + 200 - n;
  117. }
  118. inline bool string::contains(const char *s, const char *substring)
  119. {
  120. return string::find(s, substring) != string::npos;
  121. }
  122. inline unsigned string::find_last(const char *s, char c)
  123. {
  124. unsigned n = strlen32(s);
  125. for (int i=n - 1; i >= 0; --i) {
  126. if (s[i] == c)
  127. return i;
  128. }
  129. return npos;
  130. }
  131. inline unsigned string::find(const char *s, char c)
  132. {
  133. unsigned n = strlen32(s);
  134. for (unsigned i=0; i<=n; ++i) {
  135. if (s[i] == c)
  136. return i;
  137. }
  138. return npos;
  139. }
  140. inline unsigned string::find(const char *s, const char *substring)
  141. {
  142. unsigned n = strlen32(s) - strlen32(substring);
  143. if (n > 0x80000000)
  144. return npos;
  145. for (unsigned i=0; i<=n; ++i) {
  146. for (unsigned j=0;; ++j) {
  147. if (substring[j] == 0)
  148. return i;
  149. if (s[i+j] != substring[j])
  150. break;
  151. }
  152. }
  153. return npos;
  154. }
  155. inline void string::split(const char *s, const char *split_on, DynamicString &first, DynamicString &second)
  156. {
  157. unsigned pos = find(s, split_on);
  158. if (pos == npos) {
  159. first = s;
  160. second.clear();
  161. } else {
  162. unsigned s_n = strlen32(s);
  163. unsigned split_on_n = strlen32(split_on);
  164. first.resize(pos);
  165. memmove(&first[0], &s[0], pos);
  166. second.resize(s_n - pos - split_on_n);
  167. memmove(&second[0], &s[pos + split_on_n], s_n - pos - split_on_n);
  168. }
  169. }
  170. // Cannot be defined in plugin_foundation at the moment. The Scaleform plugin
  171. // includes string.h, and the plugin also removes the PS4 platform define,
  172. // leading to __forceinline being undefined when compiling PS4. A fix is in the
  173. // works. For now, defining this function in Wwise plugin (its only use)
  174. // inline void string::split(const char *s, const char *split_by, Vector<DynamicString> &result)
  175. // {
  176. // Allocator & allocator = result.allocator();
  177. // DynamicString a(allocator), b(allocator);
  178. // string::split(s, split_by, a, b);
  179. // while( !a.empty() ) {
  180. // result.resize(result.size() + 1);
  181. // result.back() = a;
  182. // if( b.empty() )
  183. // break;
  184. // DynamicString temp = b;
  185. // string::split(temp.c_str(), split_by, a, b);
  186. // }
  187. // }
  188. // ----------------------------------------------------------------------
  189. } // namespace stingray_plugin_foundation