strbase.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572
  1. #pragma once
  2. // Some of these includes are just a legacy from previous implementation.
  3. // We don't need them here, but removing them is tricky because it breaks all
  4. // kinds of builds downstream
  5. #include "mem_copy.h"
  6. #include "ptr.h"
  7. #include "utility.h"
  8. #include <util/charset/unidata.h>
  9. #include <util/system/platform.h>
  10. #include <util/system/yassert.h>
  11. #include <contrib/libs/libc_compat/string.h>
  12. #include <cctype>
  13. #include <cstring>
  14. #include <string>
  15. #include <string_view>
  16. namespace NStringPrivate {
  17. template <class TCharType>
  18. size_t GetStringLengthWithLimit(const TCharType* s, size_t maxlen) {
  19. Y_ASSERT(s);
  20. size_t i = 0;
  21. for (; i != maxlen && s[i]; ++i)
  22. ;
  23. return i;
  24. }
  25. inline size_t GetStringLengthWithLimit(const char* s, size_t maxlen) {
  26. Y_ASSERT(s);
  27. return strnlen(s, maxlen);
  28. }
  29. } // namespace NStringPrivate
  30. template <typename TDerived, typename TCharType, typename TTraitsType = std::char_traits<TCharType>>
  31. class TStringBase {
  32. using TStringView = std::basic_string_view<TCharType>;
  33. using TStringViewWithTraits = std::basic_string_view<TCharType, TTraitsType>;
  34. public:
  35. using TChar = TCharType;
  36. using TTraits = TTraitsType;
  37. using TSelf = TStringBase<TDerived, TChar, TTraits>;
  38. using size_type = size_t;
  39. using difference_type = ptrdiff_t;
  40. static constexpr size_t npos = size_t(-1);
  41. using const_iterator = const TCharType*;
  42. using const_reference = const TCharType&;
  43. using const_reverse_iterator = std::reverse_iterator<const_iterator>;
  44. static constexpr size_t StrLen(const TCharType* s) noexcept {
  45. if (Y_LIKELY(s)) {
  46. return TTraits::length(s);
  47. }
  48. return 0;
  49. }
  50. template <class TCharTraits>
  51. inline constexpr operator std::basic_string_view<TCharType, TCharTraits>() const {
  52. return std::basic_string_view<TCharType, TCharTraits>(data(), size());
  53. }
  54. template <class TCharTraits, class Allocator>
  55. inline explicit operator std::basic_string<TCharType, TCharTraits, Allocator>() const {
  56. return std::basic_string<TCharType, TCharTraits, Allocator>(Ptr(), Len());
  57. }
  58. /**
  59. * @param Pointer to character inside the string, or nullptr.
  60. * @return Offset from string beginning (in chars), or npos on nullptr.
  61. */
  62. inline size_t off(const TCharType* ret) const noexcept {
  63. return ret ? (size_t)(ret - Ptr()) : npos;
  64. }
  65. inline size_t IterOff(const_iterator it) const noexcept {
  66. return begin() <= it && end() > it ? size_t(it - begin()) : npos;
  67. }
  68. constexpr const_iterator begin() const noexcept {
  69. return Ptr();
  70. }
  71. constexpr const_iterator end() const noexcept {
  72. return Ptr() + size();
  73. }
  74. constexpr const_iterator cbegin() const noexcept {
  75. return begin();
  76. }
  77. constexpr const_iterator cend() const noexcept {
  78. return end();
  79. }
  80. constexpr const_reverse_iterator rbegin() const noexcept {
  81. return const_reverse_iterator(Ptr() + size());
  82. }
  83. constexpr const_reverse_iterator rend() const noexcept {
  84. return const_reverse_iterator(Ptr());
  85. }
  86. constexpr const_reverse_iterator crbegin() const noexcept {
  87. return rbegin();
  88. }
  89. constexpr const_reverse_iterator crend() const noexcept {
  90. return rend();
  91. }
  92. inline TCharType back() const noexcept {
  93. Y_ASSERT(!this->empty());
  94. return Ptr()[Len() - 1];
  95. }
  96. inline TCharType front() const noexcept {
  97. Y_ASSERT(!empty());
  98. return Ptr()[0];
  99. }
  100. constexpr const TCharType* data() const noexcept {
  101. return Ptr();
  102. }
  103. constexpr inline size_t size() const noexcept {
  104. return Len();
  105. }
  106. constexpr inline bool is_null() const noexcept {
  107. return *Ptr() == 0;
  108. }
  109. Y_PURE_FUNCTION constexpr inline bool empty() const noexcept {
  110. return Len() == 0;
  111. }
  112. constexpr inline explicit operator bool() const noexcept {
  113. return !empty();
  114. }
  115. public: // style-guide compliant methods
  116. constexpr const TCharType* Data() const noexcept {
  117. return Ptr();
  118. }
  119. constexpr size_t Size() const noexcept {
  120. return Len();
  121. }
  122. Y_PURE_FUNCTION constexpr bool Empty() const noexcept {
  123. return 0 == Len();
  124. }
  125. private:
  126. static constexpr TStringView LegacySubString(const TStringView view, size_t p, size_t n) noexcept {
  127. p = Min(p, view.length());
  128. return view.substr(p, n);
  129. }
  130. public:
  131. // ~~~ Comparison ~~~ : FAMILY0(int, compare)
  132. static constexpr int compare(const TSelf& s1, const TSelf& s2) noexcept {
  133. return s1.AsStringView().compare(s2.AsStringView());
  134. }
  135. static constexpr int compare(const TCharType* p, const TSelf& s2) noexcept {
  136. TCharType null{0};
  137. return TStringViewWithTraits(p ? p : &null).compare(s2.AsStringView());
  138. }
  139. static constexpr int compare(const TSelf& s1, const TCharType* p) noexcept {
  140. TCharType null{0};
  141. return s1.AsStringView().compare(p ? p : &null);
  142. }
  143. static constexpr int compare(const TStringView s1, const TStringView s2) noexcept {
  144. return TStringViewWithTraits(s1.data(), s1.size()).compare(TStringViewWithTraits(s2.data(), s2.size()));
  145. }
  146. template <class T>
  147. constexpr int compare(const T& t) const noexcept {
  148. return compare(*this, t);
  149. }
  150. constexpr int compare(size_t p, size_t n, const TStringView t) const noexcept {
  151. return compare(LegacySubString(*this, p, n), t);
  152. }
  153. constexpr int compare(size_t p, size_t n, const TStringView t, size_t p1, size_t n1) const noexcept {
  154. return compare(LegacySubString(*this, p, n), LegacySubString(t, p1, n1));
  155. }
  156. constexpr int compare(size_t p, size_t n, const TStringView t, size_t n1) const noexcept {
  157. return compare(LegacySubString(*this, p, n), LegacySubString(t, 0, n1));
  158. }
  159. constexpr int compare(const TCharType* p, size_t len) const noexcept {
  160. return compare(*this, TStringView(p, len));
  161. }
  162. static constexpr bool equal(const TSelf& s1, const TSelf& s2) noexcept {
  163. return s1.AsStringView() == s2.AsStringView();
  164. }
  165. static constexpr bool equal(const TSelf& s1, const TCharType* p) noexcept {
  166. if (p == nullptr) {
  167. return s1.Len() == 0;
  168. }
  169. return s1.AsStringView() == p;
  170. }
  171. static constexpr bool equal(const TCharType* p, const TSelf& s2) noexcept {
  172. return equal(s2, p);
  173. }
  174. static constexpr bool equal(const TStringView s1, const TStringView s2) noexcept {
  175. return TStringViewWithTraits{s1.data(), s1.size()} == TStringViewWithTraits{s2.data(), s2.size()};
  176. }
  177. template <class T>
  178. constexpr bool equal(const T& t) const noexcept {
  179. return equal(*this, t);
  180. }
  181. constexpr bool equal(size_t p, size_t n, const TStringView t) const noexcept {
  182. return equal(LegacySubString(*this, p, n), t);
  183. }
  184. constexpr bool equal(size_t p, size_t n, const TStringView t, size_t p1, size_t n1) const noexcept {
  185. return equal(LegacySubString(*this, p, n), LegacySubString(t, p1, n1));
  186. }
  187. constexpr bool equal(size_t p, size_t n, const TStringView t, size_t n1) const noexcept {
  188. return equal(LegacySubString(*this, p, n), LegacySubString(t, 0, n1));
  189. }
  190. static constexpr bool StartsWith(const TCharType* what, size_t whatLen, const TCharType* with, size_t withLen) noexcept {
  191. return withLen <= whatLen && TStringViewWithTraits(what, withLen) == TStringViewWithTraits(with, withLen);
  192. }
  193. static constexpr bool EndsWith(const TCharType* what, size_t whatLen, const TCharType* with, size_t withLen) noexcept {
  194. return withLen <= whatLen && TStringViewWithTraits(what + whatLen - withLen, withLen) == TStringViewWithTraits(with, withLen);
  195. }
  196. constexpr bool StartsWith(const TCharType* s, size_t n) const noexcept {
  197. return StartsWith(Ptr(), Len(), s, n);
  198. }
  199. constexpr bool StartsWith(const TStringView s) const noexcept {
  200. return StartsWith(s.data(), s.length());
  201. }
  202. constexpr bool StartsWith(TCharType ch) const noexcept {
  203. return !empty() && TTraits::eq(*Ptr(), ch);
  204. }
  205. constexpr bool EndsWith(const TCharType* s, size_t n) const noexcept {
  206. return EndsWith(Ptr(), Len(), s, n);
  207. }
  208. constexpr bool EndsWith(const TStringView s) const noexcept {
  209. return EndsWith(s.data(), s.length());
  210. }
  211. constexpr bool EndsWith(TCharType ch) const noexcept {
  212. return !empty() && TTraits::eq(Ptr()[Len() - 1], ch);
  213. }
  214. template <typename TDerived2, typename TTraits2>
  215. constexpr bool operator==(const TStringBase<TDerived2, TChar, TTraits2>& s2) const noexcept {
  216. return equal(*this, s2);
  217. }
  218. constexpr bool operator==(TStringView s2) const noexcept {
  219. return equal(*this, s2);
  220. }
  221. constexpr bool operator==(const TCharType* pc) const noexcept {
  222. return equal(*this, pc);
  223. }
  224. #ifndef __cpp_impl_three_way_comparison
  225. friend constexpr bool operator==(const TCharType* pc, const TSelf& s) noexcept {
  226. return equal(pc, s);
  227. }
  228. template <typename TDerived2, typename TTraits2>
  229. friend constexpr bool operator!=(const TSelf& s1, const TStringBase<TDerived2, TChar, TTraits2>& s2) noexcept {
  230. return !(s1 == s2);
  231. }
  232. friend constexpr bool operator!=(const TSelf& s1, TStringView s2) noexcept {
  233. return !(s1 == s2);
  234. }
  235. friend constexpr bool operator!=(const TSelf& s, const TCharType* pc) noexcept {
  236. return !(s == pc);
  237. }
  238. friend constexpr bool operator!=(const TCharType* pc, const TSelf& s) noexcept {
  239. return !(pc == s);
  240. }
  241. #endif
  242. template <typename TDerived2, typename TTraits2>
  243. friend constexpr bool operator<(const TSelf& s1, const TStringBase<TDerived2, TChar, TTraits2>& s2) noexcept {
  244. return compare(s1, s2) < 0;
  245. }
  246. friend constexpr bool operator<(const TSelf& s1, TStringView s2) noexcept {
  247. return compare(s1, s2) < 0;
  248. }
  249. friend constexpr bool operator<(const TSelf& s, const TCharType* pc) noexcept {
  250. return compare(s, pc) < 0;
  251. }
  252. friend constexpr bool operator<(const TCharType* pc, const TSelf& s) noexcept {
  253. return compare(pc, s) < 0;
  254. }
  255. template <typename TDerived2, typename TTraits2>
  256. friend constexpr bool operator<=(const TSelf& s1, const TStringBase<TDerived2, TChar, TTraits2>& s2) noexcept {
  257. return compare(s1, s2) <= 0;
  258. }
  259. friend constexpr bool operator<=(const TSelf& s1, TStringView s2) noexcept {
  260. return compare(s1, s2) <= 0;
  261. }
  262. friend constexpr bool operator<=(const TSelf& s, const TCharType* pc) noexcept {
  263. return compare(s, pc) <= 0;
  264. }
  265. friend constexpr bool operator<=(const TCharType* pc, const TSelf& s) noexcept {
  266. return compare(pc, s) <= 0;
  267. }
  268. template <typename TDerived2, typename TTraits2>
  269. friend constexpr bool operator>(const TSelf& s1, const TStringBase<TDerived2, TChar, TTraits2>& s2) noexcept {
  270. return compare(s1, s2) > 0;
  271. }
  272. friend constexpr bool operator>(const TSelf& s1, TStringView s2) noexcept {
  273. return compare(s1, s2) > 0;
  274. }
  275. friend constexpr bool operator>(const TSelf& s, const TCharType* pc) noexcept {
  276. return compare(s, pc) > 0;
  277. }
  278. friend constexpr bool operator>(const TCharType* pc, const TSelf& s) noexcept {
  279. return compare(pc, s) > 0;
  280. }
  281. template <typename TDerived2, typename TTraits2>
  282. friend constexpr bool operator>=(const TSelf& s1, const TStringBase<TDerived2, TChar, TTraits2>& s2) noexcept {
  283. return compare(s1, s2) >= 0;
  284. }
  285. friend constexpr bool operator>=(const TSelf& s1, TStringView s2) noexcept {
  286. return compare(s1, s2) >= 0;
  287. }
  288. friend constexpr bool operator>=(const TSelf& s, const TCharType* pc) noexcept {
  289. return compare(s, pc) >= 0;
  290. }
  291. friend constexpr bool operator>=(const TCharType* pc, const TSelf& s) noexcept {
  292. return compare(pc, s) >= 0;
  293. }
  294. // ~~ Read access ~~
  295. inline TCharType at(size_t pos) const noexcept {
  296. if (Y_LIKELY(pos < Len())) {
  297. return (Ptr())[pos];
  298. }
  299. return 0;
  300. }
  301. inline TCharType operator[](size_t pos) const noexcept {
  302. Y_ASSERT(pos < this->size());
  303. return Ptr()[pos];
  304. }
  305. //~~~~Search~~~~
  306. /**
  307. * @return Position of the substring inside this string, or `npos` if not found.
  308. */
  309. inline size_t find(const TStringView s, size_t pos = 0) const noexcept {
  310. return find(s.data(), pos, s.size());
  311. }
  312. inline size_t find(const TCharType* s, size_t pos, size_t count) const noexcept {
  313. return AsStringView().find(s, pos, count);
  314. }
  315. inline size_t find(TCharType c, size_t pos = 0) const noexcept {
  316. return AsStringView().find(c, pos);
  317. }
  318. inline size_t rfind(TCharType c) const noexcept {
  319. return AsStringView().rfind(c);
  320. }
  321. inline size_t rfind(TCharType c, size_t pos) const noexcept {
  322. if (pos == 0) {
  323. return npos;
  324. }
  325. return AsStringView().rfind(c, pos - 1);
  326. }
  327. inline size_t rfind(const TStringView str, size_t pos = npos) const {
  328. return AsStringView().rfind(str.data(), pos, str.size());
  329. }
  330. //~~~~Contains~~~~
  331. /**
  332. * @returns Whether this string contains the provided substring.
  333. */
  334. inline bool Contains(const TStringView s, size_t pos = 0) const noexcept {
  335. return !s.length() || find(s, pos) != npos;
  336. }
  337. inline bool Contains(TChar c, size_t pos = 0) const noexcept {
  338. return find(c, pos) != npos;
  339. }
  340. inline void Contains(std::enable_if<std::is_unsigned<TCharType>::value, char> c, size_t pos = 0) const noexcept {
  341. return find(ui8(c), pos) != npos;
  342. }
  343. //~~~~Character Set Search~~~
  344. inline size_t find_first_of(TCharType c) const noexcept {
  345. return find_first_of(c, 0);
  346. }
  347. inline size_t find_first_of(TCharType c, size_t pos) const noexcept {
  348. return find(c, pos);
  349. }
  350. inline size_t find_first_of(const TStringView set) const noexcept {
  351. return find_first_of(set, 0);
  352. }
  353. inline size_t find_first_of(const TStringView set, size_t pos) const noexcept {
  354. return AsStringView().find_first_of(set.data(), pos, set.size());
  355. }
  356. inline size_t find_first_not_of(TCharType c) const noexcept {
  357. return find_first_not_of(c, 0);
  358. }
  359. inline size_t find_first_not_of(TCharType c, size_t pos) const noexcept {
  360. return find_first_not_of(TStringView(&c, 1), pos);
  361. }
  362. inline size_t find_first_not_of(const TStringView set) const noexcept {
  363. return find_first_not_of(set, 0);
  364. }
  365. inline size_t find_first_not_of(const TStringView set, size_t pos) const noexcept {
  366. return AsStringView().find_first_not_of(set.data(), pos, set.size());
  367. }
  368. inline size_t find_last_of(TCharType c, size_t pos = npos) const noexcept {
  369. return find_last_of(&c, pos, 1);
  370. }
  371. inline size_t find_last_of(const TStringView set, size_t pos = npos) const noexcept {
  372. return find_last_of(set.data(), pos, set.length());
  373. }
  374. inline size_t find_last_of(const TCharType* set, size_t pos, size_t n) const noexcept {
  375. return AsStringView().find_last_of(set, pos, n);
  376. }
  377. inline size_t find_last_not_of(TCharType c, size_t pos = npos) const noexcept {
  378. return AsStringView().find_last_not_of(c, pos);
  379. }
  380. inline size_t find_last_not_of(const TStringView set, size_t pos = npos) const noexcept {
  381. return find_last_not_of(set.data(), pos, set.length());
  382. }
  383. inline size_t find_last_not_of(const TCharType* set, size_t pos, size_t n) const noexcept {
  384. return AsStringView().find_last_not_of(set, pos, n);
  385. }
  386. inline size_t copy(TCharType* pc, size_t n, size_t pos) const {
  387. if (pos > Len()) {
  388. throw std::out_of_range("TStringBase::copy");
  389. }
  390. return CopyImpl(pc, n, pos);
  391. }
  392. inline size_t copy(TCharType* pc, size_t n) const noexcept {
  393. return CopyImpl(pc, n, 0);
  394. }
  395. inline size_t strcpy(TCharType* pc, size_t n) const noexcept {
  396. if (n) {
  397. n = copy(pc, n - 1);
  398. pc[n] = 0;
  399. }
  400. return n;
  401. }
  402. inline TDerived copy() const Y_WARN_UNUSED_RESULT {
  403. return TDerived(Ptr(), Len());
  404. }
  405. // ~~~ Partial copy ~~~~
  406. TDerived substr(size_t pos, size_t n = npos) const Y_WARN_UNUSED_RESULT {
  407. return TDerived(*This(), pos, n);
  408. }
  409. private:
  410. using GenericFinder = const TCharType* (*)(const TCharType*, size_t, const TCharType*, size_t);
  411. constexpr TStringViewWithTraits AsStringView() const {
  412. return static_cast<TStringViewWithTraits>(*this);
  413. }
  414. constexpr inline const TCharType* Ptr() const noexcept {
  415. return This()->data();
  416. }
  417. constexpr inline size_t Len() const noexcept {
  418. return This()->length();
  419. }
  420. constexpr inline const TDerived* This() const noexcept {
  421. return static_cast<const TDerived*>(this);
  422. }
  423. inline size_t CopyImpl(TCharType* pc, size_t n, size_t pos) const noexcept {
  424. const size_t toCopy = Min(Len() - pos, n);
  425. TTraits::copy(pc, Ptr() + pos, toCopy);
  426. return toCopy;
  427. }
  428. };
  429. /**
  430. * @def Y_STRING_LIFETIME_BOUND
  431. *
  432. * The attribute on a string-like function parameter can be used to tell the compiler
  433. * that function return value may refer that parameter.
  434. * this macro differs from the Y_LIFETIME_BOUND in that it does not check
  435. * the lifetime of copy-on-write strings if that implementation is used.
  436. */
  437. #if defined(TSTRING_IS_STD_STRING)
  438. #define Y_STRING_LIFETIME_BOUND Y_LIFETIME_BOUND
  439. #else
  440. // It is difficult to determine the lifetime of a copy-on-write
  441. // string using static analysis, as some copies of the string may
  442. // extend the buffer's lifetime.
  443. // Therefore, checking the lifetime of such strings has not yet been implemented.
  444. #define Y_STRING_LIFETIME_BOUND
  445. #endif