String.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649
  1. // Copyright (C) 2009-2016, Panagiotis Christopoulos Charitos.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #pragma once
  6. #include <anki/util/DArray.h>
  7. #include <anki/util/Array.h>
  8. #include <anki/util/NonCopyable.h>
  9. #include <anki/util/Hash.h>
  10. #include <cstring>
  11. #include <cstdio>
  12. namespace anki
  13. {
  14. // Forward
  15. class String;
  16. /// @addtogroup util_private
  17. /// @{
  18. namespace detail
  19. {
  20. template<typename TNumber>
  21. constexpr const char* toStringFormat()
  22. {
  23. return nullptr;
  24. }
  25. #define ANKI_DEPLOY_TO_STRING(type_, string_) \
  26. template<> \
  27. constexpr const char* toStringFormat<type_>() \
  28. { \
  29. return string_; \
  30. }
  31. ANKI_DEPLOY_TO_STRING(I8, "%d")
  32. ANKI_DEPLOY_TO_STRING(I16, "%d")
  33. ANKI_DEPLOY_TO_STRING(I32, "%d")
  34. ANKI_DEPLOY_TO_STRING(I64, "%lld")
  35. ANKI_DEPLOY_TO_STRING(U8, "%u")
  36. ANKI_DEPLOY_TO_STRING(U16, "%u")
  37. ANKI_DEPLOY_TO_STRING(U32, "%u")
  38. ANKI_DEPLOY_TO_STRING(U64, "%llu")
  39. ANKI_DEPLOY_TO_STRING(F32, "%f")
  40. ANKI_DEPLOY_TO_STRING(F64, "%f")
  41. #undef ANKI_DEPLOY_TO_STRING
  42. } // end namespace detail
  43. /// @}
  44. /// @addtogroup util_containers
  45. /// @{
  46. /// A wrapper on top of C strings. Used mainly for safety.
  47. class CString
  48. {
  49. public:
  50. using Char = char;
  51. static const PtrSize NPOS = MAX_PTR_SIZE;
  52. CString() = default;
  53. CString(const Char* ptr)
  54. : m_ptr(ptr)
  55. {
  56. checkInit();
  57. }
  58. /// Copy constructor.
  59. CString(const CString& b)
  60. : m_ptr(b.m_ptr)
  61. {
  62. checkInit();
  63. }
  64. /// Copy.
  65. CString& operator=(const CString& b)
  66. {
  67. m_ptr = b.m_ptr;
  68. return *this;
  69. }
  70. /// Return true if the string is initialized.
  71. operator Bool() const
  72. {
  73. return !isEmpty();
  74. }
  75. /// Return char at the specified position.
  76. const Char& operator[](U pos) const
  77. {
  78. checkInit();
  79. ANKI_ASSERT(pos <= getLength());
  80. return m_ptr[pos];
  81. }
  82. const Char* begin() const
  83. {
  84. checkInit();
  85. return &m_ptr[0];
  86. }
  87. const Char* end() const
  88. {
  89. checkInit();
  90. return &m_ptr[getLength()];
  91. }
  92. /// Return true if the string is not initialized.
  93. Bool isEmpty() const
  94. {
  95. return m_ptr == nullptr || getLength() == 0;
  96. }
  97. Bool operator==(const CString& b) const
  98. {
  99. if(m_ptr == nullptr || b.m_ptr == nullptr)
  100. {
  101. return m_ptr == b.m_ptr;
  102. }
  103. else
  104. {
  105. return std::strcmp(m_ptr, b.m_ptr) == 0;
  106. }
  107. }
  108. Bool operator!=(const CString& b) const
  109. {
  110. return !((*this) == b);
  111. }
  112. Bool operator<(const CString& b) const
  113. {
  114. if(m_ptr == nullptr || b.m_ptr == nullptr)
  115. {
  116. return false;
  117. }
  118. else
  119. {
  120. return std::strcmp(m_ptr, b.m_ptr) < 0;
  121. }
  122. }
  123. Bool operator<=(const CString& b) const
  124. {
  125. if(m_ptr == nullptr || b.m_ptr == nullptr)
  126. {
  127. return m_ptr == b.m_ptr;
  128. }
  129. else
  130. {
  131. return std::strcmp(m_ptr, b.m_ptr) <= 0;
  132. }
  133. }
  134. Bool operator>(const CString& b) const
  135. {
  136. if(m_ptr == nullptr || b.m_ptr == nullptr)
  137. {
  138. return false;
  139. }
  140. else
  141. {
  142. return std::strcmp(m_ptr, b.m_ptr) > 0;
  143. }
  144. }
  145. Bool operator>=(const CString& b) const
  146. {
  147. if(m_ptr == nullptr || b.m_ptr == nullptr)
  148. {
  149. return m_ptr == b.m_ptr;
  150. }
  151. else
  152. {
  153. return std::strcmp(m_ptr, b.m_ptr) >= 0;
  154. }
  155. }
  156. /// Get the underlying C string.
  157. const char* get() const
  158. {
  159. checkInit();
  160. return m_ptr;
  161. }
  162. /// Get the string length.
  163. U getLength() const
  164. {
  165. checkInit();
  166. return std::strlen(m_ptr);
  167. }
  168. PtrSize find(const CString& cstr, PtrSize position = 0) const
  169. {
  170. checkInit();
  171. ANKI_ASSERT(position < getLength());
  172. const Char* out = std::strstr(m_ptr, &cstr[0]);
  173. return (out == nullptr) ? NPOS : (out - m_ptr);
  174. }
  175. /// Convert to F64.
  176. ANKI_USE_RESULT Error toF64(F64& out) const;
  177. /// Convert to F32.
  178. ANKI_USE_RESULT Error toF32(F32& out) const;
  179. /// Convert to I64.
  180. ANKI_USE_RESULT Error toI64(I64& out) const;
  181. private:
  182. const Char* m_ptr = nullptr;
  183. void checkInit() const
  184. {
  185. ANKI_ASSERT(m_ptr != nullptr);
  186. }
  187. };
  188. /// Hasher function for CStrings. Can be used in HashMap.
  189. class CStringHasher
  190. {
  191. public:
  192. U64 operator()(CString str)
  193. {
  194. ANKI_ASSERT(!str.isEmpty());
  195. return computeHash(&str[0], str.getLength());
  196. }
  197. };
  198. /// Compare function for CStrings. Can be used in HashMap.
  199. class CStringCompare
  200. {
  201. public:
  202. Bool operator()(CString a, CString b)
  203. {
  204. return a == b;
  205. }
  206. };
  207. /// The base class for strings.
  208. class String : public NonCopyable
  209. {
  210. public:
  211. using Char = char; ///< Character type
  212. using CStringType = CString;
  213. using Iterator = Char*;
  214. using ConstIterator = const Char*;
  215. using Allocator = GenericMemoryPoolAllocator<Char>;
  216. static const PtrSize NPOS = MAX_PTR_SIZE;
  217. /// Default constructor.
  218. String()
  219. {
  220. }
  221. /// Move constructor.
  222. String(String&& b)
  223. {
  224. move(b);
  225. }
  226. /// Requires manual destruction.
  227. ~String()
  228. {
  229. }
  230. /// Initialize using a const string.
  231. void create(Allocator alloc, const CStringType& cstr);
  232. /// Initialize using a range. Copies the range of [first, last)
  233. void create(Allocator alloc, ConstIterator first, ConstIterator last);
  234. /// Initialize using a character.
  235. void create(Allocator alloc, Char c, PtrSize length);
  236. /// Copy one string to this one.
  237. void create(Allocator alloc, const String& b)
  238. {
  239. create(alloc, b.toCString());
  240. }
  241. /// Destroy the string.
  242. void destroy(Allocator alloc)
  243. {
  244. m_data.destroy(alloc);
  245. }
  246. /// Move one string to this one.
  247. String& operator=(String&& b)
  248. {
  249. move(b);
  250. return *this;
  251. }
  252. /// Return char at the specified position.
  253. const Char& operator[](U pos) const
  254. {
  255. checkInit();
  256. return m_data[pos];
  257. }
  258. /// Return char at the specified position as a modifiable reference.
  259. Char& operator[](U pos)
  260. {
  261. checkInit();
  262. return m_data[pos];
  263. }
  264. Iterator begin()
  265. {
  266. checkInit();
  267. return &m_data[0];
  268. }
  269. ConstIterator begin() const
  270. {
  271. checkInit();
  272. return &m_data[0];
  273. }
  274. Iterator end()
  275. {
  276. checkInit();
  277. return &m_data[m_data.getSize() - 1];
  278. }
  279. ConstIterator end() const
  280. {
  281. checkInit();
  282. return &m_data[m_data.getSize() - 1];
  283. }
  284. /// Return true if strings are equal
  285. Bool operator==(const String& b) const
  286. {
  287. checkInit();
  288. b.checkInit();
  289. return std::strcmp(&m_data[0], &b.m_data[0]) == 0;
  290. }
  291. /// Return true if strings are not equal
  292. Bool operator!=(const String& b) const
  293. {
  294. return !(*this == b);
  295. }
  296. /// Return true if this is less than b
  297. Bool operator<(const String& b) const
  298. {
  299. checkInit();
  300. b.checkInit();
  301. return std::strcmp(&m_data[0], &b.m_data[0]) < 0;
  302. }
  303. /// Return true if this is less or equal to b
  304. Bool operator<=(const String& b) const
  305. {
  306. checkInit();
  307. b.checkInit();
  308. return std::strcmp(&m_data[0], &b.m_data[0]) <= 0;
  309. }
  310. /// Return true if this is greater than b
  311. Bool operator>(const String& b) const
  312. {
  313. checkInit();
  314. b.checkInit();
  315. return std::strcmp(&m_data[0], &b.m_data[0]) > 0;
  316. }
  317. /// Return true if this is greater or equal to b
  318. Bool operator>=(const String& b) const
  319. {
  320. checkInit();
  321. b.checkInit();
  322. return std::strcmp(&m_data[0], &b.m_data[0]) >= 0;
  323. }
  324. /// Return true if strings are equal
  325. Bool operator==(const CStringType& cstr) const
  326. {
  327. checkInit();
  328. return std::strcmp(&m_data[0], cstr.get()) == 0;
  329. }
  330. /// Return true if strings are not equal
  331. Bool operator!=(const CStringType& cstr) const
  332. {
  333. return !(*this == cstr);
  334. }
  335. /// Return true if this is less than cstr.
  336. Bool operator<(const CStringType& cstr) const
  337. {
  338. checkInit();
  339. return std::strcmp(&m_data[0], cstr.get()) < 0;
  340. }
  341. /// Return true if this is less or equal to cstr.
  342. Bool operator<=(const CStringType& cstr) const
  343. {
  344. checkInit();
  345. return std::strcmp(&m_data[0], cstr.get()) <= 0;
  346. }
  347. /// Return true if this is greater than cstr.
  348. Bool operator>(const CStringType& cstr) const
  349. {
  350. checkInit();
  351. return std::strcmp(&m_data[0], cstr.get()) > 0;
  352. }
  353. /// Return true if this is greater or equal to cstr.
  354. Bool operator>=(const CStringType& cstr) const
  355. {
  356. checkInit();
  357. return std::strcmp(&m_data[0], cstr.get()) >= 0;
  358. }
  359. /// Return the string's length. It doesn't count the terminating character.
  360. PtrSize getLength() const
  361. {
  362. auto size = m_data.getSize();
  363. auto out = (size != 0) ? (size - 1) : 0;
  364. ANKI_ASSERT(std::strlen(&m_data[0]) == out);
  365. return out;
  366. }
  367. /// Return the CString.
  368. CStringType toCString() const
  369. {
  370. checkInit();
  371. return CStringType(&m_data[0]);
  372. }
  373. /// Append another string to this one.
  374. void append(Allocator alloc, const String& b)
  375. {
  376. if(!b.isEmpty())
  377. {
  378. appendInternal(alloc, &b.m_data[0], b.m_data.getSize());
  379. }
  380. }
  381. /// Append a const string to this one.
  382. void append(Allocator alloc, const CStringType& cstr)
  383. {
  384. if(!cstr.isEmpty())
  385. {
  386. appendInternal(alloc, &cstr[0], cstr.getLength() + 1);
  387. }
  388. }
  389. /// Create formated string.
  390. void sprintf(Allocator alloc, CString fmt, ...);
  391. /// Return true if it's empty.
  392. Bool isEmpty() const
  393. {
  394. return m_data.isEmpty();
  395. }
  396. /// Find a substring of this string.
  397. /// @param[in] cstr The substring to search.
  398. /// @param position Position of the first character in the string to be
  399. /// considered in the search.
  400. /// @return A valid position if the string is found or NPOS if not found.
  401. PtrSize find(const CStringType& cstr, PtrSize position = 0) const
  402. {
  403. checkInit();
  404. return toCString().find(cstr, position);
  405. }
  406. /// Find a substring of this string.
  407. /// @param[in] str The substring to search.
  408. /// @param position Position of the first character in the string to be
  409. /// considered in the search.
  410. /// @return A valid position if the string is found or NPOS if not found.
  411. PtrSize find(const String& str, PtrSize position) const
  412. {
  413. str.checkInit();
  414. return find(str.toCString(), position);
  415. }
  416. /// Convert a number to a string.
  417. template<typename TNumber>
  418. void toString(Allocator alloc, TNumber number);
  419. /// Convert to F64.
  420. ANKI_USE_RESULT Error toF64(F64& out) const
  421. {
  422. return toCString().toF64(out);
  423. }
  424. /// Convert to I64.
  425. ANKI_USE_RESULT Error toI64(I64& out) const
  426. {
  427. return toCString().toI64(out);
  428. }
  429. protected:
  430. DArray<Char> m_data;
  431. void checkInit() const
  432. {
  433. ANKI_ASSERT(m_data.getSize() > 1);
  434. }
  435. /// Append to this string.
  436. void appendInternal(Allocator alloc, const Char* str, PtrSize strSize);
  437. void move(String& b)
  438. {
  439. ANKI_ASSERT(this != &b);
  440. m_data = std::move(b.m_data);
  441. }
  442. };
  443. //==============================================================================
  444. template<typename TNumber>
  445. inline void String::toString(Allocator alloc, TNumber number)
  446. {
  447. destroy(alloc);
  448. Array<Char, 512> buff;
  449. I ret = std::snprintf(
  450. &buff[0], buff.size(), detail::toStringFormat<TNumber>(), number);
  451. if(ret < 0 || ret > static_cast<I>(buff.getSize()))
  452. {
  453. ANKI_LOGF("To small intermediate buffer");
  454. }
  455. else
  456. {
  457. create(alloc, &buff[0]);
  458. }
  459. }
  460. /// String with automatic cleanup.
  461. class StringAuto : public String
  462. {
  463. public:
  464. using Base = String;
  465. using Allocator = typename Base::Allocator;
  466. /// Create with allocator.
  467. StringAuto(Allocator alloc)
  468. : Base()
  469. , m_alloc(alloc)
  470. {
  471. }
  472. /// Move constructor.
  473. StringAuto(StringAuto&& b)
  474. : Base()
  475. {
  476. move(b);
  477. }
  478. /// Automatic destruction.
  479. ~StringAuto()
  480. {
  481. Base::destroy(m_alloc);
  482. }
  483. /// Initialize using a const string.
  484. void create(const CStringType& cstr)
  485. {
  486. Base::create(m_alloc, cstr);
  487. }
  488. /// Initialize using a range. Copies the range of [first, last)
  489. void create(ConstIterator first, ConstIterator last)
  490. {
  491. Base::create(m_alloc, first, last);
  492. }
  493. /// Initialize using a character.
  494. void create(Char c, PtrSize length)
  495. {
  496. Base::create(m_alloc, c, length);
  497. }
  498. /// Copy one string to this one.
  499. void create(const String& b)
  500. {
  501. Base::create(m_alloc, b.toCString());
  502. }
  503. /// Destroy the string.
  504. void destroy()
  505. {
  506. Base::destroy(m_alloc);
  507. }
  508. /// Move one string to this one.
  509. StringAuto& operator=(StringAuto&& b)
  510. {
  511. move(b);
  512. return *this;
  513. }
  514. /// Append another string to this one.
  515. void append(const String& b)
  516. {
  517. Base::append(m_alloc, b);
  518. }
  519. /// Append a const string to this one.
  520. void append(const CStringType& cstr)
  521. {
  522. Base::append(m_alloc, cstr);
  523. }
  524. /// Create formated string.
  525. template<typename... TArgs>
  526. void sprintf(CString fmt, TArgs... args)
  527. {
  528. Base::sprintf(m_alloc, fmt, args...);
  529. }
  530. /// Convert a number to a string.
  531. template<typename TNumber>
  532. void toString(TNumber number)
  533. {
  534. Base::toString(m_alloc, number);
  535. }
  536. private:
  537. GenericMemoryPoolAllocator<Char> m_alloc;
  538. void move(StringAuto& b)
  539. {
  540. Base::move(b);
  541. m_alloc = std::move(b.m_alloc);
  542. }
  543. };
  544. /// @}
  545. } // end namespace anki