string.h 36 KB

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