string.inl 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. # error "Must be included from bx/string.h!"
  7. #endif // BX_STRING_H_HEADER_GUARD
  8. #if BX_CRT_MSVC && !defined(va_copy)
  9. # define va_copy(_a, _b) (_a) = (_b)
  10. #endif // BX_CRT_MSVC && !defined(va_copy)
  11. namespace bx
  12. {
  13. template <typename Ty>
  14. inline void stringPrintfVargs(Ty& _out, const char* _format, va_list _argList)
  15. {
  16. char temp[2048];
  17. char* out = temp;
  18. int32_t len = vsnprintf(out, sizeof(temp), _format, _argList);
  19. if (int32_t(sizeof(temp) ) < len)
  20. {
  21. out = (char*)alloca(len);
  22. len = vsnprintf(out, len, _format, _argList);
  23. }
  24. _out.append(out, out+len);
  25. }
  26. template <typename Ty>
  27. inline void stringPrintf(Ty& _out, const char* _format, ...)
  28. {
  29. va_list argList;
  30. va_start(argList, _format);
  31. stringPrintfVargs(_out, _format, argList);
  32. va_end(argList);
  33. }
  34. template <typename Ty>
  35. inline Ty replaceAll(const Ty& _str, const char* _from, const char* _to)
  36. {
  37. Ty str = _str;
  38. typename Ty::size_type startPos = 0;
  39. const typename Ty::size_type fromLen = strLen(_from);
  40. const typename Ty::size_type toLen = strLen(_to);
  41. while ( (startPos = str.find(_from, startPos) ) != Ty::npos)
  42. {
  43. str.replace(startPos, fromLen, _to);
  44. startPos += toLen;
  45. }
  46. return str;
  47. }
  48. inline StringView::StringView()
  49. {
  50. clear();
  51. }
  52. inline StringView::StringView(const StringView& _rhs)
  53. {
  54. set(_rhs.m_ptr, _rhs.m_len);
  55. }
  56. inline StringView& StringView::operator=(const char* _rhs)
  57. {
  58. set(_rhs);
  59. return *this;
  60. }
  61. inline StringView& StringView::operator=(const StringView& _rhs)
  62. {
  63. set(_rhs.m_ptr, _rhs.m_len);
  64. return *this;
  65. }
  66. inline StringView::StringView(const char* _ptr, int32_t _len)
  67. {
  68. set(_ptr, _len);
  69. }
  70. inline StringView::StringView(const char* _ptr, const char* _term)
  71. {
  72. set(_ptr, _term);
  73. }
  74. template<typename Ty>
  75. inline StringView::StringView(const Ty& _container)
  76. {
  77. set(_container);
  78. }
  79. inline void StringView::set(const char* _ptr, int32_t _len)
  80. {
  81. clear();
  82. if (NULL != _ptr)
  83. {
  84. int32_t len = strLen(_ptr, _len);
  85. if (0 != len)
  86. {
  87. m_len = len;
  88. m_ptr = _ptr;
  89. }
  90. }
  91. }
  92. inline void StringView::set(const char* _ptr, const char* _term)
  93. {
  94. set(_ptr, int32_t(_term-_ptr) );
  95. }
  96. template<typename Ty>
  97. inline void StringView::set(const Ty& _container)
  98. {
  99. set(_container.data(), _container.length() );
  100. }
  101. inline void StringView::set(const StringView& _str)
  102. {
  103. set(_str.m_ptr, _str.m_len);
  104. }
  105. inline void StringView::clear()
  106. {
  107. m_ptr = "";
  108. m_len = 0;
  109. }
  110. inline const char* StringView::getPtr() const
  111. {
  112. return m_ptr;
  113. }
  114. inline const char* StringView::getTerm() const
  115. {
  116. return m_ptr + m_len;
  117. }
  118. inline bool StringView::isEmpty() const
  119. {
  120. return 0 == m_len;
  121. }
  122. inline int32_t StringView::getLength() const
  123. {
  124. return m_len;
  125. }
  126. template<bx::AllocatorI** AllocatorT>
  127. inline StringT<AllocatorT>::StringT()
  128. : StringView()
  129. {
  130. }
  131. template<bx::AllocatorI** AllocatorT>
  132. inline StringT<AllocatorT>::StringT(const StringT<AllocatorT>& _rhs)
  133. : StringView()
  134. {
  135. set(_rhs);
  136. }
  137. template<bx::AllocatorI** AllocatorT>
  138. inline StringT<AllocatorT>& StringT<AllocatorT>::operator=(const StringT<AllocatorT>& _rhs)
  139. {
  140. set(_rhs);
  141. return *this;
  142. }
  143. template<bx::AllocatorI** AllocatorT>
  144. inline StringT<AllocatorT>::StringT(const StringView& _rhs)
  145. {
  146. set(_rhs);
  147. }
  148. template<bx::AllocatorI** AllocatorT>
  149. inline StringT<AllocatorT>::~StringT()
  150. {
  151. clear();
  152. }
  153. template<bx::AllocatorI** AllocatorT>
  154. inline void StringT<AllocatorT>::set(const StringView& _str)
  155. {
  156. clear();
  157. append(_str);
  158. }
  159. template<bx::AllocatorI** AllocatorT>
  160. inline void StringT<AllocatorT>::append(const StringView& _str)
  161. {
  162. if (0 != _str.getLength() )
  163. {
  164. int32_t old = m_len;
  165. int32_t len = m_len + strLen(_str);
  166. char* ptr = (char*)BX_REALLOC(*AllocatorT, 0 != m_len ? const_cast<char*>(m_ptr) : NULL, len+1);
  167. m_len = len;
  168. strCopy(ptr + old, len-old+1, _str);
  169. *const_cast<char**>(&m_ptr) = ptr;
  170. }
  171. }
  172. template<bx::AllocatorI** AllocatorT>
  173. inline void StringT<AllocatorT>::clear()
  174. {
  175. if (0 != m_len)
  176. {
  177. BX_FREE(*AllocatorT, const_cast<char*>(m_ptr) );
  178. StringView::clear();
  179. }
  180. }
  181. } // namespace bx