string.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. /*
  2. * Copyright 2010-2023 Branimir Karadzic. All rights reserved.
  3. * License: https://github.com/bkaradzic/bx/blob/master/LICENSE
  4. */
  5. #ifndef BX_STRING_H_HEADER_GUARD
  6. #define BX_STRING_H_HEADER_GUARD
  7. #include "allocator.h"
  8. namespace bx
  9. {
  10. /// Units
  11. struct Units
  12. {
  13. enum Enum //!< Units:
  14. {
  15. Kilo, //!< SI units
  16. Kibi, //!< IEC prefix
  17. };
  18. };
  19. /// Non-zero-terminated string view.
  20. ///
  21. class StringView
  22. {
  23. public:
  24. ///
  25. StringView();
  26. ///
  27. StringView(const StringView& _rhs, int32_t _start = 0, int32_t _len = INT32_MAX);
  28. ///
  29. StringView& operator=(const char* _rhs);
  30. ///
  31. StringView& operator=(const StringView& _rhs);
  32. ///
  33. StringView(const char* _ptr);
  34. ///
  35. StringView(const char* _ptr, int32_t _len);
  36. ///
  37. StringView(const char* _ptr, const char* _term);
  38. ///
  39. void set(const char* _ptr);
  40. ///
  41. void set(const char* _ptr, int32_t _len);
  42. ///
  43. void set(const char* _ptr, const char* _term);
  44. ///
  45. void set(const StringView& _str, int32_t _start = 0, int32_t _len = INT32_MAX);
  46. ///
  47. void clear();
  48. /// Returns pointer to non-terminated string.
  49. ///
  50. /// @attention Use of this pointer in standard C/C++ functions is not safe. You must use it
  51. /// in conjunction with `getTerm()` or getLength()`.
  52. ///
  53. const char* getPtr() const;
  54. /// Returns pointer past last character in string view.
  55. ///
  56. /// @attention Dereferencing this pointer is not safe.
  57. ///
  58. const char* getTerm() const;
  59. /// Returns `true` if string is empty.
  60. bool isEmpty() const;
  61. /// Returns string length.
  62. int32_t getLength() const;
  63. /// Returns `true` if string is zero terminated.
  64. bool is0Terminated() const;
  65. protected:
  66. const char* m_ptr;
  67. int32_t m_len;
  68. bool m_0terminated;
  69. };
  70. /// ASCII string
  71. template<bx::AllocatorI** AllocatorT>
  72. class StringT : public StringView
  73. {
  74. public:
  75. ///
  76. StringT();
  77. ///
  78. StringT(const StringT<AllocatorT>& _rhs);
  79. ///
  80. StringT(const StringView& _rhs);
  81. ///
  82. ~StringT();
  83. ///
  84. StringT<AllocatorT>& operator=(const StringT<AllocatorT>& _rhs);
  85. ///
  86. void set(const StringView& _str);
  87. ///
  88. void append(const StringView& _str);
  89. ///
  90. void append(const char* _ptr, const char* _term);
  91. ///
  92. void clear();
  93. /// Returns zero-terminated C string pointer.
  94. ///
  95. const char* getCPtr() const;
  96. protected:
  97. int32_t m_capacity;
  98. };
  99. /// Returns true if character is part of white space set.
  100. ///
  101. /// White space set is:
  102. /// ' ' - Space.
  103. /// '\t' - Horizontal tab.
  104. /// '\n' - Line feed / new line.
  105. /// '\r' - Carriage return.
  106. /// '\v' - Vertical tab.
  107. /// '\f' - Form feed / new page.
  108. ///
  109. bool isSpace(char _ch);
  110. /// Returns true if string view contains only space characters.
  111. bool isSpace(const StringView& _str);
  112. /// Returns true if character is uppercase.
  113. bool isUpper(char _ch);
  114. /// Returns true if string view contains only uppercase characters.
  115. bool isUpper(const StringView& _str);
  116. /// Returns true if character is lowercase.
  117. bool isLower(char _ch);
  118. /// Returns true if string view contains only lowercase characters.
  119. bool isLower(const StringView& _str);
  120. /// Returns true if character is part of alphabet set.
  121. bool isAlpha(char _ch);
  122. /// Returns true if string view contains only alphabet characters.
  123. bool isAlpha(const StringView& _str);
  124. /// Returns true if character is part of numeric set.
  125. bool isNumeric(char _ch);
  126. /// Returns true if string view contains only numeric characters.
  127. bool isNumeric(const StringView& _str);
  128. /// Returns true if character is part of alpha numeric set.
  129. bool isAlphaNum(char _ch);
  130. /// Returns true if string view contains only alphanumeric characters.
  131. bool isAlphaNum(const StringView& _str);
  132. /// Returns true if character is part of hexadecimal set.
  133. bool isHexNum(char _ch);
  134. /// Returns true if string view contains only hexadecimal characters.
  135. bool isHexNum(const StringView& _str);
  136. /// Returns true if character is printable.
  137. bool isPrint(char _ch);
  138. /// Returns true if string vieww contains only printable characters.
  139. bool isPrint(const StringView& _str);
  140. /// Returns lower case character representing _ch.
  141. char toLower(char _ch);
  142. /// Lower case string in place assuming length passed is valid.
  143. void toLowerUnsafe(char* _inOutStr, int32_t _len);
  144. /// Lower case string in place.
  145. void toLower(char* _inOutStr, int32_t _max = INT32_MAX);
  146. /// Returns upper case character representing _ch.
  147. char toUpper(char _ch);
  148. /// Upper case string in place assuming length passed is valid.
  149. void toUpperUnsafe(char* _inOutStr, int32_t _len);
  150. /// Uppre case string in place.
  151. void toUpper(char* _inOutStr, int32_t _max = INT32_MAX);
  152. /// String compare.
  153. int32_t strCmp(const StringView& _lhs, const StringView& _rhs, int32_t _max = INT32_MAX);
  154. /// Case insensitive string compare.
  155. int32_t strCmpI(const StringView& _lhs, const StringView& _rhs, int32_t _max = INT32_MAX);
  156. // Compare as strings holding indices/version numbers.
  157. int32_t strCmpV(const StringView& _lhs, const StringView& _rhs, int32_t _max = INT32_MAX);
  158. /// Get string length.
  159. int32_t strLen(const char* _str, int32_t _max = INT32_MAX);
  160. /// Get string length.
  161. int32_t strLen(const StringView& _str, int32_t _max = INT32_MAX);
  162. /// Copy _num characters from string _src to _dst buffer of maximum _dstSize capacity
  163. /// including zero terminator. Copy will be terminated with '\0'.
  164. int32_t strCopy(char* _dst, int32_t _dstSize, const StringView& _str, int32_t _num = INT32_MAX);
  165. /// Concatenate string.
  166. int32_t strCat(char* _dst, int32_t _dstSize, const StringView& _str, int32_t _num = INT32_MAX);
  167. /// Test whether the string _str begins with prefix.
  168. bool hasPrefix(const StringView& _str, const StringView& _prefix);
  169. /// Test whether the string _str ends with suffix.
  170. bool hasSuffix(const StringView& _str, const StringView& _suffix);
  171. /// Find character in string. Limit search to _max characters.
  172. StringView strFind(const StringView& _str, char _ch);
  173. /// Find character in string in reverse. Limit search to _max characters.
  174. StringView strRFind(const StringView& _str, char _ch);
  175. /// Find substring in string. Limit search to _max characters.
  176. StringView strFind(const StringView& _str, const StringView& _find, int32_t _num = INT32_MAX);
  177. /// Find substring in string. Case insensitive. Limit search to _max characters.
  178. StringView strFindI(const StringView& _str, const StringView& _find, int32_t _num = INT32_MAX);
  179. /// Returns string view with characters _chars trimmed from left.
  180. StringView strLTrim(const StringView& _str, const StringView& _chars);
  181. /// Returns string view with whitespace characters trimmed from left.
  182. StringView strLTrimSpace(const StringView& _str);
  183. /// Returns string view with non-whitespace characters trimmed from left.
  184. StringView strLTrimNonSpace(const StringView& _str);
  185. /// Returns string view with characters _chars trimmed from right.
  186. StringView strRTrim(const StringView& _str, const StringView& _chars);
  187. /// Returns string view with whitespace characters trimmed from right.
  188. StringView strRTrimSpace(const StringView& _str);
  189. /// Returns string view with characters _chars trimmed from left and right.
  190. StringView strTrim(const StringView& _str, const StringView& _chars);
  191. /// Returns string view with whitespace characters trimmed from left and right.
  192. StringView strTrimSpace(const StringView& _str);
  193. /// Returns string view with prefix trimmed.
  194. StringView strTrimPrefix(const StringView& _str, const StringView& _prefix);
  195. /// Returns string view with suffix trimmed.
  196. StringView strTrimSuffix(const StringView& _str, const StringView& _suffix);
  197. /// Find new line. Returns pointer after new line terminator.
  198. StringView strFindNl(const StringView& _str);
  199. /// Find end of line. Returns pointer to new line terminator.
  200. StringView strFindEol(const StringView& _str);
  201. /// Returns StringView of word or empty.
  202. StringView strWord(const StringView& _str);
  203. /// Returns substring in string.
  204. StringView strSubstr(const StringView& _str, int32_t _start, int32_t _len = INT32_MAX);
  205. /// Find matching block.
  206. StringView strFindBlock(const StringView& _str, char _open, char _close);
  207. // Normalize string to sane line endings.
  208. StringView normalizeEolLf(char* _out, int32_t _size, const StringView& _str);
  209. // Finds identifier.
  210. StringView findIdentifierMatch(const StringView& _str, const StringView& _word);
  211. /// Finds any identifier from NULL terminated array of identifiers.
  212. StringView findIdentifierMatch(const StringView& _str, const char** _words, int32_t _num = INT32_MAX);
  213. /// Cross platform implementation of vsnprintf that returns number of
  214. /// characters which would have been written to the final string if
  215. /// enough space had been available.
  216. int32_t vsnprintf(char* _out, int32_t _max, const char* _format, va_list _argList);
  217. /// Cross platform implementation of snprintf that returns number of
  218. /// characters which would have been written to the final string if
  219. /// enough space had been available.
  220. int32_t snprintf(char* _out, int32_t _max, const char* _format, ...);
  221. ///
  222. int32_t vprintf(const char* _format, va_list _argList);
  223. ///
  224. int32_t printf(const char* _format, ...);
  225. /// Templatized snprintf.
  226. template <typename Ty>
  227. void stringPrintfVargs(Ty& _out, const char* _format, va_list _argList);
  228. /// Templatized snprintf.
  229. template <typename Ty>
  230. void stringPrintf(Ty& _out, const char* _format, ...);
  231. /// Convert size in bytes to human readable string kibi units.
  232. int32_t prettify(char* _out, int32_t _count, uint64_t _value, Units::Enum _units = Units::Kibi);
  233. /// Converts bool value to string.
  234. int32_t toString(char* _out, int32_t _max, bool _value);
  235. /// Converts double value to string.
  236. int32_t toString(char* _out, int32_t _max, double _value);
  237. /// Converts 32-bit integer value to string.
  238. int32_t toString(char* _out, int32_t _max, int32_t _value, uint32_t _base = 10, char _separator = '\0');
  239. /// Converts 64-bit integer value to string.
  240. int32_t toString(char* _out, int32_t _max, int64_t _value, uint32_t _base = 10, char _separator = '\0');
  241. /// Converts 32-bit unsigned integer value to string.
  242. int32_t toString(char* _out, int32_t _max, uint32_t _value, uint32_t _base = 10, char _separator = '\0');
  243. /// Converts 64-bit unsigned integer value to string.
  244. int32_t toString(char* _out, int32_t _max, uint64_t _value, uint32_t _base = 10, char _separator = '\0');
  245. /// Converts string to bool value.
  246. bool fromString(bool* _out, const StringView& _str);
  247. /// Converts string to float value.
  248. bool fromString(float* _out, const StringView& _str);
  249. /// Converts string to double value.
  250. bool fromString(double* _out, const StringView& _str);
  251. /// Converts string to 32-bit integer value.
  252. bool fromString(int32_t* _out, const StringView& _str);
  253. /// Converts string to 32-bit unsigned integer value.
  254. bool fromString(uint32_t* _out, const StringView& _str);
  255. ///
  256. class LineReader
  257. {
  258. public:
  259. ///
  260. LineReader(const StringView& _str);
  261. ///
  262. void reset();
  263. ///
  264. StringView next();
  265. ///
  266. bool isDone() const;
  267. ///
  268. uint32_t getLine() const;
  269. private:
  270. const StringView m_str;
  271. StringView m_curr;
  272. uint32_t m_line;
  273. };
  274. } // namespace bx
  275. #include "inline/string.inl"
  276. #endif // BX_STRING_H_HEADER_GUARD