catboost 1.25.1 → 1.26.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (289) hide show
  1. package/DEPLOYMENT.md +22 -15
  2. package/README.md +37 -27
  3. package/binding.gyp +5 -7
  4. package/build_scripts/bootstrap.js +2 -1
  5. package/build_scripts/out/build.js +46 -68
  6. package/build_scripts/out/build_model.js +1 -1
  7. package/build_scripts/out/{build_ya.js → build_native.js} +1 -1
  8. package/build_scripts/out/ci.js +5 -5
  9. package/build_scripts/out/config.js +32 -18
  10. package/build_scripts/out/install.js +5 -3
  11. package/build_scripts/out/package_prepublish.js +1 -1
  12. package/build_scripts/out/packaging.js +1 -19
  13. package/build_scripts/out/run_tests.js +1 -1
  14. package/build_scripts/out/test.js +8 -3
  15. package/config.json +18 -11
  16. package/inc/catboost/libs/model_interface/c_api.h +349 -3
  17. package/lib/catboost.d.ts +65 -21
  18. package/package.json +4 -4
  19. package/src/api_helpers.cpp +100 -24
  20. package/src/api_helpers.h +8 -7
  21. package/src/api_module.cpp +1 -2
  22. package/src/model.cpp +483 -83
  23. package/src/model.h +24 -9
  24. package/inc/contrib/libs/cxxsupp/system_stl/include/stlfwd +0 -14
  25. package/inc/util/charset/recode_result.h +0 -9
  26. package/inc/util/charset/unicode_table.h +0 -123
  27. package/inc/util/charset/unidata.h +0 -421
  28. package/inc/util/charset/utf8.h +0 -384
  29. package/inc/util/charset/wide.h +0 -843
  30. package/inc/util/charset/wide_specific.h +0 -22
  31. package/inc/util/datetime/base.h +0 -669
  32. package/inc/util/datetime/constants.h +0 -7
  33. package/inc/util/datetime/cputimer.h +0 -124
  34. package/inc/util/datetime/parser.h +0 -292
  35. package/inc/util/datetime/systime.h +0 -47
  36. package/inc/util/datetime/uptime.h +0 -8
  37. package/inc/util/digest/city.h +0 -88
  38. package/inc/util/digest/fnv.h +0 -73
  39. package/inc/util/digest/multi.h +0 -14
  40. package/inc/util/digest/murmur.h +0 -57
  41. package/inc/util/digest/numeric.h +0 -86
  42. package/inc/util/digest/sequence.h +0 -48
  43. package/inc/util/draft/date.h +0 -129
  44. package/inc/util/draft/datetime.h +0 -184
  45. package/inc/util/draft/enum.h +0 -136
  46. package/inc/util/draft/holder_vector.h +0 -102
  47. package/inc/util/draft/ip.h +0 -131
  48. package/inc/util/draft/matrix.h +0 -108
  49. package/inc/util/draft/memory.h +0 -40
  50. package/inc/util/folder/dirent_win.h +0 -46
  51. package/inc/util/folder/dirut.h +0 -121
  52. package/inc/util/folder/filelist.h +0 -81
  53. package/inc/util/folder/fts.h +0 -108
  54. package/inc/util/folder/iterator.h +0 -109
  55. package/inc/util/folder/lstat_win.h +0 -20
  56. package/inc/util/folder/path.h +0 -225
  57. package/inc/util/folder/pathsplit.h +0 -113
  58. package/inc/util/folder/tempdir.h +0 -42
  59. package/inc/util/generic/adaptor.h +0 -134
  60. package/inc/util/generic/algorithm.h +0 -765
  61. package/inc/util/generic/array_ref.h +0 -282
  62. package/inc/util/generic/array_size.h +0 -24
  63. package/inc/util/generic/benchmark/vector_count_ctor/f.h +0 -9
  64. package/inc/util/generic/bitmap.h +0 -1115
  65. package/inc/util/generic/bitops.h +0 -459
  66. package/inc/util/generic/bt_exception.h +0 -24
  67. package/inc/util/generic/buffer.h +0 -232
  68. package/inc/util/generic/cast.h +0 -176
  69. package/inc/util/generic/deque.h +0 -24
  70. package/inc/util/generic/explicit_type.h +0 -42
  71. package/inc/util/generic/fastqueue.h +0 -55
  72. package/inc/util/generic/flags.h +0 -244
  73. package/inc/util/generic/function.h +0 -103
  74. package/inc/util/generic/fwd.h +0 -171
  75. package/inc/util/generic/guid.h +0 -61
  76. package/inc/util/generic/hash.h +0 -2032
  77. package/inc/util/generic/hash_primes.h +0 -140
  78. package/inc/util/generic/hash_set.h +0 -490
  79. package/inc/util/generic/hide_ptr.h +0 -3
  80. package/inc/util/generic/intrlist.h +0 -876
  81. package/inc/util/generic/is_in.h +0 -53
  82. package/inc/util/generic/iterator.h +0 -137
  83. package/inc/util/generic/iterator_range.h +0 -105
  84. package/inc/util/generic/lazy_value.h +0 -66
  85. package/inc/util/generic/list.h +0 -22
  86. package/inc/util/generic/map.h +0 -44
  87. package/inc/util/generic/mapfindptr.h +0 -60
  88. package/inc/util/generic/maybe.h +0 -713
  89. package/inc/util/generic/maybe_traits.h +0 -164
  90. package/inc/util/generic/mem_copy.h +0 -55
  91. package/inc/util/generic/noncopyable.h +0 -38
  92. package/inc/util/generic/object_counter.h +0 -53
  93. package/inc/util/generic/ptr.h +0 -1113
  94. package/inc/util/generic/queue.h +0 -57
  95. package/inc/util/generic/refcount.h +0 -162
  96. package/inc/util/generic/reserve.h +0 -11
  97. package/inc/util/generic/scope.h +0 -65
  98. package/inc/util/generic/serialized_enum.h +0 -406
  99. package/inc/util/generic/set.h +0 -42
  100. package/inc/util/generic/singleton.h +0 -136
  101. package/inc/util/generic/size_literals.h +0 -65
  102. package/inc/util/generic/stack.h +0 -18
  103. package/inc/util/generic/store_policy.h +0 -120
  104. package/inc/util/generic/strbase.h +0 -612
  105. package/inc/util/generic/strbuf.h +0 -552
  106. package/inc/util/generic/strfcpy.h +0 -17
  107. package/inc/util/generic/string.h +0 -1572
  108. package/inc/util/generic/string_hash.h +0 -21
  109. package/inc/util/generic/string_ut.h +0 -1175
  110. package/inc/util/generic/type_name.h +0 -34
  111. package/inc/util/generic/typelist.h +0 -114
  112. package/inc/util/generic/typetraits.h +0 -325
  113. package/inc/util/generic/utility.h +0 -132
  114. package/inc/util/generic/va_args.h +0 -400
  115. package/inc/util/generic/variant.h +0 -631
  116. package/inc/util/generic/variant_traits.h +0 -171
  117. package/inc/util/generic/vector.h +0 -119
  118. package/inc/util/generic/xrange.h +0 -258
  119. package/inc/util/generic/yexception.h +0 -212
  120. package/inc/util/generic/yexception_ut.h +0 -14
  121. package/inc/util/generic/ylimits.h +0 -92
  122. package/inc/util/generic/ymath.h +0 -206
  123. package/inc/util/memory/addstorage.h +0 -93
  124. package/inc/util/memory/alloc.h +0 -27
  125. package/inc/util/memory/blob.h +0 -296
  126. package/inc/util/memory/mmapalloc.h +0 -8
  127. package/inc/util/memory/pool.h +0 -432
  128. package/inc/util/memory/segmented_string_pool.h +0 -194
  129. package/inc/util/memory/segpool_alloc.h +0 -118
  130. package/inc/util/memory/smallobj.h +0 -141
  131. package/inc/util/memory/tempbuf.h +0 -111
  132. package/inc/util/network/address.h +0 -136
  133. package/inc/util/network/endpoint.h +0 -61
  134. package/inc/util/network/hostip.h +0 -16
  135. package/inc/util/network/init.h +0 -60
  136. package/inc/util/network/interface.h +0 -17
  137. package/inc/util/network/iovec.h +0 -65
  138. package/inc/util/network/ip.h +0 -116
  139. package/inc/util/network/nonblock.h +0 -8
  140. package/inc/util/network/pair.h +0 -9
  141. package/inc/util/network/poller.h +0 -58
  142. package/inc/util/network/pollerimpl.h +0 -707
  143. package/inc/util/network/sock.h +0 -608
  144. package/inc/util/network/socket.h +0 -421
  145. package/inc/util/random/common_ops.h +0 -130
  146. package/inc/util/random/easy.h +0 -47
  147. package/inc/util/random/entropy.h +0 -21
  148. package/inc/util/random/fast.h +0 -101
  149. package/inc/util/random/init_atfork.h +0 -3
  150. package/inc/util/random/lcg_engine.h +0 -66
  151. package/inc/util/random/mersenne.h +0 -46
  152. package/inc/util/random/mersenne32.h +0 -50
  153. package/inc/util/random/mersenne64.h +0 -50
  154. package/inc/util/random/normal.h +0 -38
  155. package/inc/util/random/random.h +0 -30
  156. package/inc/util/random/shuffle.h +0 -39
  157. package/inc/util/str_stl.h +0 -266
  158. package/inc/util/stream/aligned.h +0 -99
  159. package/inc/util/stream/buffer.h +0 -119
  160. package/inc/util/stream/buffered.h +0 -225
  161. package/inc/util/stream/debug.h +0 -53
  162. package/inc/util/stream/direct_io.h +0 -43
  163. package/inc/util/stream/file.h +0 -108
  164. package/inc/util/stream/format.h +0 -444
  165. package/inc/util/stream/fwd.h +0 -100
  166. package/inc/util/stream/hex.h +0 -8
  167. package/inc/util/stream/holder.h +0 -44
  168. package/inc/util/stream/input.h +0 -273
  169. package/inc/util/stream/labeled.h +0 -19
  170. package/inc/util/stream/length.h +0 -100
  171. package/inc/util/stream/mem.h +0 -255
  172. package/inc/util/stream/multi.h +0 -32
  173. package/inc/util/stream/null.h +0 -61
  174. package/inc/util/stream/output.h +0 -304
  175. package/inc/util/stream/pipe.h +0 -112
  176. package/inc/util/stream/printf.h +0 -25
  177. package/inc/util/stream/str.h +0 -207
  178. package/inc/util/stream/tee.h +0 -28
  179. package/inc/util/stream/tempbuf.h +0 -21
  180. package/inc/util/stream/tokenizer.h +0 -214
  181. package/inc/util/stream/trace.h +0 -60
  182. package/inc/util/stream/walk.h +0 -35
  183. package/inc/util/stream/zerocopy.h +0 -91
  184. package/inc/util/stream/zerocopy_output.h +0 -57
  185. package/inc/util/stream/zlib.h +0 -173
  186. package/inc/util/string/ascii.h +0 -236
  187. package/inc/util/string/builder.h +0 -39
  188. package/inc/util/string/cast.h +0 -347
  189. package/inc/util/string/cstriter.h +0 -14
  190. package/inc/util/string/escape.h +0 -70
  191. package/inc/util/string/hex.h +0 -59
  192. package/inc/util/string/join.h +0 -194
  193. package/inc/util/string/printf.h +0 -13
  194. package/inc/util/string/reverse.h +0 -16
  195. package/inc/util/string/split.h +0 -1080
  196. package/inc/util/string/strip.h +0 -257
  197. package/inc/util/string/strspn.h +0 -65
  198. package/inc/util/string/subst.h +0 -56
  199. package/inc/util/string/type.h +0 -50
  200. package/inc/util/string/util.h +0 -195
  201. package/inc/util/string/vector.h +0 -132
  202. package/inc/util/system/align.h +0 -50
  203. package/inc/util/system/atexit.h +0 -22
  204. package/inc/util/system/atomic.h +0 -51
  205. package/inc/util/system/atomic_gcc.h +0 -90
  206. package/inc/util/system/atomic_ops.h +0 -189
  207. package/inc/util/system/atomic_win.h +0 -114
  208. package/inc/util/system/backtrace.h +0 -39
  209. package/inc/util/system/byteorder.h +0 -186
  210. package/inc/util/system/compat.h +0 -84
  211. package/inc/util/system/compiler.h +0 -620
  212. package/inc/util/system/condvar.h +0 -71
  213. package/inc/util/system/context.h +0 -181
  214. package/inc/util/system/context_aarch64.h +0 -8
  215. package/inc/util/system/context_i686.h +0 -9
  216. package/inc/util/system/context_x86.h +0 -12
  217. package/inc/util/system/context_x86_64.h +0 -7
  218. package/inc/util/system/cpu_id.h +0 -159
  219. package/inc/util/system/daemon.h +0 -28
  220. package/inc/util/system/datetime.h +0 -98
  221. package/inc/util/system/defaults.h +0 -149
  222. package/inc/util/system/demangle.h +0 -5
  223. package/inc/util/system/demangle_impl.h +0 -23
  224. package/inc/util/system/direct_io.h +0 -71
  225. package/inc/util/system/dynlib.h +0 -119
  226. package/inc/util/system/env.h +0 -32
  227. package/inc/util/system/error.h +0 -95
  228. package/inc/util/system/event.h +0 -122
  229. package/inc/util/system/execpath.h +0 -17
  230. package/inc/util/system/fasttime.h +0 -6
  231. package/inc/util/system/fhandle.h +0 -27
  232. package/inc/util/system/file.h +0 -210
  233. package/inc/util/system/file_lock.h +0 -34
  234. package/inc/util/system/filemap.h +0 -383
  235. package/inc/util/system/flock.h +0 -35
  236. package/inc/util/system/fs.h +0 -156
  237. package/inc/util/system/fs_win.h +0 -29
  238. package/inc/util/system/fstat.h +0 -46
  239. package/inc/util/system/getpid.h +0 -12
  240. package/inc/util/system/guard.h +0 -179
  241. package/inc/util/system/hi_lo.h +0 -139
  242. package/inc/util/system/hostname.h +0 -10
  243. package/inc/util/system/hp_timer.h +0 -36
  244. package/inc/util/system/info.h +0 -12
  245. package/inc/util/system/interrupt_signals.h +0 -22
  246. package/inc/util/system/madvise.h +0 -30
  247. package/inc/util/system/maxlen.h +0 -32
  248. package/inc/util/system/mem_info.h +0 -18
  249. package/inc/util/system/mincore.h +0 -38
  250. package/inc/util/system/mktemp.h +0 -11
  251. package/inc/util/system/mlock.h +0 -43
  252. package/inc/util/system/mutex.h +0 -67
  253. package/inc/util/system/nice.h +0 -3
  254. package/inc/util/system/pipe.h +0 -90
  255. package/inc/util/system/platform.h +0 -246
  256. package/inc/util/system/progname.h +0 -13
  257. package/inc/util/system/protect.h +0 -25
  258. package/inc/util/system/rusage.h +0 -26
  259. package/inc/util/system/rwlock.h +0 -78
  260. package/inc/util/system/sanitizers.h +0 -122
  261. package/inc/util/system/sem.h +0 -41
  262. package/inc/util/system/shellcommand.h +0 -472
  263. package/inc/util/system/shmat.h +0 -32
  264. package/inc/util/system/sigset.h +0 -78
  265. package/inc/util/system/spin_wait.h +0 -10
  266. package/inc/util/system/spinlock.h +0 -121
  267. package/inc/util/system/src_location.h +0 -25
  268. package/inc/util/system/src_root.h +0 -68
  269. package/inc/util/system/sys_alloc.h +0 -43
  270. package/inc/util/system/sysstat.h +0 -52
  271. package/inc/util/system/tempfile.h +0 -34
  272. package/inc/util/system/thread.h +0 -167
  273. package/inc/util/system/tls.h +0 -307
  274. package/inc/util/system/types.h +0 -119
  275. package/inc/util/system/unaligned_mem.h +0 -67
  276. package/inc/util/system/user.h +0 -5
  277. package/inc/util/system/utime.h +0 -6
  278. package/inc/util/system/valgrind.h +0 -48
  279. package/inc/util/system/winint.h +0 -43
  280. package/inc/util/system/yassert.h +0 -121
  281. package/inc/util/system/yield.h +0 -4
  282. package/inc/util/thread/factory.h +0 -65
  283. package/inc/util/thread/fwd.h +0 -30
  284. package/inc/util/thread/lfqueue.h +0 -406
  285. package/inc/util/thread/lfstack.h +0 -188
  286. package/inc/util/thread/pool.h +0 -388
  287. package/inc/util/thread/singleton.h +0 -42
  288. package/inc/util/ysafeptr.h +0 -427
  289. package/inc/util/ysaveload.h +0 -700
@@ -1,1572 +0,0 @@
1
- #pragma once
2
-
3
- #include <cstddef>
4
- #include <cstring>
5
- #include <stlfwd>
6
- #include <stdexcept>
7
- #ifdef TSTRING_IS_STD_STRING
8
- #include <string>
9
- #endif
10
- #include <string_view>
11
-
12
- #include <util/system/yassert.h>
13
- #include <util/system/atomic.h>
14
-
15
- #include "utility.h"
16
- #include "bitops.h"
17
- #include "explicit_type.h"
18
- #include "reserve.h"
19
- #include "strbase.h"
20
- #include "strbuf.h"
21
- #include "string_hash.h"
22
-
23
- #if defined(address_sanitizer_enabled) || defined(thread_sanitizer_enabled)
24
- #include "hide_ptr.h"
25
- #endif
26
-
27
- #ifndef TSTRING_IS_STD_STRING
28
- namespace NDetail {
29
- extern void const* STRING_DATA_NULL;
30
-
31
- /** Represents string data shared between instances of string objects. */
32
- struct TStringData {
33
- TAtomic Refs;
34
- size_t BufLen; /**< Maximum number of characters that this data can fit. */
35
- size_t Length; /**< Actual string data length. */
36
- };
37
-
38
- template <typename TCharType>
39
- struct TStringDataTraits {
40
- using TData = TStringData;
41
-
42
- enum : size_t {
43
- Overhead = sizeof(TData) + sizeof(TCharType), // + null terminated symbol
44
- MaxSize = (std::numeric_limits<size_t>::max() / 2 + 1 - Overhead) / sizeof(TCharType)
45
- };
46
-
47
- static constexpr size_t CalcAllocationSizeAndCapacity(size_t& len) noexcept {
48
- // buffer should be multiple to 2^n to fit allocator's memory block size
49
- size_t alignedSize = FastClp2(len * sizeof(TCharType) + Overhead);
50
- // calc capacity
51
- len = (alignedSize - Overhead) / sizeof(TCharType);
52
- return alignedSize;
53
- }
54
-
55
- static TData* GetData(TCharType* p) {
56
- return ((TData*)(void*)p) - 1;
57
- }
58
-
59
- static TCharType* GetChars(TData* data) {
60
- return (TCharType*)(void*)(data + 1);
61
- }
62
-
63
- static TCharType* GetNull() {
64
- return (TCharType*)STRING_DATA_NULL;
65
- }
66
- };
67
-
68
- /**
69
- * Allocates new string data that fits at least @c newLen of characters.
70
- *
71
- * @throw std::length_error
72
- */
73
- template <typename TCharType>
74
- TCharType* Allocate(size_t oldLen, size_t newLen, TStringData* oldData = nullptr);
75
-
76
- void Deallocate(void* data);
77
- }
78
-
79
- template <class TStringType>
80
- class TBasicCharRef {
81
- public:
82
- using TChar = typename TStringType::TChar;
83
-
84
- TBasicCharRef(TStringType& s, size_t pos)
85
- : S_(s)
86
- , Pos_(pos)
87
- {
88
- }
89
-
90
- operator TChar() const {
91
- return S_.at(Pos_);
92
- }
93
-
94
- TChar* operator& () {
95
- return S_.begin() + Pos_;
96
- }
97
-
98
- const TChar* operator& () const {
99
- return S_.cbegin() + Pos_;
100
- }
101
-
102
- TBasicCharRef& operator=(TChar c) {
103
- Y_ASSERT(Pos_ < S_.size());
104
-
105
- TChar* p = S_.Detach();
106
- p[Pos_] = c;
107
-
108
- return *this;
109
- }
110
-
111
- TBasicCharRef& operator=(const TBasicCharRef& other) {
112
- return this->operator=(static_cast<TChar>(other));
113
- }
114
-
115
- private:
116
- TStringType& S_;
117
- size_t Pos_;
118
- };
119
- #endif
120
-
121
- template <typename TCharType, typename TTraits>
122
- class TBasicString: public TStringBase<TBasicString<TCharType, TTraits>, TCharType, TTraits> {
123
- public:
124
- // TODO: Move to private section
125
- using TBase = TStringBase<TBasicString, TCharType, TTraits>;
126
- #ifdef TSTRING_IS_STD_STRING
127
- using TStorage = std::basic_string<TCharType>;
128
- using reference = typename TStorage::reference;
129
- #else
130
- using TDataTraits = ::NDetail::TStringDataTraits<TCharType>;
131
- using TData = typename TDataTraits::TData;
132
-
133
- using reference = TBasicCharRef<TBasicString>;
134
- #endif
135
- using char_type = TCharType; // TODO: DROP
136
- using value_type = TCharType;
137
- using traits_type = TTraits;
138
-
139
- using iterator = TCharType*;
140
- using reverse_iterator = typename TBase::template TReverseIteratorBase<iterator>;
141
- using typename TBase::const_iterator;
142
- using typename TBase::const_reverse_iterator;
143
- using typename TBase::const_reference;
144
-
145
- struct TUninitialized {
146
- explicit TUninitialized(size_t size)
147
- : Size(size)
148
- {
149
- }
150
-
151
- size_t Size;
152
- };
153
-
154
- static size_t max_size() noexcept {
155
- #ifdef TSTRING_IS_STD_STRING
156
- static size_t result = TStorage{}.max_size();
157
- return result;
158
- #else
159
- return ::NDetail::TStringDataTraits<TCharType>::MaxSize;
160
- #endif
161
- }
162
-
163
- protected:
164
- #ifdef TSTRING_IS_STD_STRING
165
- TStorage Storage_;
166
- #else
167
- /**
168
- * Allocates new string data that fits at least the specified number of characters.
169
- *
170
- * @param len Number of characters.
171
- * @throw std::length_error
172
- */
173
- static TCharType* Allocate(size_t len, TData* oldData = nullptr) {
174
- return Allocate(len, len, oldData);
175
- }
176
-
177
- static TCharType* Allocate(size_t oldLen, size_t newLen, TData* oldData) {
178
- return ::NDetail::Allocate<TCharType>(oldLen, newLen, oldData);
179
- }
180
-
181
- // ~~~ Data member ~~~
182
- TCharType* Data_;
183
-
184
- // ~~~ Core functions ~~~
185
- inline void Ref() noexcept {
186
- if (Data_ != TDataTraits::GetNull()) {
187
- AtomicIncrement(GetData()->Refs);
188
- }
189
- }
190
-
191
- inline void UnRef() noexcept {
192
- if (Data_ != TDataTraits::GetNull()) {
193
- // IsDetached() check is a common case optimization
194
- if (IsDetached() || AtomicDecrement(GetData()->Refs) == 0) {
195
- ::NDetail::Deallocate(GetData());
196
- }
197
- }
198
- }
199
-
200
- inline TData* GetData() const noexcept {
201
- return TDataTraits::GetData(Data_);
202
- }
203
-
204
- void Relink(TCharType* tmp) {
205
- UnRef();
206
- Data_ = tmp;
207
- }
208
-
209
- /**
210
- * Makes a distinct copy of this string. `IsDetached()` is always true after this call.
211
- *
212
- * @throw std::length_error
213
- */
214
- void Clone() {
215
- const size_t len = length();
216
-
217
- Relink(TTraits::copy(Allocate(len), Data_, len));
218
- }
219
-
220
- void TruncNonShared(size_t n) {
221
- GetData()->Length = n;
222
- Data_[n] = 0;
223
- }
224
-
225
- void ResizeNonShared(size_t n) {
226
- if (capacity() < n) {
227
- Data_ = Allocate(n, GetData());
228
- } else {
229
- TruncNonShared(n);
230
- }
231
- }
232
- #endif
233
-
234
- public:
235
- inline const_reference operator[](size_t pos) const noexcept {
236
- Y_ASSERT(pos <= length());
237
-
238
- return this->data()[pos];
239
- }
240
-
241
- inline reference operator[](size_t pos) noexcept {
242
- Y_ASSERT(pos <= length());
243
-
244
- #ifdef TSTRING_IS_STD_STRING
245
- return Storage_[pos];
246
- #else
247
- return reference(*this, pos);
248
- #endif
249
- }
250
-
251
- using TBase::back;
252
-
253
- inline reference back() noexcept {
254
- Y_ASSERT(!this->empty());
255
-
256
- #ifdef TSTRING_IS_STD_STRING
257
- return Storage_.back();
258
- #else
259
- if (Y_UNLIKELY(this->empty())) {
260
- return reference(*this, 0);
261
- }
262
- return reference(*this, length() - 1);
263
- #endif
264
- }
265
-
266
- using TBase::front;
267
-
268
- inline reference front() noexcept {
269
- Y_ASSERT(!this->empty());
270
- #ifdef TSTRING_IS_STD_STRING
271
- return Storage_.front();
272
- #else
273
- return reference(*this, 0);
274
- #endif
275
- }
276
-
277
- inline size_t length() const noexcept {
278
- #ifdef TSTRING_IS_STD_STRING
279
- return Storage_.length();
280
- #else
281
- return GetData()->Length;
282
- #endif
283
- }
284
-
285
- inline const TCharType* data() const noexcept {
286
- #ifdef TSTRING_IS_STD_STRING
287
- return Storage_.data();
288
- #else
289
- return Data_;
290
- #endif
291
- }
292
-
293
- inline const TCharType* c_str() const noexcept {
294
- #ifdef TSTRING_IS_STD_STRING
295
- return Storage_.c_str();
296
- #else
297
- return Data_;
298
- #endif
299
- }
300
-
301
- // ~~~ STL compatible method to obtain data pointer ~~~
302
- iterator begin() {
303
- #ifdef TSTRING_IS_STD_STRING
304
- return Storage_.data();
305
- #else
306
- Detach();
307
- return Data_;
308
- #endif
309
- }
310
-
311
- iterator vend() {
312
- #ifdef TSTRING_IS_STD_STRING
313
- return Storage_.data() + length();
314
- #else
315
- Detach();
316
- return Data_ + length();
317
- #endif
318
-
319
- }
320
-
321
- reverse_iterator rbegin() {
322
- #ifdef TSTRING_IS_STD_STRING
323
- return reverse_iterator(Storage_.data() + length());
324
- #else
325
- Detach();
326
- return reverse_iterator(Data_ + length());
327
- #endif
328
- }
329
-
330
- reverse_iterator rend() {
331
- #ifdef TSTRING_IS_STD_STRING
332
- return reverse_iterator(Storage_.data());
333
- #else
334
- Detach();
335
- return reverse_iterator(Data_);
336
- #endif
337
- }
338
-
339
- using TBase::begin; //!< const_iterator TStringBase::begin() const
340
- using TBase::cbegin; //!< const_iterator TStringBase::cbegin() const
341
- using TBase::cend; //!< const_iterator TStringBase::cend() const
342
- using TBase::crbegin; //!< const_reverse_iterator TStringBase::crbegin() const
343
- using TBase::crend; //!< const_reverse_iterator TStringBase::crend() const
344
- using TBase::end; //!< const_iterator TStringBase::end() const
345
- using TBase::rbegin; //!< const_reverse_iterator TStringBase::rbegin() const
346
- using TBase::rend; //!< const_reverse_iterator TStringBase::rend() const
347
-
348
- inline size_t reserve() const noexcept {
349
- #ifdef TSTRING_IS_STD_STRING
350
- return Storage_.capacity();
351
- #else
352
- return GetData()->BufLen;
353
- #endif
354
- }
355
-
356
- inline size_t capacity() const noexcept {
357
- #ifdef TSTRING_IS_STD_STRING
358
- return Storage_.capacity();
359
- #else
360
- return reserve();
361
- #endif
362
- }
363
-
364
- TCharType* Detach() {
365
- #ifdef TSTRING_IS_STD_STRING
366
- return Storage_.data();
367
- #else
368
- if (IsDetached()) {
369
- return Data_;
370
- }
371
-
372
- Clone();
373
- return Data_;
374
- #endif
375
- }
376
-
377
- bool IsDetached() const {
378
- #ifdef TSTRING_IS_STD_STRING
379
- return true;
380
- #else
381
- return 1 == AtomicGet(GetData()->Refs);
382
- #endif
383
- }
384
-
385
- // ~~~ Size and capacity ~~~
386
- TBasicString& resize(size_t n, TCharType c = ' ') { // remove or append
387
- #ifdef TSTRING_IS_STD_STRING
388
- Storage_.resize(n, c);
389
-
390
- return *this;
391
- #else
392
- const size_t len = length();
393
-
394
- if (n > len) {
395
- ReserveAndResize(n);
396
- TTraits::assign(Data_ + len, n - len, c);
397
-
398
- return *this;
399
- }
400
-
401
- return remove(n);
402
- #endif
403
- }
404
-
405
- // ~~~ Constructor ~~~ : FAMILY0(,TBasicString)
406
- TBasicString()
407
- #ifndef TSTRING_IS_STD_STRING
408
- : Data_(TDataTraits::GetNull())
409
- #endif
410
- {
411
- }
412
-
413
- inline TBasicString(::NDetail::TReserveTag rt)
414
- #ifndef TSTRING_IS_STD_STRING
415
- : Data_(TDataTraits::GetNull())
416
- #endif
417
- {
418
- reserve(rt.Capacity);
419
- }
420
-
421
- inline TBasicString(const TBasicString& s)
422
- #ifdef TSTRING_IS_STD_STRING
423
- : Storage_(s.Storage_)
424
- #else
425
- : Data_(s.Data_)
426
- #endif
427
- {
428
- #ifndef TSTRING_IS_STD_STRING
429
- Ref();
430
- #endif
431
- }
432
-
433
- inline TBasicString(TBasicString&& s) noexcept
434
- #ifdef TSTRING_IS_STD_STRING
435
- : Storage_(std::move(s.Storage_))
436
- #else
437
- : Data_(TDataTraits::GetNull())
438
- #endif
439
- {
440
- #ifndef TSTRING_IS_STD_STRING
441
- swap(s);
442
- #endif
443
- }
444
-
445
- template <typename T, typename A>
446
- explicit inline TBasicString(const std::basic_string<TCharType, T, A>& s)
447
- #ifdef TSTRING_IS_STD_STRING
448
- : Storage_(s)
449
- #else
450
- : Data_(TDataTraits::GetNull())
451
- #endif
452
- {
453
- #ifndef TSTRING_IS_STD_STRING
454
- AssignNoAlias(s.data(), s.length());
455
- #endif
456
- }
457
-
458
- TBasicString(const TBasicString& s, size_t pos, size_t n)
459
- #ifdef TSTRING_IS_STD_STRING
460
- : Storage_(s.Storage_, pos, n)
461
- #endif
462
- {
463
- #ifndef TSTRING_IS_STD_STRING
464
- size_t len = s.length();
465
- pos = Min(pos, len);
466
- n = Min(n, len - pos);
467
- Data_ = Allocate(n);
468
- TTraits::copy(Data_, s.Data_ + pos, n);
469
- #endif
470
- }
471
-
472
- TBasicString(const TCharType* pc)
473
- #ifdef TSTRING_IS_STD_STRING
474
- : Storage_(pc, TBase::StrLen(pc))
475
- #endif
476
- {
477
- #ifndef TSTRING_IS_STD_STRING
478
- const size_t len = TBase::StrLen(pc);
479
-
480
- Data_ = Allocate(len);
481
- TTraits::copy(Data_, pc, len);
482
- #endif
483
- }
484
-
485
- TBasicString(const TCharType* pc, size_t n)
486
- #ifdef TSTRING_IS_STD_STRING
487
- : Storage_(pc, n)
488
- #endif
489
- {
490
- #ifndef TSTRING_IS_STD_STRING
491
- Data_ = Allocate(n);
492
- TTraits::copy(Data_, pc, n);
493
- #endif
494
- }
495
-
496
- TBasicString(const TCharType* pc, size_t pos, size_t n)
497
- #ifdef TSTRING_IS_STD_STRING
498
- : Storage_(pc + pos, n)
499
- #endif
500
- {
501
- #ifndef TSTRING_IS_STD_STRING
502
- Data_ = Allocate(n);
503
- TTraits::copy(Data_, pc + pos, n);
504
- #endif
505
- }
506
-
507
- #ifdef TSTRING_IS_STD_STRING
508
- explicit TBasicString(TExplicitType<TCharType> c) {
509
- Storage_.push_back(c);
510
- }
511
- #else
512
- explicit TBasicString(TExplicitType<TCharType> c) {
513
- Data_ = Allocate(1);
514
- Data_[0] = c;
515
- }
516
- explicit TBasicString(const reference& c) {
517
- Data_ = Allocate(1);
518
- Data_[0] = c;
519
- }
520
- #endif
521
-
522
- TBasicString(size_t n, TCharType c)
523
- #ifdef TSTRING_IS_STD_STRING
524
- : Storage_(n, c)
525
- #endif
526
- {
527
- #ifndef TSTRING_IS_STD_STRING
528
- Data_ = Allocate(n);
529
- TTraits::assign(Data_, n, c);
530
- #endif
531
- }
532
-
533
- /**
534
- * Constructs an uninitialized string of size `uninitialized.Size`. The proper
535
- * way to use this ctor is via `TBasicString::Uninitialized` factory function.
536
- *
537
- * @throw std::length_error
538
- */
539
- TBasicString(TUninitialized uninitialized) {
540
- #ifdef TSTRING_IS_STD_STRING
541
- Storage_.resize_uninitialized(uninitialized.Size);
542
- #else
543
- Data_ = Allocate(uninitialized.Size);
544
- #endif
545
- }
546
-
547
- TBasicString(const TCharType* b, const TCharType* e)
548
- #ifdef TSTRING_IS_STD_STRING
549
- : Storage_(b, e - b)
550
- #endif
551
- {
552
- #ifndef TSTRING_IS_STD_STRING
553
- Data_ = Allocate(e - b);
554
- TTraits::copy(Data_, b, e - b);
555
- #endif
556
- }
557
-
558
- explicit TBasicString(const TBasicStringBuf<TCharType, TTraits> s)
559
- #ifdef TSTRING_IS_STD_STRING
560
- : Storage_(s.data(), s.size())
561
- #else
562
- : Data_(Allocate(s.size()))
563
- #endif
564
- {
565
- #ifndef TSTRING_IS_STD_STRING
566
- if (0 != s.size()) {
567
- TTraits::copy(Data_, s.data(), s.size());
568
- }
569
- #endif
570
- }
571
-
572
- template <typename Traits>
573
- explicit inline TBasicString(const std::basic_string_view<TCharType, Traits>& s)
574
- #ifdef TSTRING_IS_STD_STRING
575
- : Storage_(s)
576
- #endif
577
- {
578
- #ifndef TSTRING_IS_STD_STRING
579
- Data_ = Allocate(s.size());
580
- TTraits::copy(Data_, s.data(), s.size());
581
- #endif
582
- }
583
-
584
- /**
585
- * WARN:
586
- * Certain invocations of this method will result in link-time error.
587
- * You are free to implement corresponding methods in string.cpp if you need them.
588
- */
589
- static TBasicString FromAscii(const ::TStringBuf& s) {
590
- return TBasicString().AppendAscii(s);
591
- }
592
-
593
- static TBasicString FromUtf8(const ::TStringBuf& s) {
594
- return TBasicString().AppendUtf8(s);
595
- }
596
-
597
- static TBasicString FromUtf16(const ::TWtringBuf& s) {
598
- return TBasicString().AppendUtf16(s);
599
- }
600
-
601
- static TBasicString Uninitialized(size_t n) {
602
- return TBasicString(TUninitialized(n));
603
- }
604
-
605
- private:
606
- template <typename... R>
607
- static size_t SumLength(const TBasicStringBuf<TCharType, TTraits> s1, const R&... r) noexcept {
608
- return s1.size() + SumLength(r...);
609
- }
610
-
611
- template <typename... R>
612
- static size_t SumLength(const TCharType /*s1*/, const R&... r) noexcept {
613
- return 1 + SumLength(r...);
614
- }
615
-
616
- static constexpr size_t SumLength() noexcept {
617
- return 0;
618
- }
619
-
620
- template <typename... R>
621
- static void CopyAll(TCharType* p, const TBasicStringBuf<TCharType, TTraits> s, const R&... r) {
622
- TTraits::copy(p, s.data(), s.size());
623
- CopyAll(p + s.size(), r...);
624
- }
625
-
626
- template <typename... R, class TNextCharType, typename = std::enable_if_t<std::is_same<TCharType, TNextCharType>::value>>
627
- static void CopyAll(TCharType* p, const TNextCharType s, const R&... r) {
628
- p[0] = s;
629
- CopyAll(p + 1, r...);
630
- }
631
-
632
- static void CopyAll(TCharType*) noexcept {
633
- }
634
-
635
- public:
636
- #ifndef TSTRING_IS_STD_STRING
637
- // ~~~ Destructor ~~~
638
- inline ~TBasicString() {
639
- UnRef();
640
- }
641
- #endif
642
-
643
- inline void clear() noexcept {
644
- #ifdef TSTRING_IS_STD_STRING
645
- Storage_.clear();
646
- #else
647
- if (IsDetached()) {
648
- TruncNonShared(0);
649
- return;
650
- }
651
-
652
- Relink(TDataTraits::GetNull());
653
- #endif
654
- }
655
-
656
- template <typename... R>
657
- static inline TBasicString Join(const R&... r) {
658
- #ifdef TSTRING_IS_STD_STRING
659
- TBasicString s{TUninitialized{SumLength(r...)}};
660
-
661
- TBasicString::CopyAll(s.Storage_.data(), r...);
662
- #else
663
- TBasicString s;
664
-
665
- s.Data_ = Allocate(SumLength(r...));
666
- CopyAll(s.Data_, r...);
667
- #endif
668
-
669
- return s;
670
- }
671
-
672
- // ~~~ Assignment ~~~ : FAMILY0(TBasicString&, assign);
673
- TBasicString& assign(size_t size, TCharType ch) {
674
- ReserveAndResize(size);
675
- std::fill(begin(), vend(), ch);
676
- return *this;
677
- }
678
-
679
- TBasicString& assign(const TBasicString& s) {
680
- TBasicString(s).swap(*this);
681
-
682
- return *this;
683
- }
684
-
685
- TBasicString& assign(const TBasicString& s, size_t pos, size_t n) {
686
- return assign(TBasicString(s, pos, n));
687
- }
688
-
689
- TBasicString& assign(const TCharType* pc) {
690
- return assign(pc, TBase::StrLen(pc));
691
- }
692
-
693
- TBasicString& assign(TCharType ch) {
694
- return assign(&ch, 1);
695
- }
696
-
697
- TBasicString& assign(const TCharType* pc, size_t len) {
698
- #if defined(address_sanitizer_enabled) || defined(thread_sanitizer_enabled)
699
- pc = (const TCharType*)HidePointerOrigin((void*)pc);
700
- #endif
701
- #ifdef TSTRING_IS_STD_STRING
702
- Storage_.assign(pc, len);
703
- #else
704
-
705
- if (Y_LIKELY(IsDetached() && (pc + len <= TBase::begin() || pc >= TBase::end()))) {
706
- ResizeNonShared(len);
707
- TTraits::copy(Data_, pc, len);
708
- } else if (IsDetached() && pc == data() && capacity() >= len) {
709
- TruncNonShared(len);
710
- } else {
711
- Relink(TTraits::copy(Allocate(len), pc, len));
712
- }
713
- #endif
714
-
715
- return *this;
716
- }
717
-
718
- TBasicString& assign(const TCharType* first, const TCharType* last) {
719
- return assign(first, last - first);
720
- }
721
-
722
- TBasicString& assign(const TCharType* pc, size_t pos, size_t n) {
723
- return assign(pc + pos, n);
724
- }
725
-
726
- inline TBasicString& AssignNoAlias(const TCharType* pc, size_t len) {
727
- #ifdef TSTRING_IS_STD_STRING
728
- assign(pc, len);
729
- #else
730
- if (IsDetached()) {
731
- ResizeNonShared(len);
732
- } else {
733
- Relink(Allocate(len));
734
- }
735
-
736
- TTraits::copy(Data_, pc, len);
737
- #endif
738
-
739
- return *this;
740
- }
741
-
742
- inline TBasicString& AssignNoAlias(const TCharType* b, const TCharType* e) {
743
- #ifdef TSTRING_IS_STD_STRING
744
- return assign(b, e - b);
745
- #else
746
- return AssignNoAlias(b, e - b);
747
- #endif
748
- }
749
-
750
- TBasicString& assign(const TBasicStringBuf<TCharType, TTraits> s) {
751
- return assign(s.data(), s.size());
752
- }
753
-
754
- TBasicString& assign(const TBasicStringBuf<TCharType, TTraits> s, size_t spos, size_t sn = TBase::npos) {
755
- return assign(s.SubString(spos, sn));
756
- }
757
-
758
- TBasicString& AssignNoAlias(const TBasicStringBuf<TCharType, TTraits> s) {
759
- #ifdef TSTRING_IS_STD_STRING
760
- return assign(s.data(), s.size());
761
- #else
762
- return AssignNoAlias(s.data(), s.size());
763
- #endif
764
- }
765
-
766
- TBasicString& AssignNoAlias(const TBasicStringBuf<TCharType, TTraits> s, size_t spos, size_t sn = TBase::npos) {
767
- #ifdef TSTRING_IS_STD_STRING
768
- return assign(s.SubString(spos, sn));
769
- #else
770
- return AssignNoAlias(s.SubString(spos, sn));
771
- #endif
772
- }
773
-
774
- /**
775
- * WARN:
776
- * Certain invocations of this method will result in link-time error.
777
- * You are free to implement corresponding methods in string.cpp if you need them.
778
- */
779
- auto AssignAscii(const ::TStringBuf& s) {
780
- clear();
781
- return AppendAscii(s);
782
- }
783
-
784
- auto AssignUtf8(const ::TStringBuf& s) {
785
- clear();
786
- return AppendUtf8(s);
787
- }
788
-
789
- auto AssignUtf16(const ::TWtringBuf& s) {
790
- clear();
791
- return AppendUtf16(s);
792
- }
793
-
794
- TBasicString& operator=(const TBasicString& s) {
795
- return assign(s);
796
- }
797
-
798
- TBasicString& operator=(TBasicString&& s) noexcept {
799
- swap(s);
800
- return *this;
801
- }
802
-
803
- TBasicString& operator=(const TBasicStringBuf<TCharType, TTraits> s) {
804
- return assign(s);
805
- }
806
-
807
- TBasicString& operator=(std::initializer_list<TCharType> il) {
808
- return assign(il.begin(), il.end());
809
- }
810
-
811
- TBasicString& operator=(const TCharType* s) {
812
- return assign(s);
813
- }
814
-
815
- TBasicString& operator=(TExplicitType<TCharType> ch) {
816
- return assign(ch);
817
- }
818
-
819
- inline void reserve(size_t len) {
820
- #ifdef TSTRING_IS_STD_STRING
821
- Storage_.reserve(len);
822
- #else
823
- if (IsDetached()) {
824
- if (capacity() < len) {
825
- Data_ = Allocate(length(), len, GetData());
826
- }
827
- } else {
828
- const size_t sufficientLen = Max(length(), len);
829
- Relink(TTraits::copy(Allocate(length(), sufficientLen, nullptr), Data_, length()));
830
- }
831
- #endif
832
- }
833
-
834
- // ~~~ Appending ~~~ : FAMILY0(TBasicString&, append);
835
- inline TBasicString& append(size_t count, TCharType ch) {
836
- #ifdef TSTRING_IS_STD_STRING
837
- Storage_.append(count, ch);
838
- #else
839
- while (count--) {
840
- append(ch);
841
- }
842
- #endif
843
-
844
- return *this;
845
- }
846
-
847
- inline TBasicString& append(const TBasicString& s) {
848
- #ifdef TSTRING_IS_STD_STRING
849
- Storage_.append(s.Storage_);
850
-
851
- return *this;
852
- #else
853
- if (&s != this) {
854
- return AppendNoAlias(s.data(), s.size());
855
- }
856
-
857
- return append(s.data(), s.size());
858
- #endif
859
- }
860
-
861
- inline TBasicString& append(const TBasicString& s, size_t pos, size_t n) {
862
- #ifdef TSTRING_IS_STD_STRING
863
- Storage_.append(s.Storage_, pos, n);
864
-
865
- return *this;
866
- #else
867
- return append(s.data(), pos, n, s.size());
868
- #endif
869
- }
870
-
871
- inline TBasicString& append(const TCharType* pc) {
872
- #ifdef TSTRING_IS_STD_STRING
873
- Storage_.append(pc);
874
-
875
- return *this;
876
- #else
877
- return append(pc, TBase::StrLen(pc));
878
- #endif
879
- }
880
-
881
- inline TBasicString& append(TCharType c) {
882
- #ifdef TSTRING_IS_STD_STRING
883
- Storage_.push_back(c);
884
- #else
885
- const size_t olen = length();
886
-
887
- ReserveAndResize(olen + 1);
888
- *(Data_ + olen) = c;
889
- #endif
890
-
891
- return *this;
892
- }
893
-
894
- inline TBasicString& append(const TCharType* first, const TCharType* last) {
895
- #ifdef TSTRING_IS_STD_STRING
896
- Storage_.append(first, last);
897
-
898
- return *this;
899
- #else
900
- return append(first, last - first);
901
- #endif
902
- }
903
-
904
- inline TBasicString& append(const TCharType* pc, size_t len) {
905
- #ifdef TSTRING_IS_STD_STRING
906
- Storage_.append(pc, len);
907
-
908
- return *this;
909
- #else
910
- if (pc + len <= TBase::begin() || pc >= TBase::end()) {
911
- return AppendNoAlias(pc, len);
912
- }
913
-
914
- return append(pc, 0, len, len);
915
- #endif
916
- }
917
-
918
- inline void ReserveAndResize(size_t len) {
919
- #ifdef TSTRING_IS_STD_STRING
920
- Storage_.resize_uninitialized(len);
921
- #else
922
- if (IsDetached()) {
923
- ResizeNonShared(len);
924
- } else {
925
- Relink(TTraits::copy(Allocate(len), Data_, Min(len, length())));
926
- }
927
- #endif
928
- }
929
-
930
- inline TBasicString& AppendNoAlias(const TCharType* pc, size_t len) {
931
- #ifdef TSTRING_IS_STD_STRING
932
- return append(pc, len);
933
- #else
934
- const size_t olen = length();
935
- const size_t nlen = olen + len;
936
-
937
- ReserveAndResize(nlen);
938
- TTraits::copy(Data_ + olen, pc, len);
939
- #endif
940
-
941
- return *this;
942
- }
943
-
944
- TBasicString& AppendNoAlias(const TBasicStringBuf<TCharType, TTraits> s) {
945
- #ifdef TSTRING_IS_STD_STRING
946
- return append(s.data(), s.size());
947
- #else
948
- return AppendNoAlias(s.data(), s.size());
949
- #endif
950
- }
951
-
952
- TBasicString& AppendNoAlias(const TBasicStringBuf<TCharType, TTraits> s, size_t spos, size_t sn = TBase::npos) {
953
- #ifdef TSTRING_IS_STD_STRING
954
- return append(s.SubString(spos, sn));
955
- #else
956
- return AppendNoAlias(s.SubString(spos, sn));
957
- #endif
958
- }
959
-
960
- TBasicString& append(const TBasicStringBuf<TCharType, TTraits> s) {
961
- return append(s.data(), s.size());
962
- }
963
-
964
- TBasicString& append(const TBasicStringBuf<TCharType, TTraits> s, size_t spos, size_t sn = TBase::npos) {
965
- return append(s.SubString(spos, sn));
966
- }
967
-
968
- inline TBasicString& append(const TCharType* pc, size_t pos, size_t n, size_t pc_len = TBase::npos) {
969
- #ifdef TSTRING_IS_STD_STRING
970
- return append(pc + pos, Min(n, pc_len - pos));
971
- #else
972
- return replace(length(), 0, pc, pos, n, pc_len);
973
- #endif
974
- }
975
-
976
- /**
977
- * WARN:
978
- * Certain invocations of this method will result in link-time error.
979
- * You are free to implement corresponding methods in string.cpp if you need them.
980
- */
981
- TBasicString& AppendAscii(const ::TStringBuf& s);
982
-
983
- TBasicString& AppendUtf8(const ::TStringBuf& s);
984
-
985
- TBasicString& AppendUtf16(const ::TWtringBuf& s);
986
-
987
- inline void push_back(TCharType c) {
988
- append(c);
989
- }
990
-
991
- template <class T>
992
- TBasicString& operator+=(const T& s) {
993
- return append(s);
994
- }
995
-
996
- template <class T>
997
- friend TBasicString operator*(const TBasicString& s, T count) {
998
- TBasicString result;
999
-
1000
- for (T i = 0; i < count; ++i) {
1001
- result += s;
1002
- }
1003
-
1004
- return result;
1005
- }
1006
-
1007
- template <class T>
1008
- TBasicString& operator*=(T count) {
1009
- TBasicString temp;
1010
-
1011
- for (T i = 0; i < count; ++i) {
1012
- temp += *this;
1013
- }
1014
-
1015
- swap(temp);
1016
-
1017
- return *this;
1018
- }
1019
-
1020
- template <class TCharTraits, class Allocator>
1021
- /* implicit */ operator std::basic_string<TCharType, TCharTraits, Allocator>() const {
1022
- // NB(eeight) MSVC cannot compiler direct reference to TBase::operator std::basic_string<...>
1023
- // so we are using static_cast to force the needed operator call.
1024
- return static_cast<std::basic_string<TCharType, TCharTraits, Allocator>>(
1025
- static_cast<const TBase&>(*this));
1026
- }
1027
-
1028
- /*
1029
- * Following overloads of "operator+" aim to choose the cheapest implementation depending on
1030
- * summand types: lvalues, detached rvalues, shared rvalues.
1031
- *
1032
- * General idea is to use the detached-rvalue argument (left of right) to store the result
1033
- * wherever possible. If a buffer in rvalue is large enough this saves a re-allocation. If
1034
- * both arguments are rvalues we check which one is detached. If both of them are detached then
1035
- * the left argument is obviously preferrable because you won't need to shift the data.
1036
- *
1037
- * If an rvalue is shared then it's basically the same as lvalue because you cannot use its
1038
- * buffer to store the sum. However, we rely on the fact that append() and prepend() are already
1039
- * optimized for the shared case and detach the string into the buffer large enough to store
1040
- * the sum (compared to the detach+reallocation). This way, if we have only one rvalue argument
1041
- * (left or right) then we simply append/prepend into it, without checking if it's detached or
1042
- * not. This will be checked inside ReserveAndResize anyway.
1043
- *
1044
- * If both arguments cannot be used to store the sum (e.g. two lvalues) then we fall back to the
1045
- * Join function that constructs a resulting string in the new buffer with the minimum overhead:
1046
- * malloc + memcpy + memcpy.
1047
- */
1048
-
1049
- friend TBasicString operator+(TBasicString&& s1, const TBasicString& s2) Y_WARN_UNUSED_RESULT {
1050
- s1 += s2;
1051
- return std::move(s1);
1052
- }
1053
-
1054
- friend TBasicString operator+(const TBasicString& s1, TBasicString&& s2) Y_WARN_UNUSED_RESULT {
1055
- s2.prepend(s1);
1056
- return std::move(s2);
1057
- }
1058
-
1059
- friend TBasicString operator+(TBasicString&& s1, TBasicString&& s2) Y_WARN_UNUSED_RESULT {
1060
- #ifndef TSTRING_IS_STD_STRING
1061
- if (!s1.IsDetached() && s2.IsDetached()) {
1062
- s2.prepend(s1);
1063
- return std::move(s2);
1064
- }
1065
- #endif
1066
- s1 += s2;
1067
- return std::move(s1);
1068
- }
1069
-
1070
- friend TBasicString operator+(TBasicString&& s1, const TBasicStringBuf<TCharType, TTraits> s2) Y_WARN_UNUSED_RESULT {
1071
- s1 += s2;
1072
- return std::move(s1);
1073
- }
1074
-
1075
- friend TBasicString operator+(TBasicString&& s1, const TCharType* s2) Y_WARN_UNUSED_RESULT {
1076
- s1 += s2;
1077
- return std::move(s1);
1078
- }
1079
-
1080
- friend TBasicString operator+(TBasicString&& s1, TCharType s2) Y_WARN_UNUSED_RESULT {
1081
- s1 += s2;
1082
- return std::move(s1);
1083
- }
1084
-
1085
- friend TBasicString operator+ (TExplicitType<TCharType> ch, const TBasicString& s) Y_WARN_UNUSED_RESULT {
1086
- return Join(TCharType(ch), s);
1087
- }
1088
-
1089
- friend TBasicString operator+(const TBasicString& s1, const TBasicString& s2) Y_WARN_UNUSED_RESULT {
1090
- return Join(s1, s2);
1091
- }
1092
-
1093
- friend TBasicString operator+(const TBasicString& s1, const TBasicStringBuf<TCharType, TTraits> s2) Y_WARN_UNUSED_RESULT {
1094
- return Join(s1, s2);
1095
- }
1096
-
1097
- friend TBasicString operator+(const TBasicString& s1, const TCharType* s2) Y_WARN_UNUSED_RESULT {
1098
- return Join(s1, s2);
1099
- }
1100
-
1101
- friend TBasicString operator+(const TBasicString& s1, TCharType s2) Y_WARN_UNUSED_RESULT {
1102
- return Join(s1, TBasicStringBuf<TCharType, TTraits>(&s2, 1));
1103
- }
1104
-
1105
- friend TBasicString operator+(const TCharType* s1, TBasicString&& s2) Y_WARN_UNUSED_RESULT {
1106
- s2.prepend(s1);
1107
- return std::move(s2);
1108
- }
1109
-
1110
- friend TBasicString operator+(const TBasicStringBuf<TCharType, TTraits> s1, TBasicString&& s2) Y_WARN_UNUSED_RESULT {
1111
- s2.prepend(s1);
1112
- return std::move(s2);
1113
- }
1114
-
1115
- friend TBasicString operator+(const TBasicStringBuf<TCharType, TTraits> s1, const TBasicString& s2) Y_WARN_UNUSED_RESULT {
1116
- return Join(s1, s2);
1117
- }
1118
-
1119
- friend TBasicString operator+(const TCharType* s1, const TBasicString& s2) Y_WARN_UNUSED_RESULT {
1120
- return Join(s1, s2);
1121
- }
1122
-
1123
- // ~~~ Prepending ~~~ : FAMILY0(TBasicString&, prepend);
1124
- TBasicString& prepend(const TBasicString& s) {
1125
- #ifdef TSTRING_IS_STD_STRING
1126
- Storage_.insert(0, s.Storage_);
1127
-
1128
- return *this;
1129
- #else
1130
- return replace(0, 0, s.Data_, 0, TBase::npos, s.length());
1131
- #endif
1132
- }
1133
-
1134
- TBasicString& prepend(const TBasicString& s, size_t pos, size_t n) {
1135
- #ifdef TSTRING_IS_STD_STRING
1136
- Storage_.insert(0, s.Storage_, pos, n);
1137
-
1138
- return *this;
1139
- #else
1140
- return replace(0, 0, s.Data_, pos, n, s.length());
1141
- #endif
1142
- }
1143
-
1144
- TBasicString& prepend(const TCharType* pc) {
1145
- #ifdef TSTRING_IS_STD_STRING
1146
- Storage_.insert(0, pc);
1147
-
1148
- return *this;
1149
- #else
1150
- return replace(0, 0, pc);
1151
- #endif
1152
- }
1153
-
1154
- TBasicString& prepend(size_t n, TCharType c) {
1155
- #ifdef TSTRING_IS_STD_STRING
1156
- Storage_.insert(size_t(0), n, c);
1157
-
1158
- return *this;
1159
- #else
1160
- return insert(size_t(0), n, c);
1161
- #endif
1162
- }
1163
-
1164
- TBasicString& prepend(TCharType c) {
1165
- #ifdef TSTRING_IS_STD_STRING
1166
- Storage_.insert(size_t(0), 1, c);
1167
-
1168
- return *this;
1169
- #else
1170
- return replace(0, 0, &c, 0, 1, 1);
1171
- #endif
1172
- }
1173
-
1174
- TBasicString& prepend(const TBasicStringBuf<TCharType, TTraits> s, size_t spos = 0, size_t sn = TBase::npos) {
1175
- #ifdef TSTRING_IS_STD_STRING
1176
- Storage_.insert(0, s, spos, sn);
1177
-
1178
- return *this;
1179
- #else
1180
- return insert(0, s, spos, sn);
1181
- #endif
1182
- }
1183
-
1184
- // ~~~ Insertion ~~~ : FAMILY1(TBasicString&, insert, size_t pos);
1185
- TBasicString& insert(size_t pos, const TBasicString& s) {
1186
- #ifdef TSTRING_IS_STD_STRING
1187
- Storage_.insert(pos, s.Storage_);
1188
-
1189
- return *this;
1190
- #else
1191
- return replace(pos, 0, s.Data_, 0, TBase::npos, s.length());
1192
- #endif
1193
- }
1194
-
1195
- TBasicString& insert(size_t pos, const TBasicString& s, size_t pos1, size_t n1) {
1196
- #ifdef TSTRING_IS_STD_STRING
1197
- Storage_.insert(pos, s.Storage_, pos1, n1);
1198
-
1199
- return *this;
1200
- #else
1201
- return replace(pos, 0, s.Data_, pos1, n1, s.length());
1202
- #endif
1203
- }
1204
-
1205
- TBasicString& insert(size_t pos, const TCharType* pc) {
1206
- #ifdef TSTRING_IS_STD_STRING
1207
- Storage_.insert(pos, pc);
1208
-
1209
- return *this;
1210
- #else
1211
- return replace(pos, 0, pc);
1212
- #endif
1213
- }
1214
-
1215
- TBasicString& insert(size_t pos, const TCharType* pc, size_t len) {
1216
- #ifdef TSTRING_IS_STD_STRING
1217
- Storage_.insert(pos, pc, len);
1218
-
1219
- return *this;
1220
- #else
1221
- return insert(pos, TBasicStringBuf<TCharType, TTraits>(pc, len));
1222
- #endif
1223
- }
1224
-
1225
- TBasicString& insert(const_iterator pos, const_iterator b, const_iterator e) {
1226
- #ifdef TSTRING_IS_STD_STRING
1227
- Storage_.insert(Storage_.begin() + this->off(pos), b, e);
1228
-
1229
- return *this;
1230
- #else
1231
- return insert(this->off(pos), b, e - b);
1232
- #endif
1233
- }
1234
-
1235
- TBasicString& insert(size_t pos, size_t n, TCharType c) {
1236
- #ifdef TSTRING_IS_STD_STRING
1237
- Storage_.insert(pos, n, c);
1238
-
1239
- return *this;
1240
- #else
1241
- if (n == 1) {
1242
- return replace(pos, 0, &c, 0, 1, 1);
1243
- } else {
1244
- return insert(pos, TBasicString(n, c));
1245
- }
1246
- #endif
1247
- }
1248
-
1249
- TBasicString& insert(const_iterator pos, size_t len, TCharType ch) {
1250
- #ifdef TSTRING_IS_STD_STRING
1251
- Storage_.insert(this->off(pos), len, ch);
1252
-
1253
- return *this;
1254
- #else
1255
- return this->insert(this->off(pos), len, ch);
1256
- #endif
1257
- }
1258
-
1259
- TBasicString& insert(const_iterator pos, TCharType ch) {
1260
- #ifdef TSTRING_IS_STD_STRING
1261
- Storage_.insert(this->off(pos), 1, ch);
1262
-
1263
- return *this;
1264
- #else
1265
- return this->insert(pos, 1, ch);
1266
- #endif
1267
- }
1268
-
1269
- TBasicString& insert(size_t pos, const TBasicStringBuf<TCharType, TTraits> s, size_t spos = 0, size_t sn = TBase::npos) {
1270
- #ifdef TSTRING_IS_STD_STRING
1271
- Storage_.insert(pos, s, spos, sn);
1272
-
1273
- return *this;
1274
- #else
1275
- return replace(pos, 0, s, spos, sn);
1276
- #endif
1277
- }
1278
-
1279
- // ~~~ Removing ~~~
1280
- TBasicString& remove(size_t pos, size_t n) {
1281
- #ifdef TSTRING_IS_STD_STRING
1282
- Storage_.erase(pos, n);
1283
-
1284
- return *this;
1285
- #else
1286
- return replace(pos, n, TDataTraits::GetNull(), 0, 0, 0);
1287
- #endif
1288
- }
1289
-
1290
- TBasicString& remove(size_t pos = 0) {
1291
- if (pos < length()) {
1292
- #ifdef TSTRING_IS_STD_STRING
1293
- Storage_.erase(pos);
1294
- #else
1295
- Detach();
1296
- TruncNonShared(pos);
1297
- #endif
1298
- }
1299
-
1300
- return *this;
1301
- }
1302
-
1303
- TBasicString& erase(size_t pos = 0, size_t n = TBase::npos) {
1304
- #ifdef TSTRING_IS_STD_STRING
1305
- Storage_.erase(pos, n);
1306
-
1307
- return *this;
1308
- #else
1309
- return remove(pos, n);
1310
- #endif
1311
- }
1312
-
1313
- TBasicString& erase(const_iterator b, const_iterator e) {
1314
- return erase(this->off(b), e - b);
1315
- }
1316
-
1317
- TBasicString& erase(const_iterator i) {
1318
- return erase(i, i + 1);
1319
- }
1320
-
1321
- TBasicString& pop_back() {
1322
- Y_ASSERT(!this->empty());
1323
- #ifdef TSTRING_IS_STD_STRING
1324
- Storage_.pop_back();
1325
-
1326
- return *this;
1327
- #else
1328
- return erase(this->length() - 1, 1);
1329
- #endif
1330
- }
1331
-
1332
- // ~~~ replacement ~~~ : FAMILY2(TBasicString&, replace, size_t pos, size_t n);
1333
- TBasicString& replace(size_t pos, size_t n, const TBasicString& s) {
1334
- #ifdef TSTRING_IS_STD_STRING
1335
- Storage_.replace(pos, n, s.Storage_);
1336
-
1337
- return *this;
1338
- #else
1339
- return replace(pos, n, s.Data_, 0, TBase::npos, s.length());
1340
- #endif
1341
- }
1342
-
1343
- TBasicString& replace(size_t pos, size_t n, const TBasicString& s, size_t pos1, size_t n1) {
1344
- #ifdef TSTRING_IS_STD_STRING
1345
- Storage_.replace(pos, n, s.Storage_, pos1, n1);
1346
-
1347
- return *this;
1348
- #else
1349
- return replace(pos, n, s.Data_, pos1, n1, s.length());
1350
- #endif
1351
- }
1352
-
1353
- TBasicString& replace(size_t pos, size_t n, const TCharType* pc) {
1354
- #ifdef TSTRING_IS_STD_STRING
1355
- Storage_.replace(pos, n, pc);
1356
-
1357
- return *this;
1358
- #else
1359
- return replace(pos, n, TBasicStringBuf<TCharType, TTraits>(pc));
1360
- #endif
1361
- }
1362
-
1363
- TBasicString& replace(size_t pos, size_t n, const TCharType* s, size_t len) {
1364
- #ifdef TSTRING_IS_STD_STRING
1365
- Storage_.replace(pos, n, s, len);
1366
-
1367
- return *this;
1368
- #else
1369
- return replace(pos, n, s, 0, len, len);
1370
- #endif
1371
- }
1372
-
1373
- TBasicString& replace(size_t pos, size_t n, const TCharType* s, size_t spos, size_t sn) {
1374
- #ifdef TSTRING_IS_STD_STRING
1375
- Storage_.replace(pos, n, s + spos, sn - spos);
1376
-
1377
- return *this;
1378
- #else
1379
- return replace(pos, n, s, spos, sn, sn);
1380
- #endif
1381
- }
1382
-
1383
- TBasicString& replace(size_t pos, size_t n1, size_t n2, TCharType c) {
1384
- #ifdef TSTRING_IS_STD_STRING
1385
- Storage_.replace(pos, n1, n2, c);
1386
-
1387
- return *this;
1388
- #else
1389
- if (n2 == 1) {
1390
- return replace(pos, n1, &c, 0, 1, 1);
1391
- } else {
1392
- return replace(pos, n1, TBasicString(n2, c));
1393
- }
1394
- #endif
1395
- }
1396
-
1397
- TBasicString& replace(size_t pos, size_t n, const TBasicStringBuf<TCharType, TTraits> s, size_t spos = 0, size_t sn = TBase::npos) {
1398
- #ifdef TSTRING_IS_STD_STRING
1399
- Storage_.replace(pos, n, s, spos, sn);
1400
-
1401
- return *this;
1402
- #else
1403
- return replace(pos, n, s.data(), spos, sn, s.size());
1404
- #endif
1405
- }
1406
-
1407
- #ifndef TSTRING_IS_STD_STRING
1408
- private:
1409
- // ~~~ main driver
1410
- TBasicString& replace(size_t pos, size_t del, const TCharType* pc, size_t pos1, size_t ins, size_t len1) {
1411
- size_t len = length();
1412
- // 'pc' can point to a single character that is not null terminated, so in this case TTraits::length must not be called
1413
- len1 = pc ? (len1 == TBase::npos ? (ins == TBase::npos ? TTraits::length(pc) : NStringPrivate::GetStringLengthWithLimit(pc, ins + pos1)) : len1) : 0;
1414
-
1415
- pos = Min(pos, len);
1416
- pos1 = Min(pos1, len1);
1417
-
1418
- del = Min(del, len - pos);
1419
- ins = Min(ins, len1 - pos1);
1420
-
1421
- if (len - del > this->max_size() - ins) { // len-del+ins -- overflow
1422
- throw std::length_error("TBasicString::replace");
1423
- }
1424
-
1425
- size_t total = len - del + ins;
1426
-
1427
- if (!total) {
1428
- clear();
1429
- return *this;
1430
- }
1431
-
1432
- size_t rem = len - del - pos;
1433
-
1434
- if (!IsDetached() || (pc && (pc >= Data_ && pc < Data_ + len))) {
1435
- // malloc
1436
- // 1. alias
1437
- // 2. overlapped
1438
- TCharType* temp = Allocate(total);
1439
- TTraits::copy(temp, Data_, pos);
1440
- TTraits::copy(temp + pos, pc + pos1, ins);
1441
- TTraits::copy(temp + pos + ins, Data_ + pos + del, rem);
1442
- Relink(temp);
1443
- } else if (reserve() < total) {
1444
- // realloc (increasing)
1445
- // 3. not enough room
1446
- Data_ = Allocate(total, GetData());
1447
- TTraits::move(Data_ + pos + ins, Data_ + pos + del, rem);
1448
- TTraits::copy(Data_ + pos, pc + pos1, ins);
1449
- } else {
1450
- // 1. not alias
1451
- // 2. not overlapped
1452
- // 3. enough room
1453
- // 4. not too much room
1454
- TTraits::move(Data_ + pos + ins, Data_ + pos + del, rem);
1455
- TTraits::copy(Data_ + pos, pc + pos1, ins);
1456
- //GetData()->SetLength(total);
1457
- TruncNonShared(total);
1458
- }
1459
-
1460
- return *this;
1461
- }
1462
- #endif
1463
-
1464
- public:
1465
- void swap(TBasicString& s) noexcept {
1466
- #ifdef TSTRING_IS_STD_STRING
1467
- std::swap(Storage_, s.Storage_);
1468
- #else
1469
- DoSwap(Data_, s.Data_);
1470
- #endif
1471
- }
1472
-
1473
- /**
1474
- * @returns String suitable for debug printing (like Python's `repr()`).
1475
- * Format of the string is unspecified and may be changed over time.
1476
- */
1477
- TBasicString Quote() const {
1478
- extern TBasicString EscapeC(const TBasicString&);
1479
-
1480
- return TBasicString() + '"' + EscapeC(*this) + '"';
1481
- }
1482
-
1483
- /**
1484
- * Modifies the case of the string, depending on the operation.
1485
- * @return false if no changes have been made.
1486
- *
1487
- * @warning when the value_type is char, these methods will not work with non-ASCII letters.
1488
- */
1489
- bool to_lower(size_t pos = 0, size_t n = TBase::npos);
1490
- bool to_upper(size_t pos = 0, size_t n = TBase::npos);
1491
- bool to_title(size_t pos = 0, size_t n = TBase::npos);
1492
-
1493
- public:
1494
- /**
1495
- * Modifies the substring of length `n` starting from `pos`, applying `f` to each position and symbol.
1496
- *
1497
- * @return false if no changes have been made.
1498
- */
1499
- template <typename T>
1500
- bool Transform(T&& f, size_t pos = 0, size_t n = TBase::npos) {
1501
- size_t len = length();
1502
-
1503
- if (pos > len) {
1504
- pos = len;
1505
- }
1506
-
1507
- if (n > len - pos) {
1508
- n = len - pos;
1509
- }
1510
-
1511
- bool changed = false;
1512
-
1513
- for (size_t i = pos; i != pos + n; ++i) {
1514
- #ifdef TSTRING_IS_STD_STRING
1515
- auto c = f(i, Storage_[i]);
1516
-
1517
- if (c != Storage_[i]) {
1518
- changed = true;
1519
-
1520
- Storage_[i] = c;
1521
- }
1522
- #else
1523
- auto c = f(i, Data_[i]);
1524
- if (c != Data_[i]) {
1525
- if (!changed) {
1526
- Detach();
1527
- changed = true;
1528
- }
1529
-
1530
- Data_[i] = c;
1531
- }
1532
- #endif
1533
- }
1534
-
1535
- return changed;
1536
- }
1537
- };
1538
-
1539
- std::ostream& operator<<(std::ostream&, const TString&);
1540
- std::istream& operator>>(std::istream&, TString&);
1541
-
1542
- template<typename TCharType, typename TTraits>
1543
- TBasicString<TCharType> to_lower(const TBasicString<TCharType, TTraits>& s) {
1544
- TBasicString<TCharType> ret(s);
1545
- ret.to_lower();
1546
- return ret;
1547
- }
1548
-
1549
- template<typename TCharType, typename TTraits>
1550
- TBasicString<TCharType> to_upper(const TBasicString<TCharType, TTraits>& s) {
1551
- TBasicString<TCharType> ret(s);
1552
- ret.to_upper();
1553
- return ret;
1554
- }
1555
-
1556
- template<typename TCharType, typename TTraits>
1557
- TBasicString<TCharType> to_title(const TBasicString<TCharType, TTraits>& s) {
1558
- TBasicString<TCharType> ret(s);
1559
- ret.to_title();
1560
- return ret;
1561
- }
1562
-
1563
- namespace std {
1564
- template <>
1565
- struct hash<TString> {
1566
- using argument_type = TString;
1567
- using result_type = size_t;
1568
- inline result_type operator()(argument_type const& s) const noexcept {
1569
- return NHashPrivate::ComputeStringHash(s.data(), s.size());
1570
- }
1571
- };
1572
- }