StrBuffer.hpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. /***************************************************************************
  2. *
  3. * Project _____ __ ____ _ _
  4. * ( _ ) /__\ (_ _)_| |_ _| |_
  5. * )(_)( /(__)\ )( (_ _)(_ _)
  6. * (_____)(__)(__)(__) |_| |_|
  7. *
  8. *
  9. * Copyright 2018-present, Leonid Stryzhevskyi <lganzzzo@gmail.com>
  10. *
  11. * Licensed under the Apache License, Version 2.0 (the "License");
  12. * you may not use this file except in compliance with the License.
  13. * You may obtain a copy of the License at
  14. *
  15. * http://www.apache.org/licenses/LICENSE-2.0
  16. *
  17. * Unless required by applicable law or agreed to in writing, software
  18. * distributed under the License is distributed on an "AS IS" BASIS,
  19. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  20. * See the License for the specific language governing permissions and
  21. * limitations under the License.
  22. *
  23. ***************************************************************************/
  24. #ifndef oatpp_base_StrBuffer_hpp
  25. #define oatpp_base_StrBuffer_hpp
  26. #include "memory/ObjectPool.hpp"
  27. #include "./Countable.hpp"
  28. #include <cstring> // c
  29. namespace oatpp { namespace base {
  30. /**
  31. * String buffer class.
  32. */
  33. class StrBuffer : public oatpp::base::Countable {
  34. private:
  35. static constexpr v_buff_size SM_STRING_POOL_ENTRY_SIZE = 256;
  36. static memory::ThreadDistributedMemoryPool& getSmallStringPool() {
  37. static auto pool = new memory::ThreadDistributedMemoryPool("Small_String_Pool", SM_STRING_POOL_ENTRY_SIZE, 16);
  38. return *pool;
  39. }
  40. static v_buff_size getSmStringBaseSize() {
  41. memory::AllocationExtras extras(0);
  42. auto ptr = memory::customPoolAllocateSharedWithExtras<StrBuffer>(extras, getSmallStringPool());
  43. return extras.baseSize;
  44. }
  45. static v_buff_size getSmStringSize() {
  46. static v_buff_size size = SM_STRING_POOL_ENTRY_SIZE - getSmStringBaseSize();
  47. return size;
  48. }
  49. private:
  50. p_char8 m_data;
  51. v_buff_size m_size;
  52. bool m_hasOwnData;
  53. private:
  54. void set(const void* data, v_buff_size size, bool hasOwnData);
  55. void setAndCopy(const void* data, const void* originData, v_buff_size size);
  56. static std::shared_ptr<StrBuffer> allocShared(const void* data, v_buff_size size, bool copyAsOwnData);
  57. /*
  58. * Allocate memory for string or use originData<br>
  59. * if copyAsOwnData == false return originData
  60. */
  61. static p_char8 allocStrBuffer(const void* originData, v_buff_size size, bool copyAsOwnData);
  62. public:
  63. /**
  64. * Constructor. Default.
  65. */
  66. StrBuffer();
  67. /**
  68. * Constructor.
  69. * @param data - pointer to data.
  70. * @param size - size of the data.
  71. * @param copyAsOwnData - if true then allocate own buffer and copy data to that buffer.
  72. */
  73. StrBuffer(const void* data, v_buff_size size, bool copyAsOwnData);
  74. public:
  75. /**
  76. * virtual Destructor.
  77. */
  78. virtual ~StrBuffer();
  79. /**
  80. * Create shared StrBuffer of specified size.
  81. * @param size - size of the buffer.
  82. * @return - shared_ptr to StrBuffer.
  83. */
  84. static std::shared_ptr<StrBuffer> createShared(v_buff_size size);
  85. /**
  86. * Create shared StrBuffer with data, size, and copyAsOwnData parameters.
  87. * @param data - buffer data.
  88. * @param size - size of the data.
  89. * @param copyAsOwnData - if true then allocate own buffer and copy data to that buffer.
  90. * @return - shared_ptr to StrBuffer.
  91. */
  92. static std::shared_ptr<StrBuffer> createShared(const void* data, v_buff_size size, bool copyAsOwnData = true);
  93. /**
  94. * Create shared StrBuffer with data, and copyAsOwnData parameters.
  95. * @param data - buffer data.
  96. * @param copyAsOwnData - if true then allocate own buffer and copy data to that buffer.
  97. * @return - shared_ptr to StrBuffer.
  98. */
  99. static std::shared_ptr<StrBuffer> createShared(const char* data, bool copyAsOwnData = true);
  100. /**
  101. * Create shared StrBuffer from other StrBuffer.
  102. * @param other - other StrBuffer.
  103. * @param copyAsOwnData - if true then allocate own buffer and copy data to that buffer.
  104. * @return - shared_ptr to StrBuffer.
  105. */
  106. static std::shared_ptr<StrBuffer> createShared(StrBuffer* other, bool copyAsOwnData = true);
  107. /**
  108. * Create shared StrBuffer of size=size1 + size2 and data=data1 + data2.
  109. * @param data1 - pointer to data1.
  110. * @param size1 - size of the data1.
  111. * @param data2 - pointer to data2.
  112. * @param size2 - size of the data2.
  113. * @return - shared_ptr to StrBuffer.
  114. */
  115. static std::shared_ptr<StrBuffer> createSharedConcatenated(const void* data1, v_buff_size size1, const void* data2, v_buff_size size2);
  116. /**
  117. * Create shared StrBuffer from c-string.
  118. * @param data - data.
  119. * @param copyAsOwnData - if true then allocate own buffer and copy data to that buffer.
  120. * @return - shared_ptr to StrBuffer.
  121. */
  122. static std::shared_ptr<StrBuffer> createFromCString(const char* data, bool copyAsOwnData = true) {
  123. if(data != nullptr) {
  124. return allocShared(data, std::strlen(data), copyAsOwnData);
  125. }
  126. return nullptr;
  127. }
  128. /**
  129. * Load data from file and store in StrBuffer.
  130. * @param filename - name of the file.
  131. * @return - shared_ptr to StrBuffer.
  132. */
  133. static std::shared_ptr<StrBuffer> loadFromFile(const char* filename);
  134. /**
  135. * Save content of the buffer to file.
  136. * @param filename - name of the file.
  137. */
  138. void saveToFile(const char* filename);
  139. /**
  140. * Get pointer to data of the buffer.
  141. * @return - pointer to data of the buffer.
  142. */
  143. p_char8 getData() const;
  144. /**
  145. * Get buffer size.
  146. * @return - buffer size.
  147. */
  148. v_buff_size getSize() const;
  149. /**
  150. * Get pointer to data of the buffer as `const* char`.
  151. * @return - pointer to data of the buffer.
  152. */
  153. const char* c_str() const;
  154. /**
  155. * Get copy of the buffer data as `std::string`.
  156. * @return - copy of the buffer data as `std::string`.
  157. */
  158. std::string std_str() const;
  159. /**
  160. * Is this object is responsible for freeing buffer data.
  161. * @return - true if this object is responsible for freeing buffer data.
  162. */
  163. bool hasOwnData() const;
  164. /**
  165. * Create lowercase copy of the buffer.<br>
  166. * (correct for ASCII only)
  167. * @return - copy of the buffer containing lowercase variants of ascii symbols.
  168. */
  169. std::shared_ptr<StrBuffer> toLowerCase() const;
  170. /**
  171. * Create uppercase copy of the buffer.<br>
  172. * (correct for ASCII only)
  173. * @return - copy of the buffer containing uppercase variants of ascii symbols.
  174. */
  175. std::shared_ptr<StrBuffer> toUpperCase() const;
  176. /**
  177. * Check string equality of the buffer to data of specified size.
  178. * @param data - pointer to data to be compared with the buffer data.
  179. * @param size - size of the data.
  180. * @return - true if all chars of buffer are same as in data, and size == this.getSize().
  181. */
  182. bool equals(const void* data, v_buff_size size) const;
  183. /**
  184. * Check string equality of the buffer to data of specified size.
  185. * @param data - pointer to data to be compared with the buffer data.
  186. * @return - true if all chars of buffer are same as in data, and std::strlen(data) == this.getSize().
  187. */
  188. bool equals(const char* data) const;
  189. /**
  190. * Check string equality to other buffer.
  191. * @param other - pointer to other StrBuffer to be compared with the buffer data.
  192. * @return - true if all chars of one buffer are same as in other, and other.getSize() == this.getSize().
  193. */
  194. bool equals(StrBuffer* other) const;
  195. /**
  196. * Check if buffer starts with specified data, size.
  197. * @param data - data as `const void*`.
  198. * @param size - size of the data.
  199. * @return - true if buffer starts with specified data.
  200. */
  201. bool startsWith(const void* data, v_buff_size size) const;
  202. /**
  203. * Check if buffer starts with specified data.
  204. * @param data - data as `const char*`.
  205. * @return - true if buffer starts with specified data.
  206. */
  207. bool startsWith(const char* data) const;
  208. /**
  209. * Check if buffer starts with specified data.
  210. * @param data - data as `StrBuffer`.
  211. * @return - true if buffer starts with specified data.
  212. */
  213. bool startsWith(StrBuffer* data) const;
  214. public:
  215. /**
  216. * Compare data1, data2 using `std::memcmp`.
  217. * @param data1 - pointer to data1.
  218. * @param data2 - pointer to data2.
  219. * @param size - number of characters to compare.
  220. * @return - Negative value if the first differing byte (reinterpreted as unsigned char) in data1 is less than the corresponding byte in data2.<br>
  221. * ​0​ if all count bytes of data1 and data2 are equal.<br>
  222. * Positive value if the first differing byte in data1 is greater than the corresponding byte in data2.
  223. */
  224. static v_buff_size compare(const void* data1, const void* data2, v_buff_size size);
  225. /**
  226. * Compare data1, data2 using `std::memcmp`.
  227. * @param data1 - data1 as `StrBuffer`.
  228. * @param data2 - data2 as `StrBuffer`.
  229. * @return - Negative value if the first differing byte (reinterpreted as unsigned char) in data1 is less than the corresponding byte in data2.<br>
  230. * ​0​ if all count bytes of data1 and data2 are equal.<br>
  231. * Positive value if the first differing byte in data1 is greater than the corresponding byte in data2.
  232. */
  233. static v_buff_size compare(StrBuffer* data1, StrBuffer* data2);
  234. /**
  235. * Check string equality of data1 to data2.
  236. * @param data1 - pointer to data1.
  237. * @param data2 - pointer to data2.
  238. * @param size - number of characters to compare.
  239. * @return - `true` if equals.
  240. */
  241. static bool equals(const void* data1, const void* data2, v_buff_size size);
  242. /**
  243. * Check string equality of data1 to data2.
  244. * @param data1 - pointer to data1.
  245. * @param data2 - pointer to data2.
  246. * @return - `true` if equals.
  247. */
  248. static bool equals(const char* data1, const char* data2);
  249. /**
  250. * Check string equality of str1 to str2.
  251. * @param str1 - pointer to str1.
  252. * @param str2 - pointer to str2.
  253. * @return - `true` if equals.
  254. */
  255. static bool equals(StrBuffer* str1, StrBuffer* str2);
  256. /**
  257. * Check Case Insensitive string equality of data1 to data2.
  258. * @param data1 - pointer to data1.
  259. * @param data2 - pointer to data2.
  260. * @param size - number of characters to compare.
  261. * @return - `true` if equals.
  262. */
  263. static bool equalsCI(const void* data1, const void* data2, v_buff_size size);
  264. /**
  265. * Check Case Insensitive string equality of data1 to data2.
  266. * @param data1 - pointer to data1.
  267. * @param data2 - pointer to data2.
  268. * @return - `true` if equals.
  269. */
  270. static bool equalsCI(const char* data1, const char* data2);
  271. /**
  272. * Check Case Insensitive string equality of str1 to str2.
  273. * @param str1 - pointer to str1.
  274. * @param str2 - pointer to str2.
  275. * @return - `true` if equals.
  276. */
  277. static bool equalsCI(StrBuffer* str1, StrBuffer* str2);
  278. /**
  279. * Check Case Insensitive string equality of data1 to data2. (ASCII only, correct compare if one of strings contains letters only)
  280. * @param data1 - pointer to data1.
  281. * @param data2 - pointer to data2.
  282. * @param size - number of characters to compare.
  283. * @return - `true` if equals.
  284. */
  285. static bool equalsCI_FAST(const void* data1, const void* data2, v_buff_size size);
  286. /**
  287. * Check Case Insensitive string equality of data1 to data2. (ASCII only, correct compare if one of strings contains letters only)
  288. * @param data1 - pointer to data1.
  289. * @param data2 - pointer to data2.
  290. * @return - `true` if equals.
  291. */
  292. static bool equalsCI_FAST(const char* data1, const char* data2);
  293. /**
  294. * Check Case Insensitive string equality of str1 to str2. (ASCII only, correct compare if one of strings contains letters only)
  295. * @param str1 - pointer to str1.
  296. * @param str2 - pointer to str2.
  297. * @return - `true` if equals.
  298. */
  299. static bool equalsCI_FAST(StrBuffer* str1, StrBuffer* str2);
  300. /**
  301. * Check Case Insensitive string equality of str1 to str2. (ASCII only, correct compare if one of strings contains letters only)
  302. * @param str1 - pointer to str1 as `StrBuffer`.
  303. * @param str2 - pointer to str2 as `const char*`
  304. * @return - `true` if equals.
  305. */
  306. static bool equalsCI_FAST(StrBuffer* str1, const char* str2);
  307. /**
  308. * Change characters in data to lowercase.
  309. * @param data - pointer to data.
  310. * @param size - size of the data.
  311. */
  312. static void lowerCase(const void* data, v_buff_size size);
  313. /**
  314. * Change characters in data to uppercase.
  315. * @param data - pointer to data.
  316. * @param size - size of the data.
  317. */
  318. static void upperCase(const void* data, v_buff_size size);
  319. };
  320. }}
  321. #endif /* oatpp_base_StrBuffer_hpp */