string.h 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293
  1. #pragma once
  2. #include <algorithm>
  3. #include <cstddef>
  4. #include <cstring>
  5. #include <stlfwd>
  6. #include <stdexcept>
  7. #include <string>
  8. #include <string_view>
  9. #include <util/system/compiler.h>
  10. #include <util/system/yassert.h>
  11. #include "ptr.h"
  12. #include "utility.h"
  13. #include "bitops.h"
  14. #include "explicit_type.h"
  15. #include "reserve.h"
  16. #include "singleton.h"
  17. #include "strbase.h"
  18. #include "strbuf.h"
  19. #include "string_hash.h"
  20. #if defined(address_sanitizer_enabled) || defined(thread_sanitizer_enabled)
  21. #include "hide_ptr.h"
  22. #endif
  23. template <class TCharType, class TCharTraits, class TAllocator>
  24. void ResizeUninitialized(std::basic_string<TCharType, TCharTraits, TAllocator>& s, size_t len) {
  25. #if defined(_YNDX_LIBCXX_ENABLE_STRING_RESIZE_UNINITIALIZED)
  26. s.resize_uninitialized(len);
  27. #else
  28. s.resize(len);
  29. #endif
  30. }
  31. #define Y_NOEXCEPT
  32. #ifndef TSTRING_IS_STD_STRING
  33. template <class T>
  34. class TStringPtrOps {
  35. public:
  36. static inline void Ref(T* t) noexcept {
  37. if (t != T::NullStr()) {
  38. t->Ref();
  39. }
  40. }
  41. static inline void UnRef(T* t) noexcept {
  42. if (t != T::NullStr()) {
  43. t->UnRef();
  44. }
  45. }
  46. static inline long RefCount(const T* t) noexcept {
  47. if (t == T::NullStr()) {
  48. return -1;
  49. }
  50. return t->RefCount();
  51. }
  52. };
  53. alignas(32) extern const char NULL_STRING_REPR[128];
  54. struct TRefCountHolder {
  55. TAtomicCounter C = 1;
  56. };
  57. template <class B>
  58. struct TStdString: public TRefCountHolder, public B {
  59. template <typename... Args>
  60. inline TStdString(Args&&... args)
  61. : B(std::forward<Args>(args)...)
  62. {
  63. }
  64. inline bool IsNull() const noexcept {
  65. return this == NullStr();
  66. }
  67. static TStdString* NullStr() noexcept {
  68. #ifdef _LIBCPP_VERSION
  69. return (TStdString*)NULL_STRING_REPR;
  70. #else
  71. return Singleton<TStdString>();
  72. #endif
  73. }
  74. private:
  75. friend TStringPtrOps<TStdString>;
  76. inline void Ref() noexcept {
  77. C.Inc();
  78. }
  79. inline void UnRef() noexcept {
  80. if (C.Val() == 1 || C.Dec() == 0) {
  81. delete this;
  82. }
  83. }
  84. inline long RefCount() const noexcept {
  85. return C.Val();
  86. }
  87. };
  88. template <class TStringType>
  89. class TBasicCharRef {
  90. public:
  91. using TChar = typename TStringType::TChar;
  92. TBasicCharRef(TStringType& s, size_t pos)
  93. : S_(s)
  94. , Pos_(pos)
  95. {
  96. }
  97. operator TChar() const {
  98. return S_.at(Pos_);
  99. }
  100. TChar* operator&() {
  101. return S_.begin() + Pos_;
  102. }
  103. const TChar* operator&() const {
  104. return S_.cbegin() + Pos_;
  105. }
  106. TBasicCharRef& operator=(TChar c) {
  107. Y_ASSERT(Pos_ < S_.size() || (Pos_ == S_.size() && !c));
  108. S_.Detach()[Pos_] = c;
  109. return *this;
  110. }
  111. TBasicCharRef& operator=(const TBasicCharRef& other) {
  112. return this->operator=(static_cast<TChar>(other));
  113. }
  114. /*
  115. * WARN:
  116. * Though references are copyable types according to the standard,
  117. * the behavior of this explicit default specification is different from the one
  118. * implemented by the assignment operator above.
  119. *
  120. * An attempt to explicitly delete it will break valid invocations like
  121. * auto c = flag ? s[i] : s[j];
  122. */
  123. TBasicCharRef(const TBasicCharRef&) = default;
  124. private:
  125. TStringType& S_;
  126. size_t Pos_;
  127. };
  128. #endif
  129. template <typename TCharType, typename TTraits>
  130. class TBasicString: public TStringBase<TBasicString<TCharType, TTraits>, TCharType, TTraits> {
  131. public:
  132. // TODO: Move to private section
  133. using TBase = TStringBase<TBasicString, TCharType, TTraits>;
  134. using TStringType = std::basic_string<TCharType, TTraits>;
  135. #ifdef TSTRING_IS_STD_STRING
  136. using TStorage = TStringType;
  137. using reference = typename TStorage::reference;
  138. #else
  139. using TStdStr = TStdString<TStringType>;
  140. using TStorage = TIntrusivePtr<TStdStr, TStringPtrOps<TStdStr>>;
  141. using reference = TBasicCharRef<TBasicString>;
  142. #endif
  143. using char_type = TCharType; // TODO: DROP
  144. using value_type = TCharType;
  145. using traits_type = TTraits;
  146. using iterator = TCharType*;
  147. using reverse_iterator = std::reverse_iterator<iterator>;
  148. using typename TBase::const_iterator;
  149. using typename TBase::const_reference;
  150. using typename TBase::const_reverse_iterator;
  151. struct TUninitialized {
  152. explicit TUninitialized(size_t size)
  153. : Size(size)
  154. {
  155. }
  156. size_t Size;
  157. };
  158. static size_t max_size() noexcept {
  159. static size_t res = TStringType().max_size();
  160. return res;
  161. }
  162. protected:
  163. #ifdef TSTRING_IS_STD_STRING
  164. TStorage Storage_;
  165. #else
  166. TStorage S_;
  167. template <typename... A>
  168. static TStorage Construct(A&&... a) {
  169. return {new TStdStr(std::forward<A>(a)...), typename TStorage::TNoIncrement()};
  170. }
  171. static TStorage Construct() noexcept {
  172. return TStdStr::NullStr();
  173. }
  174. TStdStr& StdStr() noexcept {
  175. return *S_;
  176. }
  177. const TStdStr& StdStr() const noexcept {
  178. return *S_;
  179. }
  180. /**
  181. * Makes a distinct copy of this string. `IsDetached()` is always true after this call.
  182. *
  183. * @throw std::length_error
  184. */
  185. void Clone() {
  186. Construct(StdStr()).Swap(S_);
  187. }
  188. size_t RefCount() const noexcept {
  189. return S_.RefCount();
  190. }
  191. #endif
  192. public:
  193. inline const TStringType& ConstRef() const {
  194. #ifdef TSTRING_IS_STD_STRING
  195. return Storage_;
  196. #else
  197. return StdStr();
  198. #endif
  199. }
  200. inline TStringType& MutRef() {
  201. #ifdef TSTRING_IS_STD_STRING
  202. return Storage_;
  203. #else
  204. Detach();
  205. return StdStr();
  206. #endif
  207. }
  208. inline const_reference operator[](size_t pos) const noexcept {
  209. Y_ASSERT(pos <= length());
  210. return this->data()[pos];
  211. }
  212. inline reference operator[](size_t pos) noexcept {
  213. Y_ASSERT(pos <= length());
  214. #ifdef TSTRING_IS_STD_STRING
  215. return Storage_[pos];
  216. #else
  217. return reference(*this, pos);
  218. #endif
  219. }
  220. using TBase::back;
  221. inline reference back() noexcept {
  222. Y_ASSERT(!this->empty());
  223. #ifdef TSTRING_IS_STD_STRING
  224. return Storage_.back();
  225. #else
  226. if (Y_UNLIKELY(this->empty())) {
  227. return reference(*this, 0);
  228. }
  229. return reference(*this, length() - 1);
  230. #endif
  231. }
  232. using TBase::front;
  233. inline reference front() noexcept {
  234. Y_ASSERT(!this->empty());
  235. #ifdef TSTRING_IS_STD_STRING
  236. return Storage_.front();
  237. #else
  238. return reference(*this, 0);
  239. #endif
  240. }
  241. inline size_t length() const noexcept {
  242. return ConstRef().length();
  243. }
  244. inline const TCharType* data() const noexcept {
  245. return ConstRef().data();
  246. }
  247. inline const TCharType* c_str() const noexcept {
  248. return ConstRef().c_str();
  249. }
  250. // ~~~ STL compatible method to obtain data pointer ~~~
  251. iterator begin() {
  252. return &*MutRef().begin();
  253. }
  254. iterator vend() {
  255. return &*MutRef().end();
  256. }
  257. reverse_iterator rbegin() {
  258. return reverse_iterator(vend());
  259. }
  260. reverse_iterator rend() {
  261. return reverse_iterator(begin());
  262. }
  263. using TBase::begin; //!< const_iterator TStringBase::begin() const
  264. using TBase::cbegin; //!< const_iterator TStringBase::cbegin() const
  265. using TBase::cend; //!< const_iterator TStringBase::cend() const
  266. using TBase::crbegin; //!< const_reverse_iterator TStringBase::crbegin() const
  267. using TBase::crend; //!< const_reverse_iterator TStringBase::crend() const
  268. using TBase::end; //!< const_iterator TStringBase::end() const
  269. using TBase::rbegin; //!< const_reverse_iterator TStringBase::rbegin() const
  270. using TBase::rend; //!< const_reverse_iterator TStringBase::rend() const
  271. inline size_t capacity() const noexcept {
  272. #ifdef TSTRING_IS_STD_STRING
  273. return Storage_.capacity();
  274. #else
  275. if (S_->IsNull()) {
  276. return 0;
  277. }
  278. return S_->capacity();
  279. #endif
  280. }
  281. TCharType* Detach() {
  282. #ifdef TSTRING_IS_STD_STRING
  283. return Storage_.data();
  284. #else
  285. if (Y_UNLIKELY(!IsDetached())) {
  286. Clone();
  287. }
  288. return (TCharType*)S_->data();
  289. #endif
  290. }
  291. bool IsDetached() const {
  292. #ifdef TSTRING_IS_STD_STRING
  293. return true;
  294. #else
  295. return 1 == RefCount();
  296. #endif
  297. }
  298. // ~~~ Size and capacity ~~~
  299. TBasicString& resize(size_t n, TCharType c = ' ') { // remove or append
  300. MutRef().resize(n, c);
  301. return *this;
  302. }
  303. // ~~~ Constructor ~~~ : FAMILY0(,TBasicString)
  304. TBasicString() noexcept
  305. #ifndef TSTRING_IS_STD_STRING
  306. : S_(Construct())
  307. #endif
  308. {
  309. }
  310. inline explicit TBasicString(::NDetail::TReserveTag rt)
  311. #ifndef TSTRING_IS_STD_STRING
  312. : S_(Construct())
  313. #endif
  314. {
  315. reserve(rt.Capacity);
  316. }
  317. inline TBasicString(const TBasicString& s)
  318. #ifdef TSTRING_IS_STD_STRING
  319. : Storage_(s.Storage_)
  320. #else
  321. : S_(s.S_)
  322. #endif
  323. {
  324. }
  325. inline TBasicString(TBasicString&& s) noexcept
  326. #ifdef TSTRING_IS_STD_STRING
  327. : Storage_(std::move(s.Storage_))
  328. #else
  329. : S_(Construct())
  330. #endif
  331. {
  332. #ifdef TSTRING_IS_STD_STRING
  333. #else
  334. s.swap(*this);
  335. #endif
  336. }
  337. template <typename T, typename A>
  338. explicit inline TBasicString(const std::basic_string<TCharType, T, A>& s)
  339. : TBasicString(s.data(), s.size())
  340. {
  341. }
  342. template <typename T, typename A>
  343. inline TBasicString(std::basic_string<TCharType, T, A>&& s)
  344. #ifdef TSTRING_IS_STD_STRING
  345. : Storage_(std::move(s))
  346. #else
  347. : S_(s.empty() ? Construct() : Construct(std::move(s)))
  348. #endif
  349. {
  350. }
  351. TBasicString(const TBasicString& s, size_t pos, size_t n) Y_NOEXCEPT
  352. #ifdef TSTRING_IS_STD_STRING
  353. : Storage_(s.Storage_, pos, n)
  354. #else
  355. : S_(n ? Construct(s, pos, n) : Construct())
  356. #endif
  357. {
  358. }
  359. TBasicString(const TCharType* pc)
  360. : TBasicString(pc, TBase::StrLen(pc))
  361. {
  362. }
  363. // TODO thegeorg@: uncomment and fix clients
  364. // TBasicString(std::nullptr_t) = delete;
  365. TBasicString(const TCharType* pc, size_t n)
  366. #ifdef TSTRING_IS_STD_STRING
  367. : Storage_(pc, n)
  368. #else
  369. : S_(n ? Construct(pc, n) : Construct())
  370. #endif
  371. {
  372. }
  373. TBasicString(std::nullptr_t, size_t) = delete;
  374. TBasicString(const TCharType* pc, size_t pos, size_t n)
  375. : TBasicString(pc + pos, n)
  376. {
  377. }
  378. #ifdef TSTRING_IS_STD_STRING
  379. explicit TBasicString(TExplicitType<TCharType> c) {
  380. Storage_.push_back(c);
  381. }
  382. #else
  383. explicit TBasicString(TExplicitType<TCharType> c)
  384. : TBasicString(&c.Value(), 1)
  385. {
  386. }
  387. explicit TBasicString(const reference& c)
  388. : TBasicString(&c, 1)
  389. {
  390. }
  391. #endif
  392. TBasicString(size_t n, TCharType c)
  393. #ifdef TSTRING_IS_STD_STRING
  394. : Storage_(n, c)
  395. #else
  396. : S_(Construct(n, c))
  397. #endif
  398. {
  399. }
  400. /**
  401. * Constructs an uninitialized string of size `uninitialized.Size`. The proper
  402. * way to use this ctor is via `TBasicString::Uninitialized` factory function.
  403. *
  404. * @throw std::length_error
  405. */
  406. TBasicString(TUninitialized uninitialized) {
  407. #if !defined(TSTRING_IS_STD_STRING)
  408. S_ = Construct();
  409. #endif
  410. ReserveAndResize(uninitialized.Size);
  411. }
  412. TBasicString(const TCharType* b, const TCharType* e)
  413. : TBasicString(b, e - b)
  414. {
  415. }
  416. explicit TBasicString(const TBasicStringBuf<TCharType, TTraits> s)
  417. : TBasicString(s.data(), s.size())
  418. {
  419. }
  420. template <typename Traits>
  421. explicit inline TBasicString(const std::basic_string_view<TCharType, Traits>& s)
  422. : TBasicString(s.data(), s.size())
  423. {
  424. }
  425. /**
  426. * WARN:
  427. * Certain invocations of this method will result in link-time error.
  428. * You are free to implement corresponding methods in string.cpp if you need them.
  429. */
  430. static TBasicString FromAscii(const ::TStringBuf& s) {
  431. return TBasicString().AppendAscii(s);
  432. }
  433. static TBasicString FromUtf8(const ::TStringBuf& s) {
  434. return TBasicString().AppendUtf8(s);
  435. }
  436. static TBasicString FromUtf16(const ::TWtringBuf& s) {
  437. return TBasicString().AppendUtf16(s);
  438. }
  439. static TBasicString Uninitialized(size_t n) {
  440. return TBasicString(TUninitialized(n));
  441. }
  442. private:
  443. template <typename... R>
  444. static size_t SumLength(const TBasicStringBuf<TCharType, TTraits> s1, const R&... r) noexcept {
  445. return s1.size() + SumLength(r...);
  446. }
  447. template <typename... R>
  448. static size_t SumLength(const TCharType /*s1*/, const R&... r) noexcept {
  449. return 1 + SumLength(r...);
  450. }
  451. static constexpr size_t SumLength() noexcept {
  452. return 0;
  453. }
  454. template <typename... R>
  455. static void CopyAll(TCharType* p, const TBasicStringBuf<TCharType, TTraits> s, const R&... r) {
  456. TTraits::copy(p, s.data(), s.size());
  457. CopyAll(p + s.size(), r...);
  458. }
  459. template <typename... R, class TNextCharType, typename = std::enable_if_t<std::is_same<TCharType, TNextCharType>::value>>
  460. static void CopyAll(TCharType* p, const TNextCharType s, const R&... r) {
  461. p[0] = s;
  462. CopyAll(p + 1, r...);
  463. }
  464. static void CopyAll(TCharType*) noexcept {
  465. }
  466. public:
  467. Y_REINITIALIZES_OBJECT inline void clear() noexcept {
  468. #ifdef TSTRING_IS_STD_STRING
  469. Storage_.clear();
  470. #else
  471. if (IsDetached()) {
  472. S_->clear();
  473. return;
  474. }
  475. Construct().Swap(S_);
  476. #endif
  477. }
  478. template <typename... R>
  479. static inline TBasicString Join(const R&... r) {
  480. TBasicString s{TUninitialized{SumLength(r...)}};
  481. TBasicString::CopyAll((TCharType*)s.data(), r...);
  482. return s;
  483. }
  484. // ~~~ Assignment ~~~ : FAMILY0(TBasicString&, assign);
  485. TBasicString& assign(size_t size, TCharType ch) {
  486. ReserveAndResize(size);
  487. std::fill(begin(), vend(), ch);
  488. return *this;
  489. }
  490. TBasicString& assign(const TBasicString& s) {
  491. TBasicString(s).swap(*this);
  492. return *this;
  493. }
  494. TBasicString& assign(const TBasicString& s, size_t pos, size_t n) {
  495. return assign(TBasicString(s, pos, n));
  496. }
  497. TBasicString& assign(const TCharType* pc) {
  498. return assign(pc, TBase::StrLen(pc));
  499. }
  500. TBasicString& assign(TCharType ch) {
  501. return assign(&ch, 1);
  502. }
  503. TBasicString& assign(const TCharType* pc, size_t len) {
  504. #if defined(address_sanitizer_enabled) || defined(thread_sanitizer_enabled)
  505. pc = (const TCharType*)HidePointerOrigin((void*)pc);
  506. #endif
  507. if (IsDetached()) {
  508. MutRef().assign(pc, len);
  509. } else {
  510. TBasicString(pc, len).swap(*this);
  511. }
  512. return *this;
  513. }
  514. TBasicString& assign(const TCharType* first, const TCharType* last) {
  515. return assign(first, last - first);
  516. }
  517. TBasicString& assign(const TCharType* pc, size_t pos, size_t n) {
  518. return assign(pc + pos, n);
  519. }
  520. TBasicString& assign(const TBasicStringBuf<TCharType, TTraits> s) {
  521. return assign(s.data(), s.size());
  522. }
  523. TBasicString& assign(const TBasicStringBuf<TCharType, TTraits> s, size_t spos, size_t sn = TBase::npos) {
  524. return assign(s.SubString(spos, sn));
  525. }
  526. inline TBasicString& AssignNoAlias(const TCharType* pc, size_t len) {
  527. return assign(pc, len);
  528. }
  529. inline TBasicString& AssignNoAlias(const TCharType* b, const TCharType* e) {
  530. return AssignNoAlias(b, e - b);
  531. }
  532. TBasicString& AssignNoAlias(const TBasicStringBuf<TCharType, TTraits> s) {
  533. return AssignNoAlias(s.data(), s.size());
  534. }
  535. TBasicString& AssignNoAlias(const TBasicStringBuf<TCharType, TTraits> s, size_t spos, size_t sn = TBase::npos) {
  536. return AssignNoAlias(s.SubString(spos, sn));
  537. }
  538. /**
  539. * WARN:
  540. * Certain invocations of this method will result in link-time error.
  541. * You are free to implement corresponding methods in string.cpp if you need them.
  542. */
  543. auto AssignAscii(const ::TStringBuf& s) {
  544. clear();
  545. return AppendAscii(s);
  546. }
  547. auto AssignUtf8(const ::TStringBuf& s) {
  548. clear();
  549. return AppendUtf8(s);
  550. }
  551. auto AssignUtf16(const ::TWtringBuf& s) {
  552. clear();
  553. return AppendUtf16(s);
  554. }
  555. TBasicString& operator=(const TBasicString& s) {
  556. return assign(s);
  557. }
  558. TBasicString& operator=(TBasicString&& s) noexcept {
  559. swap(s);
  560. return *this;
  561. }
  562. template <typename T, typename A>
  563. TBasicString& operator=(std::basic_string<TCharType, T, A>&& s) noexcept {
  564. TBasicString(std::move(s)).swap(*this);
  565. return *this;
  566. }
  567. TBasicString& operator=(const TBasicStringBuf<TCharType, TTraits> s) {
  568. return assign(s);
  569. }
  570. TBasicString& operator=(std::initializer_list<TCharType> il) {
  571. return assign(il.begin(), il.end());
  572. }
  573. TBasicString& operator=(const TCharType* s) {
  574. return assign(s);
  575. }
  576. TBasicString& operator=(std::nullptr_t) = delete;
  577. TBasicString& operator=(TExplicitType<TCharType> ch) {
  578. return assign(ch);
  579. }
  580. inline void reserve(size_t len) {
  581. MutRef().reserve(len);
  582. }
  583. // ~~~ Appending ~~~ : FAMILY0(TBasicString&, append);
  584. inline TBasicString& append(size_t count, TCharType ch) {
  585. MutRef().append(count, ch);
  586. return *this;
  587. }
  588. inline TBasicString& append(const TBasicString& s) {
  589. MutRef().append(s.ConstRef());
  590. return *this;
  591. }
  592. inline TBasicString& append(const TBasicString& s, size_t pos, size_t n) {
  593. MutRef().append(s.ConstRef(), pos, n);
  594. return *this;
  595. }
  596. inline TBasicString& append(const TCharType* pc) Y_NOEXCEPT {
  597. MutRef().append(pc);
  598. return *this;
  599. }
  600. inline TBasicString& append(TCharType c) {
  601. MutRef().push_back(c);
  602. return *this;
  603. }
  604. inline TBasicString& append(const TCharType* first, const TCharType* last) {
  605. MutRef().append(first, last);
  606. return *this;
  607. }
  608. inline TBasicString& append(const TCharType* pc, size_t len) {
  609. MutRef().append(pc, len);
  610. return *this;
  611. }
  612. inline void ReserveAndResize(size_t len) {
  613. ::ResizeUninitialized(MutRef(), len);
  614. }
  615. TBasicString& AppendNoAlias(const TCharType* pc, size_t len) {
  616. if (len) {
  617. auto s = this->size();
  618. ReserveAndResize(s + len);
  619. memcpy(&*(begin() + s), pc, len * sizeof(*pc));
  620. }
  621. return *this;
  622. }
  623. TBasicString& AppendNoAlias(const TBasicStringBuf<TCharType, TTraits> s) {
  624. return AppendNoAlias(s.data(), s.size());
  625. }
  626. TBasicString& AppendNoAlias(const TBasicStringBuf<TCharType, TTraits> s, size_t spos, size_t sn = TBase::npos) {
  627. return AppendNoAlias(s.SubString(spos, sn));
  628. }
  629. TBasicString& append(const TBasicStringBuf<TCharType, TTraits> s) {
  630. return append(s.data(), s.size());
  631. }
  632. TBasicString& append(const TBasicStringBuf<TCharType, TTraits> s, size_t spos, size_t sn = TBase::npos) {
  633. return append(s.SubString(spos, sn));
  634. }
  635. TBasicString& append(const TCharType* pc, size_t pos, size_t n, size_t pc_len = TBase::npos) {
  636. return append(pc + pos, Min(n, pc_len - pos));
  637. }
  638. /**
  639. * WARN:
  640. * Certain invocations of this method will result in link-time error.
  641. * You are free to implement corresponding methods in string.cpp if you need them.
  642. */
  643. TBasicString& AppendAscii(const ::TStringBuf& s);
  644. TBasicString& AppendUtf8(const ::TStringBuf& s);
  645. TBasicString& AppendUtf16(const ::TWtringBuf& s);
  646. inline void push_back(TCharType c) {
  647. // TODO
  648. append(c);
  649. }
  650. template <class T>
  651. TBasicString& operator+=(const T& s) {
  652. return append(s);
  653. }
  654. template <class T>
  655. friend TBasicString operator*(const TBasicString& s, T count) {
  656. TBasicString result;
  657. for (T i = 0; i < count; ++i) {
  658. result += s;
  659. }
  660. return result;
  661. }
  662. template <class T>
  663. TBasicString& operator*=(T count) {
  664. TBasicString temp;
  665. for (T i = 0; i < count; ++i) {
  666. temp += *this;
  667. }
  668. swap(temp);
  669. return *this;
  670. }
  671. operator const TStringType&() const noexcept {
  672. return this->ConstRef();
  673. }
  674. operator TStringType&() {
  675. return this->MutRef();
  676. }
  677. /*
  678. * Following overloads of "operator+" aim to choose the cheapest implementation depending on
  679. * summand types: lvalues, detached rvalues, shared rvalues.
  680. *
  681. * General idea is to use the detached-rvalue argument (left of right) to store the result
  682. * wherever possible. If a buffer in rvalue is large enough this saves a re-allocation. If
  683. * both arguments are rvalues we check which one is detached. If both of them are detached then
  684. * the left argument is obviously preferrable because you won't need to shift the data.
  685. *
  686. * If an rvalue is shared then it's basically the same as lvalue because you cannot use its
  687. * buffer to store the sum. However, we rely on the fact that append() and prepend() are already
  688. * optimized for the shared case and detach the string into the buffer large enough to store
  689. * the sum (compared to the detach+reallocation). This way, if we have only one rvalue argument
  690. * (left or right) then we simply append/prepend into it, without checking if it's detached or
  691. * not. This will be checked inside ReserveAndResize anyway.
  692. *
  693. * If both arguments cannot be used to store the sum (e.g. two lvalues) then we fall back to the
  694. * Join function that constructs a resulting string in the new buffer with the minimum overhead:
  695. * malloc + memcpy + memcpy.
  696. */
  697. friend TBasicString operator+(TBasicString&& s1, const TBasicString& s2) Y_WARN_UNUSED_RESULT {
  698. s1 += s2;
  699. return std::move(s1);
  700. }
  701. friend TBasicString operator+(const TBasicString& s1, TBasicString&& s2) Y_WARN_UNUSED_RESULT {
  702. s2.prepend(s1);
  703. return std::move(s2);
  704. }
  705. friend TBasicString operator+(TBasicString&& s1, TBasicString&& s2) Y_WARN_UNUSED_RESULT {
  706. #if 0 && !defined(TSTRING_IS_STD_STRING)
  707. if (!s1.IsDetached() && s2.IsDetached()) {
  708. s2.prepend(s1);
  709. return std::move(s2);
  710. }
  711. #endif
  712. s1 += s2;
  713. return std::move(s1);
  714. }
  715. friend TBasicString operator+(TBasicString&& s1, const TBasicStringBuf<TCharType, TTraits> s2) Y_WARN_UNUSED_RESULT {
  716. s1 += s2;
  717. return std::move(s1);
  718. }
  719. friend TBasicString operator+(TBasicString&& s1, const TCharType* s2) Y_WARN_UNUSED_RESULT {
  720. s1 += s2;
  721. return std::move(s1);
  722. }
  723. friend TBasicString operator+(TBasicString&& s1, TCharType s2) Y_WARN_UNUSED_RESULT {
  724. s1 += s2;
  725. return std::move(s1);
  726. }
  727. friend TBasicString operator+(TExplicitType<TCharType> ch, const TBasicString& s) Y_WARN_UNUSED_RESULT {
  728. return Join(TCharType(ch), s);
  729. }
  730. friend TBasicString operator+(TExplicitType<TCharType> ch, TBasicString&& s) Y_WARN_UNUSED_RESULT {
  731. s.prepend(ch);
  732. return std::move(s);
  733. }
  734. friend TBasicString operator+(const TBasicString& s1, const TBasicString& s2) Y_WARN_UNUSED_RESULT {
  735. return Join(s1, s2);
  736. }
  737. friend TBasicString operator+(const TBasicString& s1, const TBasicStringBuf<TCharType, TTraits> s2) Y_WARN_UNUSED_RESULT {
  738. return Join(s1, s2);
  739. }
  740. friend TBasicString operator+(const TBasicString& s1, const TCharType* s2) Y_WARN_UNUSED_RESULT {
  741. return Join(s1, s2);
  742. }
  743. friend TBasicString operator+(const TBasicString& s1, TCharType s2) Y_WARN_UNUSED_RESULT {
  744. return Join(s1, TBasicStringBuf<TCharType, TTraits>(&s2, 1));
  745. }
  746. friend TBasicString operator+(const TCharType* s1, TBasicString&& s2) Y_WARN_UNUSED_RESULT {
  747. s2.prepend(s1);
  748. return std::move(s2);
  749. }
  750. friend TBasicString operator+(const TBasicStringBuf<TCharType, TTraits> s1, TBasicString&& s2) Y_WARN_UNUSED_RESULT {
  751. s2.prepend(s1);
  752. return std::move(s2);
  753. }
  754. friend TBasicString operator+(const TBasicStringBuf<TCharType, TTraits> s1, const TBasicString& s2) Y_WARN_UNUSED_RESULT {
  755. return Join(s1, s2);
  756. }
  757. friend TBasicString operator+(const TCharType* s1, const TBasicString& s2) Y_WARN_UNUSED_RESULT {
  758. return Join(s1, s2);
  759. }
  760. friend TBasicString operator+(std::basic_string<TCharType, TTraits> l, TBasicString r) {
  761. return std::move(l) + r.ConstRef();
  762. }
  763. friend TBasicString operator+(TBasicString l, std::basic_string<TCharType, TTraits> r) {
  764. return l.ConstRef() + std::move(r);
  765. }
  766. // ~~~ Prepending ~~~ : FAMILY0(TBasicString&, prepend);
  767. TBasicString& prepend(const TBasicString& s) {
  768. MutRef().insert(0, s.ConstRef());
  769. return *this;
  770. }
  771. TBasicString& prepend(const TBasicString& s, size_t pos, size_t n) {
  772. MutRef().insert(0, s.ConstRef(), pos, n);
  773. return *this;
  774. }
  775. TBasicString& prepend(const TCharType* pc) {
  776. MutRef().insert(0, pc);
  777. return *this;
  778. }
  779. TBasicString& prepend(size_t n, TCharType c) {
  780. MutRef().insert(size_t(0), n, c);
  781. return *this;
  782. }
  783. TBasicString& prepend(TCharType c) {
  784. MutRef().insert(size_t(0), 1, c);
  785. return *this;
  786. }
  787. TBasicString& prepend(const TBasicStringBuf<TCharType, TTraits> s, size_t spos = 0, size_t sn = TBase::npos) {
  788. return insert(0, s, spos, sn);
  789. }
  790. // ~~~ Insertion ~~~ : FAMILY1(TBasicString&, insert, size_t pos);
  791. TBasicString& insert(size_t pos, const TBasicString& s) {
  792. MutRef().insert(pos, s.ConstRef());
  793. return *this;
  794. }
  795. TBasicString& insert(size_t pos, const TBasicString& s, size_t pos1, size_t n1) {
  796. MutRef().insert(pos, s.ConstRef(), pos1, n1);
  797. return *this;
  798. }
  799. TBasicString& insert(size_t pos, const TCharType* pc) {
  800. MutRef().insert(pos, pc);
  801. return *this;
  802. }
  803. TBasicString& insert(size_t pos, const TCharType* pc, size_t len) {
  804. MutRef().insert(pos, pc, len);
  805. return *this;
  806. }
  807. TBasicString& insert(const_iterator pos, const_iterator b, const_iterator e) {
  808. #ifdef TSTRING_IS_STD_STRING
  809. Storage_.insert(Storage_.begin() + this->off(pos), b, e);
  810. return *this;
  811. #else
  812. return insert(this->off(pos), b, e - b);
  813. #endif
  814. }
  815. TBasicString& insert(size_t pos, size_t n, TCharType c) {
  816. MutRef().insert(pos, n, c);
  817. return *this;
  818. }
  819. TBasicString& insert(const_iterator pos, size_t len, TCharType ch) {
  820. return this->insert(this->off(pos), len, ch);
  821. }
  822. TBasicString& insert(const_iterator pos, TCharType ch) {
  823. return this->insert(pos, 1, ch);
  824. }
  825. TBasicString& insert(size_t pos, const TBasicStringBuf<TCharType, TTraits> s, size_t spos = 0, size_t sn = TBase::npos) {
  826. MutRef().insert(pos, s, spos, sn);
  827. return *this;
  828. }
  829. // ~~~ Removing ~~~
  830. TBasicString& remove(size_t pos, size_t n) Y_NOEXCEPT {
  831. if (pos < length()) {
  832. MutRef().erase(pos, n);
  833. }
  834. return *this;
  835. }
  836. TBasicString& remove(size_t pos = 0) Y_NOEXCEPT {
  837. if (pos < length()) {
  838. MutRef().erase(pos);
  839. }
  840. return *this;
  841. }
  842. TBasicString& erase(size_t pos = 0, size_t n = TBase::npos) Y_NOEXCEPT {
  843. MutRef().erase(pos, n);
  844. return *this;
  845. }
  846. TBasicString& erase(const_iterator b, const_iterator e) Y_NOEXCEPT {
  847. return erase(this->off(b), e - b);
  848. }
  849. TBasicString& erase(const_iterator i) Y_NOEXCEPT {
  850. return erase(i, i + 1);
  851. }
  852. TBasicString& pop_back() Y_NOEXCEPT {
  853. Y_ASSERT(!this->empty());
  854. MutRef().pop_back();
  855. return *this;
  856. }
  857. // ~~~ replacement ~~~ : FAMILY2(TBasicString&, replace, size_t pos, size_t n);
  858. TBasicString& replace(size_t pos, size_t n, const TBasicString& s) Y_NOEXCEPT {
  859. MutRef().replace(pos, n, s.ConstRef());
  860. return *this;
  861. }
  862. TBasicString& replace(size_t pos, size_t n, const TBasicString& s, size_t pos1, size_t n1) Y_NOEXCEPT {
  863. MutRef().replace(pos, n, s.ConstRef(), pos1, n1);
  864. return *this;
  865. }
  866. TBasicString& replace(size_t pos, size_t n, const TCharType* pc) Y_NOEXCEPT {
  867. MutRef().replace(pos, n, pc);
  868. return *this;
  869. }
  870. TBasicString& replace(size_t pos, size_t n, const TCharType* s, size_t len) Y_NOEXCEPT {
  871. MutRef().replace(pos, n, s, len);
  872. return *this;
  873. }
  874. TBasicString& replace(size_t pos, size_t n, const TCharType* s, size_t spos, size_t sn) Y_NOEXCEPT {
  875. MutRef().replace(pos, n, s + spos, sn - spos);
  876. return *this;
  877. }
  878. TBasicString& replace(size_t pos, size_t n1, size_t n2, TCharType c) Y_NOEXCEPT {
  879. MutRef().replace(pos, n1, n2, c);
  880. return *this;
  881. }
  882. TBasicString& replace(size_t pos, size_t n, const TBasicStringBuf<TCharType, TTraits> s, size_t spos = 0, size_t sn = TBase::npos) Y_NOEXCEPT {
  883. MutRef().replace(pos, n, s, spos, sn);
  884. return *this;
  885. }
  886. void swap(TBasicString& s) noexcept {
  887. #ifdef TSTRING_IS_STD_STRING
  888. std::swap(Storage_, s.Storage_);
  889. #else
  890. S_.Swap(s.S_);
  891. #endif
  892. }
  893. /**
  894. * @returns String suitable for debug printing (like Python's `repr()`).
  895. * Format of the string is unspecified and may be changed over time.
  896. */
  897. TBasicString Quote() const {
  898. extern TBasicString EscapeC(const TBasicString&);
  899. return TBasicString() + '"' + EscapeC(*this) + '"';
  900. }
  901. /**
  902. * Modifies the case of the string, depending on the operation.
  903. * @return false if no changes have been made.
  904. *
  905. * @warning when the value_type is char, these methods will not work with non-ASCII letters.
  906. */
  907. bool to_lower(size_t pos = 0, size_t n = TBase::npos);
  908. bool to_upper(size_t pos = 0, size_t n = TBase::npos);
  909. bool to_title(size_t pos = 0, size_t n = TBase::npos);
  910. public:
  911. /**
  912. * Modifies the substring of length `n` starting from `pos`, applying `f` to each position and symbol.
  913. *
  914. * @return false if no changes have been made.
  915. */
  916. template <typename T>
  917. bool Transform(T&& f, size_t pos = 0, size_t n = TBase::npos) {
  918. size_t len = length();
  919. if (pos > len) {
  920. pos = len;
  921. }
  922. if (n > len - pos) {
  923. n = len - pos;
  924. }
  925. bool changed = false;
  926. for (size_t i = pos; i != pos + n; ++i) {
  927. #ifdef TSTRING_IS_STD_STRING
  928. auto c = f(i, Storage_[i]);
  929. if (c != Storage_[i]) {
  930. changed = true;
  931. Storage_[i] = c;
  932. }
  933. #else
  934. auto c = f(i, data()[i]);
  935. if (c != data()[i]) {
  936. if (!changed) {
  937. Detach();
  938. changed = true;
  939. }
  940. begin()[i] = c;
  941. }
  942. #endif
  943. }
  944. return changed;
  945. }
  946. };
  947. std::ostream& operator<<(std::ostream&, const TString&);
  948. std::istream& operator>>(std::istream&, TString&);
  949. template <typename TCharType, typename TTraits>
  950. TBasicString<TCharType> to_lower(const TBasicString<TCharType, TTraits>& s) {
  951. TBasicString<TCharType> ret(s);
  952. ret.to_lower();
  953. return ret;
  954. }
  955. template <typename TCharType, typename TTraits>
  956. TBasicString<TCharType> to_upper(const TBasicString<TCharType, TTraits>& s) {
  957. TBasicString<TCharType> ret(s);
  958. ret.to_upper();
  959. return ret;
  960. }
  961. template <typename TCharType, typename TTraits>
  962. TBasicString<TCharType> to_title(const TBasicString<TCharType, TTraits>& s) {
  963. TBasicString<TCharType> ret(s);
  964. ret.to_title();
  965. return ret;
  966. }
  967. namespace std {
  968. template <>
  969. struct hash<TString> {
  970. using argument_type = TString;
  971. using result_type = size_t;
  972. inline result_type operator()(argument_type const& s) const noexcept {
  973. return NHashPrivate::ComputeStringHash(s.data(), s.size());
  974. }
  975. };
  976. }
  977. #undef Y_NOEXCEPT
  978. template <class S>
  979. inline S LegacySubstr(const S& s, size_t pos, size_t n = S::npos) {
  980. size_t len = s.length();
  981. pos = Min(pos, len);
  982. n = Min(n, len - pos);
  983. return S(s, pos, n);
  984. }
  985. template <typename S, typename... Args>
  986. inline S&& LegacyReplace(S&& s, size_t pos, Args&&... args) {
  987. if (pos <= s.length()) {
  988. s.replace(pos, std::forward<Args>(args)...);
  989. }
  990. return s;
  991. }
  992. template <typename S, typename... Args>
  993. inline S&& LegacyErase(S&& s, size_t pos, Args&&... args) {
  994. if (pos <= s.length()) {
  995. s.erase(pos, std::forward<Args>(args)...);
  996. }
  997. return s;
  998. }
  999. inline const char* LegacyStr(const char* s) noexcept {
  1000. return s ? s : "";
  1001. }
  1002. // interop
  1003. template <class TCharType, class TTraits>
  1004. auto& MutRef(TBasicString<TCharType, TTraits>& s) {
  1005. return s.MutRef();
  1006. }
  1007. template <class TCharType, class TTraits>
  1008. const auto& ConstRef(const TBasicString<TCharType, TTraits>& s) noexcept {
  1009. return s.ConstRef();
  1010. }
  1011. template <class TCharType, class TCharTraits, class TAllocator>
  1012. auto& MutRef(std::basic_string<TCharType, TCharTraits, TAllocator>& s) noexcept {
  1013. return s;
  1014. }
  1015. template <class TCharType, class TCharTraits, class TAllocator>
  1016. const auto& ConstRef(const std::basic_string<TCharType, TCharTraits, TAllocator>& s) noexcept {
  1017. return s;
  1018. }
  1019. template <class TCharType, class TTraits>
  1020. void ResizeUninitialized(TBasicString<TCharType, TTraits>& s, size_t len) {
  1021. s.ReserveAndResize(len);
  1022. }