string.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. /*
  2. * Copyright 2010-2019 Branimir Karadzic. All rights reserved.
  3. * License: https://github.com/bkaradzic/bx#license-bsd-2-clause
  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. class StringView
  21. {
  22. public:
  23. ///
  24. StringView();
  25. ///
  26. StringView(const StringView& _rhs, int32_t _start = 0, int32_t _len = INT32_MAX);
  27. ///
  28. StringView& operator=(const char* _rhs);
  29. ///
  30. StringView& operator=(const StringView& _rhs);
  31. ///
  32. StringView(char* _ptr);
  33. ///
  34. StringView(const char* _ptr);
  35. ///
  36. StringView(char* _ptr, int32_t _len);
  37. ///
  38. StringView(const char* _ptr, int32_t _len);
  39. ///
  40. StringView(const char* _ptr, const char* _term);
  41. ///
  42. template<typename Ty>
  43. explicit StringView(const Ty& _container);
  44. ///
  45. void set(char* _ptr);
  46. ///
  47. void set(const char* _ptr);
  48. ///
  49. void set(const char* _ptr, int32_t _len);
  50. ///
  51. void set(const char* _ptr, const char* _term);
  52. ///
  53. void set(const StringView& _str, int32_t _start = 0, int32_t _len = INT32_MAX);
  54. ///
  55. template<typename Ty>
  56. void set(const Ty& _container);
  57. ///
  58. void clear();
  59. ///
  60. const char* getPtr() const;
  61. ///
  62. const char* getTerm() const;
  63. ///
  64. bool isEmpty() const;
  65. ///
  66. int32_t getLength() const;
  67. protected:
  68. const char* m_ptr;
  69. int32_t m_len;
  70. };
  71. /// ASCII string
  72. template<bx::AllocatorI** AllocatorT>
  73. class StringT : public StringView
  74. {
  75. public:
  76. ///
  77. StringT();
  78. ///
  79. StringT(const StringT<AllocatorT>& _rhs);
  80. ///
  81. StringT<AllocatorT>& operator=(const StringT<AllocatorT>& _rhs);
  82. ///
  83. StringT(const StringView& _rhs);
  84. ///
  85. ~StringT();
  86. ///
  87. void set(const StringView& _str);
  88. ///
  89. void append(const StringView& _str);
  90. ///
  91. void clear();
  92. };
  93. /// Retruns true if character is part of space set.
  94. bool isSpace(char _ch);
  95. /// Returns true if string view contains only space characters.
  96. bool isSpace(const StringView& _str);
  97. /// Retruns true if character is uppercase.
  98. bool isUpper(char _ch);
  99. /// Returns true if string view contains only uppercase characters.
  100. bool isUpper(const StringView& _str);
  101. /// Retruns true if character is lowercase.
  102. bool isLower(char _ch);
  103. /// Returns true if string view contains only lowercase characters.
  104. bool isLower(const StringView& _str);
  105. /// Returns true if character is part of alphabet set.
  106. bool isAlpha(char _ch);
  107. /// Retruns true if string view contains only alphabet characters.
  108. bool isAlpha(const StringView& _str);
  109. /// Returns true if character is part of numeric set.
  110. bool isNumeric(char _ch);
  111. /// Retruns true if string view contains only numeric characters.
  112. bool isNumeric(const StringView& _str);
  113. /// Returns true if character is part of alpha numeric set.
  114. bool isAlphaNum(char _ch);
  115. /// Returns true if string view contains only alpha-numeric characters.
  116. bool isAlphaNum(const StringView& _str);
  117. /// Returns true if character is part of hexadecimal set.
  118. bool isHexNum(char _ch);
  119. /// Returns true if string view contains only hexadecimal characters.
  120. bool isHexNum(const StringView& _str);
  121. /// Returns true if character is printable.
  122. bool isPrint(char _ch);
  123. /// Returns true if string vieww contains only printable characters.
  124. bool isPrint(const StringView& _str);
  125. /// Retruns lower case character representing _ch.
  126. char toLower(char _ch);
  127. /// Lower case string in place assuming length passed is valid.
  128. void toLowerUnsafe(char* _inOutStr, int32_t _len);
  129. /// Lower case string in place.
  130. void toLower(char* _inOutStr, int32_t _max = INT32_MAX);
  131. /// Returns upper case character representing _ch.
  132. char toUpper(char _ch);
  133. /// Upper case string in place assuming length passed is valid.
  134. void toUpperUnsafe(char* _inOutStr, int32_t _len);
  135. /// Uppre case string in place.
  136. void toUpper(char* _inOutStr, int32_t _max = INT32_MAX);
  137. /// String compare.
  138. int32_t strCmp(const StringView& _lhs, const StringView& _rhs, int32_t _max = INT32_MAX);
  139. /// Case insensitive string compare.
  140. int32_t strCmpI(const StringView& _lhs, const StringView& _rhs, int32_t _max = INT32_MAX);
  141. // Compare as strings holding indices/version numbers.
  142. int32_t strCmpV(const StringView& _lhs, const StringView& _rhs, int32_t _max = INT32_MAX);
  143. /// Get string length.
  144. int32_t strLen(const char* _str, int32_t _max = INT32_MAX);
  145. /// Get string length.
  146. int32_t strLen(const StringView& _str, int32_t _max = INT32_MAX);
  147. /// Copy _num characters from string _src to _dst buffer of maximum _dstSize capacity
  148. /// including zero terminator. Copy will be terminated with '\0'.
  149. int32_t strCopy(char* _dst, int32_t _dstSize, const StringView& _str, int32_t _num = INT32_MAX);
  150. /// Concatinate string.
  151. int32_t strCat(char* _dst, int32_t _dstSize, const StringView& _str, int32_t _num = INT32_MAX);
  152. /// Find character in string. Limit search to _max characters.
  153. StringView strFind(const StringView& _str, char _ch);
  154. /// Find character in string in reverse. Limit search to _max characters.
  155. StringView strRFind(const StringView& _str, char _ch);
  156. /// Find substring in string. Limit search to _max characters.
  157. StringView strFind(const StringView& _str, const StringView& _find, int32_t _num = INT32_MAX);
  158. /// Find substring in string. Case insensitive. Limit search to _max characters.
  159. StringView strFindI(const StringView& _str, const StringView& _find, int32_t _num = INT32_MAX);
  160. /// Returns string view with characters _chars trimmed from left.
  161. StringView strLTrim(const StringView& _str, const StringView& _chars);
  162. /// Returns string view with whitespace characters trimmed from left.
  163. StringView strLTrimSpace(const StringView& _str);
  164. /// Returns string view with non-whitespace characters trimmed from left.
  165. StringView strLTrimNonSpace(const StringView& _str);
  166. /// Returns string view with characters _chars trimmed from right.
  167. StringView strRTrim(const StringView& _str, const StringView& _chars);
  168. /// Returns string view with characters _chars trimmed from left and right.
  169. StringView strTrim(const StringView& _str, const StringView& _chars);
  170. /// Find new line. Returns pointer after new line terminator.
  171. StringView strFindNl(const StringView& _str);
  172. /// Find end of line. Retuns pointer to new line terminator.
  173. StringView strFindEol(const StringView& _str);
  174. /// Returns StringView of word or empty.
  175. StringView strWord(const StringView& _str);
  176. /// Returns substring in string.
  177. StringView strSubstr(const StringView& _str, int32_t _start, int32_t _len = INT32_MAX);
  178. /// Find matching block.
  179. StringView strFindBlock(const StringView& _str, char _open, char _close);
  180. // Normalize string to sane line endings.
  181. StringView normalizeEolLf(char* _out, int32_t _size, const StringView& _str);
  182. // Finds identifier.
  183. StringView findIdentifierMatch(const StringView& _str, const StringView& _word);
  184. /// Finds any identifier from NULL terminated array of identifiers.
  185. StringView findIdentifierMatch(const StringView& _str, const char** _words, int32_t _num = INT32_MAX);
  186. /// Cross platform implementation of vsnprintf that returns number of
  187. /// characters which would have been written to the final string if
  188. /// enough space had been available.
  189. int32_t vsnprintf(char* _out, int32_t _max, const char* _format, va_list _argList);
  190. /// Cross platform implementation of snprintf that returns number of
  191. /// characters which would have been written to the final string if
  192. /// enough space had been available.
  193. int32_t snprintf(char* _out, int32_t _max, const char* _format, ...);
  194. /// Templatized snprintf.
  195. template <typename Ty>
  196. void stringPrintfVargs(Ty& _out, const char* _format, va_list _argList);
  197. /// Templatized snprintf.
  198. template <typename Ty>
  199. void stringPrintf(Ty& _out, const char* _format, ...);
  200. /// Replace all instances of substring.
  201. template <typename Ty>
  202. Ty replaceAll(const Ty& _str, const char* _from, const char* _to);
  203. /// Convert size in bytes to human readable string kibi units.
  204. int32_t prettify(char* _out, int32_t _count, uint64_t _value, Units::Enum _units = Units::Kibi);
  205. /// Converts bool value to string.
  206. int32_t toString(char* _out, int32_t _max, bool _value);
  207. /// Converts double value to string.
  208. int32_t toString(char* _out, int32_t _max, double _value);
  209. /// Converts 32-bit integer value to string.
  210. int32_t toString(char* _out, int32_t _max, int32_t _value, uint32_t _base = 10, char _separator = '\0');
  211. /// Converts 64-bit integer value to string.
  212. int32_t toString(char* _out, int32_t _max, int64_t _value, uint32_t _base = 10, char _separator = '\0');
  213. /// Converts 32-bit unsigned integer value to string.
  214. int32_t toString(char* _out, int32_t _max, uint32_t _value, uint32_t _base = 10, char _separator = '\0');
  215. /// Converts 64-bit unsigned integer value to string.
  216. int32_t toString(char* _out, int32_t _max, uint64_t _value, uint32_t _base = 10, char _separator = '\0');
  217. /// Converts string to bool value.
  218. bool fromString(bool* _out, const StringView& _str);
  219. /// Converts string to float value.
  220. bool fromString(float* _out, const StringView& _str);
  221. /// Converts string to double value.
  222. bool fromString(double* _out, const StringView& _str);
  223. /// Converts string to 32-bit integer value.
  224. bool fromString(int32_t* _out, const StringView& _str);
  225. /// Converts string to 32-bit unsigned integer value.
  226. bool fromString(uint32_t* _out, const StringView& _str);
  227. ///
  228. class LineReader
  229. {
  230. public:
  231. ///
  232. LineReader(const bx::StringView& _str);
  233. ///
  234. void reset();
  235. ///
  236. StringView next();
  237. ///
  238. bool isDone() const;
  239. ///
  240. uint32_t getLine() const;
  241. private:
  242. const bx::StringView m_str;
  243. bx::StringView m_curr;
  244. uint32_t m_line;
  245. };
  246. } // namespace bx
  247. #include "inline/string.inl"
  248. #endif // BX_STRING_H_HEADER_GUARD