string.h 8.9 KB

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