format-inl.h 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355
  1. #ifndef FORMAT_INL_H_
  2. #error "Direct inclusion of this file is not allowed, include format.h"
  3. // For the sake of sane code completion.
  4. #include "format.h"
  5. #endif
  6. #include "guid.h"
  7. #include "enum.h"
  8. #include "string.h"
  9. #include <library/cpp/yt/assert/assert.h>
  10. #include <library/cpp/yt/compact_containers/compact_vector.h>
  11. #include <library/cpp/yt/containers/enum_indexed_array.h>
  12. #include <library/cpp/yt/misc/concepts.h>
  13. #include <library/cpp/yt/misc/enum.h>
  14. #include <library/cpp/yt/misc/source_location.h>
  15. #include <util/generic/maybe.h>
  16. #include <util/system/platform.h>
  17. #include <cctype>
  18. #include <optional>
  19. #include <span>
  20. #if __cplusplus >= 202302L
  21. #include <filesystem>
  22. #endif
  23. #ifdef __cpp_lib_source_location
  24. #include <source_location>
  25. #endif // __cpp_lib_source_location
  26. namespace NYT {
  27. ////////////////////////////////////////////////////////////////////////////////
  28. inline void FormatValue(TStringBuilderBase* builder, const TStringBuilder& value, TStringBuf /*spec*/)
  29. {
  30. builder->AppendString(value.GetBuffer());
  31. }
  32. ////////////////////////////////////////////////////////////////////////////////
  33. template <class T>
  34. TString ToStringViaBuilder(const T& value, TStringBuf spec)
  35. {
  36. TStringBuilder builder;
  37. FormatValue(&builder, value, spec);
  38. return builder.Flush();
  39. }
  40. ////////////////////////////////////////////////////////////////////////////////
  41. // Compatibility for users of NYT::ToString(nyt_type).
  42. template <CFormattable T>
  43. TString ToString(const T& t)
  44. {
  45. return ToStringViaBuilder(t);
  46. }
  47. // Sometime we want to implement
  48. // FormatValue using util's ToString
  49. // However, if we inside the FormatValue
  50. // we cannot just call the ToString since
  51. // in this scope T is already CFormattable
  52. // and ToString will call the very
  53. // FormatValue we are implementing,
  54. // causing an infinite recursion loop.
  55. // This method is basically a call to
  56. // util's ToString default implementation.
  57. template <class T>
  58. TString ToStringIgnoringFormatValue(const T& t)
  59. {
  60. TString s;
  61. ::TStringOutput o(s);
  62. o << t;
  63. return s;
  64. }
  65. ////////////////////////////////////////////////////////////////////////////////
  66. // Helper functions for formatting.
  67. namespace NDetail {
  68. constexpr inline char IntroductorySymbol = '%';
  69. constexpr inline char GenericSpecSymbol = 'v';
  70. inline bool IsQuotationSpecSymbol(char symbol)
  71. {
  72. return symbol == 'Q' || symbol == 'q';
  73. }
  74. ////////////////////////////////////////////////////////////////////////////////
  75. template <class TValue>
  76. void FormatValueViaSprintf(
  77. TStringBuilderBase* builder,
  78. TValue value,
  79. TStringBuf spec,
  80. TStringBuf genericSpec);
  81. template <class TValue>
  82. void FormatIntValue(
  83. TStringBuilderBase* builder,
  84. TValue value,
  85. TStringBuf spec,
  86. TStringBuf genericSpec);
  87. void FormatPointerValue(
  88. TStringBuilderBase* builder,
  89. const void* value,
  90. TStringBuf spec);
  91. ////////////////////////////////////////////////////////////////////////////////
  92. // Helper concepts for matching the correct overload.
  93. // NB(arkady-e1ppa): We prefer to hardcode the known types
  94. // so that someone doesn't accidentally implement the
  95. // "SimpleRange" concept and have a non-trivial
  96. // formatting procedure at the same time.
  97. // Sadly, clang is bugged and thus we must do implementation by hand
  98. // if we want to use this concept in class specializations.
  99. template <class R>
  100. constexpr bool CKnownRange = false;
  101. template <class T>
  102. requires requires (T* t) { [] <class... Ts> (std::vector<Ts...>*) {} (t); }
  103. constexpr bool CKnownRange<T> = true;
  104. template <class T, size_t E>
  105. constexpr bool CKnownRange<std::span<T, E>> = true;
  106. template <class T, size_t N>
  107. constexpr bool CKnownRange<std::array<T, N>> = true;
  108. template <class T, size_t N>
  109. constexpr bool CKnownRange<TCompactVector<T, N>> = true;
  110. template <class T>
  111. requires requires (T* t) { [] <class... Ts> (std::set<Ts...>*) {} (t); }
  112. constexpr bool CKnownRange<T> = true;
  113. template <class T>
  114. requires requires (T* t) { [] <class... Ts> (std::multiset<Ts...>*) {} (t); }
  115. constexpr bool CKnownRange<T> = true;
  116. template <class... Ts>
  117. constexpr bool CKnownRange<THashSet<Ts...>> = true;
  118. template <class... Ts>
  119. constexpr bool CKnownRange<THashMultiSet<Ts...>> = true;
  120. ////////////////////////////////////////////////////////////////////////////////
  121. template <class R>
  122. constexpr bool CKnownKVRange = false;
  123. template <class T>
  124. requires requires (T* t) { [] <class... Ts> (std::map<Ts...>*) {} (t); }
  125. constexpr bool CKnownKVRange<T> = true;
  126. template <class T>
  127. requires requires (T* t) { [] <class... Ts> (std::multimap<Ts...>*) {} (t); }
  128. constexpr bool CKnownKVRange<T> = true;
  129. template <class... Ts>
  130. constexpr bool CKnownKVRange<THashMap<Ts...>> = true;
  131. template <class... Ts>
  132. constexpr bool CKnownKVRange<THashMultiMap<Ts...>> = true;
  133. // TODO(arkady-e1ppa): Uncomment me when
  134. // https://github.com/llvm/llvm-project/issues/58534 is shipped.
  135. // template <class R>
  136. // concept CKnownRange =
  137. // requires (R r) { [] <class... Ts> (std::vector<Ts...>) { } (r); } ||
  138. // requires (R r) { [] <class T, size_t E> (std::span<T, E>) { } (r); } ||
  139. // requires (R r) { [] <class T, size_t N> (TCompactVector<T, N>) { } (r); } ||
  140. // requires (R r) { [] <class... Ts> (std::set<Ts...>) { } (r); } ||
  141. // requires (R r) { [] <class... Ts> (THashSet<Ts...>) { } (r); } ||
  142. // requires (R r) { [] <class... Ts> (THashMultiSet<Ts...>) { } (r); };
  143. // ////////////////////////////////////////////////////////////////////////////////
  144. // template <class R>
  145. // concept CKnownKVRange =
  146. // requires (R r) { [] <class... Ts> (std::map<Ts...>) { } (r); } ||
  147. // requires (R r) { [] <class... Ts> (std::multimap<Ts...>) { } (r); } ||
  148. // requires (R r) { [] <class... Ts> (THashMap<Ts...>) { } (r); } ||
  149. // requires (R r) { [] <class... Ts> (THashMultiMap<Ts...>) { } (r); };
  150. } // namespace NDetail
  151. ////////////////////////////////////////////////////////////////////////////////
  152. template <class TRange, class TFormatter>
  153. void FormatRange(TStringBuilderBase* builder, const TRange& range, const TFormatter& formatter, size_t limit = std::numeric_limits<size_t>::max())
  154. {
  155. builder->AppendChar('[');
  156. size_t index = 0;
  157. for (const auto& item : range) {
  158. if (index > 0) {
  159. builder->AppendString(DefaultJoinToStringDelimiter);
  160. }
  161. if (index == limit) {
  162. builder->AppendString(DefaultRangeEllipsisFormat);
  163. break;
  164. }
  165. formatter(builder, item);
  166. ++index;
  167. }
  168. builder->AppendChar(']');
  169. }
  170. ////////////////////////////////////////////////////////////////////////////////
  171. template <class TRange, class TFormatter>
  172. void FormatKeyValueRange(TStringBuilderBase* builder, const TRange& range, const TFormatter& formatter, size_t limit = std::numeric_limits<size_t>::max())
  173. {
  174. builder->AppendChar('{');
  175. size_t index = 0;
  176. for (const auto& item : range) {
  177. if (index > 0) {
  178. builder->AppendString(DefaultJoinToStringDelimiter);
  179. }
  180. if (index == limit) {
  181. builder->AppendString(DefaultRangeEllipsisFormat);
  182. break;
  183. }
  184. formatter(builder, item.first);
  185. builder->AppendString(DefaultKeyValueDelimiter);
  186. formatter(builder, item.second);
  187. ++index;
  188. }
  189. builder->AppendChar('}');
  190. }
  191. ////////////////////////////////////////////////////////////////////////////////
  192. template <class TRange, class TValueGetter, class TIntervalFormatter>
  193. void FormatCompactIntervalRange(
  194. TStringBuilderBase* builder,
  195. const TRange& range,
  196. const TValueGetter& valueGetter,
  197. const TIntervalFormatter& intervalFormatter)
  198. {
  199. if (range.begin() == range.end()) {
  200. builder->AppendString("[]");
  201. return;
  202. }
  203. builder->AppendChar('[');
  204. auto first = range.begin();
  205. auto last = first;
  206. auto current = first + 1;
  207. while (current != range.end()) {
  208. if (valueGetter(current) != valueGetter(last) + 1) {
  209. bool firstInterval = first == range.begin();
  210. intervalFormatter(builder, first, last, valueGetter, firstInterval);
  211. first = current;
  212. }
  213. last = current;
  214. ++current;
  215. }
  216. bool firstInterval = first == range.begin();
  217. intervalFormatter(builder, first, last, valueGetter, firstInterval);
  218. builder->AppendChar(']');
  219. }
  220. ////////////////////////////////////////////////////////////////////////////////
  221. template <class R>
  222. concept CFormattableRange =
  223. NDetail::CKnownRange<R> &&
  224. CFormattable<typename R::value_type>;
  225. template <class R>
  226. concept CFormattableKVRange =
  227. NDetail::CKnownKVRange<R> &&
  228. CFormattable<typename R::key_type> &&
  229. CFormattable<typename R::value_type>;
  230. ////////////////////////////////////////////////////////////////////////////////
  231. // Specializations of TFormatArg for ranges
  232. template <class R>
  233. requires CFormattableRange<std::remove_cvref_t<R>>
  234. struct TFormatArg<R>
  235. : public TFormatArgBase
  236. {
  237. using TUnderlying = typename std::remove_cvref_t<R>::value_type;
  238. static constexpr auto ConversionSpecifiers = TFormatArg<TUnderlying>::ConversionSpecifiers;
  239. static constexpr auto FlagSpecifiers = TFormatArg<TUnderlying>::FlagSpecifiers;
  240. };
  241. ////////////////////////////////////////////////////////////////////////////////
  242. template <class TRange, class TFormatter>
  243. typename TFormattableView<TRange, TFormatter>::TBegin TFormattableView<TRange, TFormatter>::begin() const
  244. {
  245. return RangeBegin;
  246. }
  247. template <class TRange, class TFormatter>
  248. typename TFormattableView<TRange, TFormatter>::TEnd TFormattableView<TRange, TFormatter>::end() const
  249. {
  250. return RangeEnd;
  251. }
  252. template <class TRange, class TFormatter>
  253. TFormattableView<TRange, TFormatter> MakeFormattableView(
  254. const TRange& range,
  255. TFormatter&& formatter)
  256. {
  257. return TFormattableView<TRange, std::decay_t<TFormatter>>{range.begin(), range.end(), std::forward<TFormatter>(formatter)};
  258. }
  259. template <class TRange, class TFormatter>
  260. TFormattableView<TRange, TFormatter> MakeShrunkFormattableView(
  261. const TRange& range,
  262. TFormatter&& formatter,
  263. size_t limit)
  264. {
  265. return TFormattableView<TRange, std::decay_t<TFormatter>>{
  266. range.begin(),
  267. range.end(),
  268. std::forward<TFormatter>(formatter),
  269. limit};
  270. }
  271. template <class TFormatter>
  272. TFormatterWrapper<TFormatter> MakeFormatterWrapper(
  273. TFormatter&& formatter)
  274. {
  275. return TFormatterWrapper<TFormatter>{
  276. .Formatter = std::move(formatter)
  277. };
  278. }
  279. template <class TRange, class TValueGetter, class TIntervalFormatter>
  280. auto TCompactIntervalView<TRange, TValueGetter, TIntervalFormatter>::begin() const
  281. -> TBegin
  282. {
  283. return RangeBegin;
  284. }
  285. template <class TRange, class TValueGetter, class TIntervalFormatter>
  286. auto TCompactIntervalView<TRange, TValueGetter, TIntervalFormatter>::end() const
  287. -> TEnd
  288. {
  289. return RangeEnd;
  290. }
  291. template <class TRange>
  292. auto TDefaultValueGetter<TRange>::operator()(const TIterator& iterator) const
  293. -> typename std::iterator_traits<TIterator>::value_type
  294. {
  295. return *iterator;
  296. }
  297. template <class TRange, class TValueGetter>
  298. void TDefaultIntervalFormatter<TRange, TValueGetter>::operator()(
  299. TStringBuilderBase* builder,
  300. const TIterator& first,
  301. const TIterator& last,
  302. const TValueGetter& valueGetter,
  303. bool firstInterval) const
  304. {
  305. if (!firstInterval) {
  306. builder->AppendString(DefaultJoinToStringDelimiter);
  307. }
  308. if (first == last) {
  309. builder->AppendFormat("%v", valueGetter(first));
  310. } else {
  311. builder->AppendFormat("%v-%v", valueGetter(first), valueGetter(last));
  312. }
  313. }
  314. template <class TRange, class TValueGetter, class TIntervalFormatter>
  315. TCompactIntervalView<TRange, TValueGetter, TIntervalFormatter> MakeCompactIntervalView(
  316. const TRange& range,
  317. TValueGetter&& valueGetter,
  318. TIntervalFormatter&& intervalFormatter)
  319. {
  320. return TCompactIntervalView<TRange, TValueGetter, TIntervalFormatter>{
  321. range.begin(),
  322. range.end(),
  323. std::forward<TValueGetter>(valueGetter),
  324. std::forward<TIntervalFormatter>(intervalFormatter)};
  325. }
  326. template <class... TArgs>
  327. TLazyMultiValueFormatter<TArgs...>::TLazyMultiValueFormatter(
  328. TStringBuf format,
  329. TArgs&&... args)
  330. : Format_(format)
  331. , Args_(std::forward<TArgs>(args)...)
  332. { }
  333. template <class... TArgs>
  334. auto MakeLazyMultiValueFormatter(TStringBuf format, TArgs&&... args)
  335. {
  336. return TLazyMultiValueFormatter<TArgs...>(format, std::forward<TArgs>(args)...);
  337. }
  338. ////////////////////////////////////////////////////////////////////////////////
  339. // Non-container objects.
  340. #define XX(valueType, castType, genericSpec) \
  341. inline void FormatValue(TStringBuilderBase* builder, valueType value, TStringBuf spec) \
  342. { \
  343. NYT::NDetail::FormatIntValue(builder, static_cast<castType>(value), spec, genericSpec); \
  344. }
  345. XX(i8, i32, TStringBuf("d"))
  346. XX(ui8, ui32, TStringBuf("u"))
  347. XX(i16, i32, TStringBuf("d"))
  348. XX(ui16, ui32, TStringBuf("u"))
  349. XX(i32, i32, TStringBuf("d"))
  350. XX(ui32, ui32, TStringBuf("u"))
  351. XX(long, i64, TStringBuf(PRIdLEAST64))
  352. XX(long long, i64, TStringBuf(PRIdLEAST64))
  353. XX(unsigned long, ui64, TStringBuf(PRIuLEAST64))
  354. XX(unsigned long long, ui64, TStringBuf(PRIuLEAST64))
  355. #undef XX
  356. #define XX(valueType, castType, genericSpec) \
  357. inline void FormatValue(TStringBuilderBase* builder, valueType value, TStringBuf spec) \
  358. { \
  359. NYT::NDetail::FormatValueViaSprintf(builder, static_cast<castType>(value), spec, genericSpec); \
  360. }
  361. XX(double, double, TStringBuf("lf"))
  362. XX(float, float, TStringBuf("f"))
  363. #undef XX
  364. // Pointer
  365. template <class T>
  366. void FormatValue(TStringBuilderBase* builder, T* value, TStringBuf spec)
  367. {
  368. NYT::NDetail::FormatPointerValue(builder, static_cast<const void*>(value), spec);
  369. }
  370. // TStringBuf
  371. inline void FormatValue(TStringBuilderBase* builder, TStringBuf value, TStringBuf spec)
  372. {
  373. if (!spec) {
  374. builder->AppendString(value);
  375. return;
  376. }
  377. // Parse alignment.
  378. bool alignLeft = false;
  379. const char* current = spec.begin();
  380. if (*current == '-') {
  381. alignLeft = true;
  382. ++current;
  383. }
  384. bool hasAlign = false;
  385. int alignSize = 0;
  386. while (*current >= '0' && *current <= '9') {
  387. hasAlign = true;
  388. alignSize = 10 * alignSize + (*current - '0');
  389. if (alignSize > 1000000) {
  390. builder->AppendString(TStringBuf("<alignment overflow>"));
  391. return;
  392. }
  393. ++current;
  394. }
  395. int padding = 0;
  396. bool padLeft = false;
  397. bool padRight = false;
  398. if (hasAlign) {
  399. padding = alignSize - value.size();
  400. if (padding < 0) {
  401. padding = 0;
  402. }
  403. padLeft = !alignLeft;
  404. padRight = alignLeft;
  405. }
  406. bool singleQuotes = false;
  407. bool doubleQuotes = false;
  408. bool escape = false;
  409. while (current < spec.end()) {
  410. switch (*current++) {
  411. case 'q':
  412. singleQuotes = true;
  413. break;
  414. case 'Q':
  415. doubleQuotes = true;
  416. break;
  417. case 'h':
  418. escape = true;
  419. break;
  420. }
  421. }
  422. if (padLeft) {
  423. builder->AppendChar(' ', padding);
  424. }
  425. if (singleQuotes || doubleQuotes || escape) {
  426. for (const char* valueCurrent = value.begin(); valueCurrent < value.end(); ++valueCurrent) {
  427. char ch = *valueCurrent;
  428. if (ch == '\n') {
  429. builder->AppendString("\\n");
  430. } else if (ch == '\t') {
  431. builder->AppendString("\\t");
  432. } else if (ch == '\\') {
  433. builder->AppendString("\\\\");
  434. } else if (ch < PrintableASCIILow || ch > PrintableASCIIHigh) {
  435. builder->AppendString("\\x");
  436. builder->AppendChar(IntToHexLowercase[static_cast<ui8>(ch) >> 4]);
  437. builder->AppendChar(IntToHexLowercase[static_cast<ui8>(ch) & 0xf]);
  438. } else if ((singleQuotes && ch == '\'') || (doubleQuotes && ch == '\"')) {
  439. builder->AppendChar('\\');
  440. builder->AppendChar(ch);
  441. } else {
  442. builder->AppendChar(ch);
  443. }
  444. }
  445. } else {
  446. builder->AppendString(value);
  447. }
  448. if (padRight) {
  449. builder->AppendChar(' ', padding);
  450. }
  451. }
  452. // TString
  453. inline void FormatValue(TStringBuilderBase* builder, const TString& value, TStringBuf spec)
  454. {
  455. FormatValue(builder, TStringBuf(value), spec);
  456. }
  457. // const char*
  458. inline void FormatValue(TStringBuilderBase* builder, const char* value, TStringBuf spec)
  459. {
  460. FormatValue(builder, TStringBuf(value), spec);
  461. }
  462. template <size_t N>
  463. inline void FormatValue(TStringBuilderBase* builder, const char (&value)[N], TStringBuf spec)
  464. {
  465. FormatValue(builder, TStringBuf(value), spec);
  466. }
  467. // char*
  468. inline void FormatValue(TStringBuilderBase* builder, char* value, TStringBuf spec)
  469. {
  470. FormatValue(builder, TStringBuf(value), spec);
  471. }
  472. // std::string
  473. inline void FormatValue(TStringBuilderBase* builder, const std::string& value, TStringBuf spec)
  474. {
  475. FormatValue(builder, TStringBuf(value), spec);
  476. }
  477. // std::string_view
  478. inline void FormatValue(TStringBuilderBase* builder, const std::string_view& value, TStringBuf spec)
  479. {
  480. FormatValue(builder, TStringBuf(value), spec);
  481. }
  482. #if __cplusplus >= 202302L
  483. // std::filesystem::path
  484. inline void FormatValue(TStringBuilderBase* builder, const std::filesystem::path& value, TStringBuf spec)
  485. {
  486. FormatValue(builder, std::string(value), spec);
  487. }
  488. #endif
  489. #ifdef __cpp_lib_source_location
  490. // std::source_location
  491. inline void FormatValue(TStringBuilderBase* builder, const std::source_location& location, TStringBuf /*spec*/)
  492. {
  493. if (location.file_name() != nullptr) {
  494. builder->AppendFormat(
  495. "%v:%v:%v",
  496. location.file_name(),
  497. location.line(),
  498. location.column());
  499. } else {
  500. builder->AppendString("<unknown>");
  501. }
  502. }
  503. #endif // __cpp_lib_source_location
  504. // TSourceLocation
  505. inline void FormatValue(TStringBuilderBase* builder, const TSourceLocation& location, TStringBuf /*spec*/)
  506. {
  507. if (location.GetFileName() != nullptr) {
  508. builder->AppendFormat(
  509. "%v:%v",
  510. location.GetFileName(),
  511. location.GetLine());
  512. } else {
  513. builder->AppendString("<unknown>");
  514. }
  515. }
  516. // std::monostate
  517. inline void FormatValue(TStringBuilderBase* builder, const std::monostate&, TStringBuf /*spec*/)
  518. {
  519. builder->AppendString(TStringBuf("<monostate>"));
  520. }
  521. // std::variant
  522. template <class... Ts>
  523. requires (CFormattable<Ts> && ...)
  524. void FormatValue(TStringBuilderBase* builder, const std::variant<Ts...>& variant, TStringBuf spec)
  525. {
  526. [&] <size_t... Ids> (std::index_sequence<Ids...>) {
  527. ([&] {
  528. if (variant.index() == Ids) {
  529. FormatValue(builder, std::get<Ids>(variant), spec);
  530. return false;
  531. }
  532. return true;
  533. } () && ...);
  534. } (std::index_sequence_for<Ts...>());
  535. }
  536. // char
  537. inline void FormatValue(TStringBuilderBase* builder, char value, TStringBuf spec)
  538. {
  539. FormatValue(builder, TStringBuf(&value, 1), spec);
  540. }
  541. // bool
  542. inline void FormatValue(TStringBuilderBase* builder, bool value, TStringBuf spec)
  543. {
  544. // Parse custom flags.
  545. bool lowercase = false;
  546. const char* current = spec.begin();
  547. while (current != spec.end()) {
  548. if (*current == 'l') {
  549. ++current;
  550. lowercase = true;
  551. } else if (NYT::NDetail::IsQuotationSpecSymbol(*current)) {
  552. ++current;
  553. } else
  554. break;
  555. }
  556. auto str = lowercase
  557. ? (value ? TStringBuf("true") : TStringBuf("false"))
  558. : (value ? TStringBuf("True") : TStringBuf("False"));
  559. builder->AppendString(str);
  560. }
  561. // TDuration
  562. inline void FormatValue(TStringBuilderBase* builder, TDuration value, TStringBuf /*spec*/)
  563. {
  564. builder->AppendFormat("%vus", value.MicroSeconds());
  565. }
  566. // TInstant
  567. inline void FormatValue(TStringBuilderBase* builder, TInstant value, TStringBuf spec)
  568. {
  569. // TODO(babenko): Optimize.
  570. FormatValue(builder, NYT::ToStringIgnoringFormatValue(value), spec);
  571. }
  572. // Enum
  573. template <class TEnum>
  574. requires (TEnumTraits<TEnum>::IsEnum)
  575. void FormatValue(TStringBuilderBase* builder, TEnum value, TStringBuf spec)
  576. {
  577. // Parse custom flags.
  578. bool lowercase = false;
  579. const char* current = spec.begin();
  580. while (current != spec.end()) {
  581. if (*current == 'l') {
  582. ++current;
  583. lowercase = true;
  584. } else if (NYT::NDetail::IsQuotationSpecSymbol(*current)) {
  585. ++current;
  586. } else {
  587. break;
  588. }
  589. }
  590. FormatEnum(builder, value, lowercase);
  591. }
  592. template <class TArcadiaEnum>
  593. requires (std::is_enum_v<TArcadiaEnum> && !TEnumTraits<TArcadiaEnum>::IsEnum)
  594. void FormatValue(TStringBuilderBase* builder, TArcadiaEnum value, TStringBuf /*spec*/)
  595. {
  596. // NB(arkady-e1ppa): This can catch normal enums which
  597. // just want to be serialized as numbers.
  598. // Unfortunately, we have no way of determining that other than
  599. // marking every relevant arcadia enum in the code by trait
  600. // or writing their complete trait and placing such trait in
  601. // every single file where it is formatted.
  602. // We gotta figure something out but until that
  603. // we will just have to make a string for such enums.
  604. // If only arcadia enums provided compile-time check
  605. // if enum is serializable :(((((.
  606. builder->AppendString(NYT::ToStringIgnoringFormatValue(value));
  607. }
  608. // Container objects.
  609. // NB(arkady-e1ppa): In order to support container combinations
  610. // we forward-declare them before defining.
  611. // TMaybe
  612. template <class T, class TPolicy>
  613. void FormatValue(TStringBuilderBase* builder, const TMaybe<T, TPolicy>& value, TStringBuf spec);
  614. // std::optional
  615. template <CFormattable T>
  616. void FormatValue(TStringBuilderBase* builder, const std::optional<T>& value, TStringBuf spec);
  617. // std::pair
  618. template <CFormattable A, CFormattable B>
  619. void FormatValue(TStringBuilderBase* builder, const std::pair<A, B>& value, TStringBuf spec);
  620. // std::tuple
  621. template <CFormattable... Ts>
  622. void FormatValue(TStringBuilderBase* builder, const std::tuple<Ts...>& value, TStringBuf spec);
  623. // TEnumIndexedArray
  624. template <class E, CFormattable T>
  625. void FormatValue(TStringBuilderBase* builder, const TEnumIndexedArray<E, T>& collection, TStringBuf spec);
  626. // One-valued ranges
  627. template <CFormattableRange TRange>
  628. void FormatValue(TStringBuilderBase* builder, const TRange& collection, TStringBuf spec);
  629. // Two-valued ranges
  630. template <CFormattableKVRange TRange>
  631. void FormatValue(TStringBuilderBase* builder, const TRange& collection, TStringBuf spec);
  632. // FormattableView
  633. template <class TRange, class TFormatter>
  634. void FormatValue(
  635. TStringBuilderBase* builder,
  636. const TFormattableView<TRange, TFormatter>& formattableView,
  637. TStringBuf spec);
  638. // TFormatterWrapper
  639. template <class TFormatter>
  640. void FormatValue(
  641. TStringBuilderBase* builder,
  642. const TFormatterWrapper<TFormatter>& wrapper,
  643. TStringBuf spec);
  644. // TCompactIntervalView
  645. template <class TRange, class TValueGetter, class TIntervalFormatter>
  646. concept CCompactIntervalView = requires (
  647. TRange range,
  648. TValueGetter valueGetter,
  649. TIntervalFormatter intervalFormatter)
  650. {
  651. { valueGetter(range.begin()) } -> std::integral;
  652. {
  653. intervalFormatter(nullptr, range.begin(), range.begin(), valueGetter, true)
  654. } -> std::same_as<void>;
  655. };
  656. template <class TRange, class TValueGetter, class TIntervalFormatter>
  657. requires CCompactIntervalView<TRange, TValueGetter, TIntervalFormatter>
  658. void FormatValue(
  659. TStringBuilderBase* builder,
  660. const TCompactIntervalView<TRange, TValueGetter, TIntervalFormatter>& compactIntervalView,
  661. TStringBuf spec);
  662. // TLazyMultiValueFormatter
  663. template <class... TArgs>
  664. void FormatValue(
  665. TStringBuilderBase* builder,
  666. const TLazyMultiValueFormatter<TArgs...>& value,
  667. TStringBuf /*spec*/);
  668. // TMaybe
  669. template <class T, class TPolicy>
  670. void FormatValue(TStringBuilderBase* builder, const TMaybe<T, TPolicy>& value, TStringBuf spec)
  671. {
  672. FormatValue(builder, NYT::ToStringIgnoringFormatValue(value), spec);
  673. }
  674. // std::optional: nullopt
  675. inline void FormatValue(TStringBuilderBase* builder, std::nullopt_t, TStringBuf /*spec*/)
  676. {
  677. builder->AppendString(TStringBuf("<null>"));
  678. }
  679. // std::optional: generic T
  680. template <CFormattable T>
  681. void FormatValue(TStringBuilderBase* builder, const std::optional<T>& value, TStringBuf spec)
  682. {
  683. if (value.has_value()) {
  684. FormatValue(builder, *value, spec);
  685. } else {
  686. FormatValue(builder, std::nullopt, spec);
  687. }
  688. }
  689. // std::pair
  690. template <CFormattable A, CFormattable B>
  691. void FormatValue(TStringBuilderBase* builder, const std::pair<A, B>& value, TStringBuf spec)
  692. {
  693. builder->AppendChar('{');
  694. FormatValue(builder, value.first, spec);
  695. builder->AppendString(TStringBuf(", "));
  696. FormatValue(builder, value.second, spec);
  697. builder->AppendChar('}');
  698. }
  699. // std::tuple
  700. template <CFormattable... Ts>
  701. void FormatValue(TStringBuilderBase* builder, const std::tuple<Ts...>& value, TStringBuf spec)
  702. {
  703. builder->AppendChar('{');
  704. [&] <size_t... Idx> (std::index_sequence<Idx...>) {
  705. ([&] {
  706. FormatValue(builder, std::get<Idx>(value), spec);
  707. if constexpr (Idx != sizeof...(Ts) - 1) {
  708. builder->AppendString(TStringBuf(", "));
  709. }
  710. } (), ...);
  711. } (std::index_sequence_for<Ts...>());
  712. builder->AppendChar('}');
  713. }
  714. // TEnumIndexedArray
  715. template <class E, CFormattable T>
  716. void FormatValue(TStringBuilderBase* builder, const TEnumIndexedArray<E, T>& collection, TStringBuf spec)
  717. {
  718. builder->AppendChar('{');
  719. bool firstItem = true;
  720. for (const auto& index : TEnumTraits<E>::GetDomainValues()) {
  721. if (!firstItem) {
  722. builder->AppendString(DefaultJoinToStringDelimiter);
  723. }
  724. FormatValue(builder, index, spec);
  725. builder->AppendString(": ");
  726. FormatValue(builder, collection[index], spec);
  727. firstItem = false;
  728. }
  729. builder->AppendChar('}');
  730. }
  731. // One-valued ranges
  732. template <CFormattableRange TRange>
  733. void FormatValue(TStringBuilderBase* builder, const TRange& collection, TStringBuf spec)
  734. {
  735. NYT::FormatRange(builder, collection, TSpecBoundFormatter(spec));
  736. }
  737. // Two-valued ranges
  738. template <CFormattableKVRange TRange>
  739. void FormatValue(TStringBuilderBase* builder, const TRange& collection, TStringBuf /*spec*/)
  740. {
  741. NYT::FormatKeyValueRange(builder, collection, TDefaultFormatter());
  742. }
  743. // FormattableView
  744. template <class TRange, class TFormatter>
  745. void FormatValue(
  746. TStringBuilderBase* builder,
  747. const TFormattableView<TRange, TFormatter>& formattableView,
  748. TStringBuf /*spec*/)
  749. {
  750. NYT::FormatRange(builder, formattableView, formattableView.Formatter, formattableView.Limit);
  751. }
  752. // TCompactIntervalView
  753. template <class TRange, class TValueGetter, class TIntervalFormatter>
  754. requires CCompactIntervalView<TRange, TValueGetter, TIntervalFormatter>
  755. void FormatValue(
  756. TStringBuilderBase* builder,
  757. const TCompactIntervalView<TRange, TValueGetter, TIntervalFormatter>& compactIntervalView,
  758. TStringBuf /*spec*/)
  759. {
  760. NYT::FormatCompactIntervalRange(
  761. builder,
  762. compactIntervalView,
  763. compactIntervalView.ValueGetter,
  764. compactIntervalView.IntervalFormatter);
  765. }
  766. // TFormatterWrapper
  767. template <class TFormatter>
  768. void FormatValue(
  769. TStringBuilderBase* builder,
  770. const TFormatterWrapper<TFormatter>& wrapper,
  771. TStringBuf /*spec*/)
  772. {
  773. wrapper.Formatter(builder);
  774. }
  775. // TLazyMultiValueFormatter
  776. template <class... TArgs>
  777. void FormatValue(
  778. TStringBuilderBase* builder,
  779. const TLazyMultiValueFormatter<TArgs...>& value,
  780. TStringBuf /*spec*/)
  781. {
  782. std::apply(
  783. [&] <class... TInnerArgs> (TInnerArgs&&... args) {
  784. builder->AppendFormat(value.Format_, std::forward<TInnerArgs>(args)...);
  785. },
  786. value.Args_);
  787. }
  788. ////////////////////////////////////////////////////////////////////////////////
  789. namespace NDetail {
  790. template <size_t HeadPos, class... TArgs>
  791. class TValueFormatter;
  792. template <size_t HeadPos>
  793. class TValueFormatter<HeadPos>
  794. {
  795. public:
  796. void operator() (size_t /*index*/, TStringBuilderBase* builder, TStringBuf /*spec*/) const
  797. {
  798. builder->AppendString(TStringBuf("<missing argument>"));
  799. }
  800. };
  801. template <size_t HeadPos, class THead, class... TTail>
  802. class TValueFormatter<HeadPos, THead, TTail...>
  803. {
  804. public:
  805. explicit TValueFormatter(const THead& head, const TTail&... tail) noexcept
  806. : Head_(head)
  807. , TailFormatter_(tail...)
  808. { }
  809. void operator() (size_t index, TStringBuilderBase* builder, TStringBuf spec) const
  810. {
  811. YT_ASSERT(index >= HeadPos);
  812. if (index == HeadPos) {
  813. FormatValue(builder, Head_, spec);
  814. } else {
  815. TailFormatter_(index, builder, spec);
  816. }
  817. }
  818. private:
  819. const THead& Head_;
  820. TValueFormatter<HeadPos + 1, TTail...> TailFormatter_;
  821. };
  822. ////////////////////////////////////////////////////////////////////////////////
  823. template <class TRangeValue>
  824. class TRangeFormatter
  825. {
  826. public:
  827. template <class... TArgs>
  828. requires std::constructible_from<std::span<const TRangeValue>, TArgs...>
  829. explicit TRangeFormatter(TArgs&&... args) noexcept
  830. : Span_(std::forward<TArgs>(args)...)
  831. { }
  832. void operator() (size_t index, TStringBuilderBase* builder, TStringBuf spec) const
  833. {
  834. if (index >= Span_.size()) {
  835. builder->AppendString(TStringBuf("<missing argument>"));
  836. } else {
  837. FormatValue(builder, *(Span_.begin() + index), spec);
  838. }
  839. }
  840. private:
  841. std::span<const TRangeValue> Span_;
  842. };
  843. ////////////////////////////////////////////////////////////////////////////////
  844. template <class T>
  845. concept CFormatter = CInvocable<T, void(size_t, TStringBuilderBase*, TStringBuf)>;
  846. ////////////////////////////////////////////////////////////////////////////////
  847. template <CFormatter TFormatter>
  848. void RunFormatterAt(
  849. const TFormatter& formatter,
  850. size_t index,
  851. TStringBuilderBase* builder,
  852. TStringBuf spec,
  853. bool singleQuotes,
  854. bool doubleQuotes)
  855. {
  856. // 'n' means 'nothing'; skip the argument.
  857. if (!spec.Contains('n')) {
  858. if (singleQuotes) {
  859. builder->AppendChar('\'');
  860. }
  861. if (doubleQuotes) {
  862. builder->AppendChar('"');
  863. }
  864. formatter(index, builder, spec);
  865. if (singleQuotes) {
  866. builder->AppendChar('\'');
  867. }
  868. if (doubleQuotes) {
  869. builder->AppendChar('"');
  870. }
  871. }
  872. }
  873. ////////////////////////////////////////////////////////////////////////////////
  874. template <CFormatter TFormatter>
  875. void RunFormatterFullScan(
  876. TStringBuilderBase* builder,
  877. TStringBuf format,
  878. const TFormatter& formatter,
  879. int argIndex = 0)
  880. {
  881. auto current = std::begin(format);
  882. auto end = std::end(format);
  883. while (true) {
  884. // Scan verbatim part until stop symbol.
  885. auto verbatimBegin = current;
  886. auto verbatimEnd = std::find(current, end, IntroductorySymbol);
  887. // Copy verbatim part, if any.
  888. size_t verbatimSize = verbatimEnd - verbatimBegin;
  889. if (verbatimSize > 0) {
  890. builder->AppendString(TStringBuf(verbatimBegin, verbatimSize));
  891. }
  892. // Handle stop symbol.
  893. current = verbatimEnd;
  894. if (current == end) {
  895. break;
  896. }
  897. YT_ASSERT(*current == IntroductorySymbol);
  898. ++current;
  899. if (*current == IntroductorySymbol) {
  900. // Verbatim %.
  901. builder->AppendChar(IntroductorySymbol);
  902. ++current;
  903. continue;
  904. }
  905. // Scan format part until stop symbol.
  906. auto argFormatBegin = current;
  907. auto argFormatEnd = argFormatBegin;
  908. bool singleQuotes = false;
  909. bool doubleQuotes = false;
  910. static constexpr TStringBuf conversionSpecifiers =
  911. "diuoxXfFeEgGaAcspn";
  912. while (
  913. argFormatEnd != end &&
  914. *argFormatEnd != GenericSpecSymbol && // value in generic format
  915. !conversionSpecifiers.Contains(*argFormatEnd)) // others are standard specifiers supported by printf
  916. {
  917. switch (*argFormatEnd) {
  918. case 'q':
  919. singleQuotes = true;
  920. break;
  921. case 'Q':
  922. doubleQuotes = true;
  923. break;
  924. case 'h':
  925. break;
  926. }
  927. ++argFormatEnd;
  928. }
  929. // Handle end of format string.
  930. if (argFormatEnd != end) {
  931. ++argFormatEnd;
  932. }
  933. RunFormatterAt(
  934. formatter,
  935. argIndex++,
  936. builder,
  937. TStringBuf{argFormatBegin, argFormatEnd},
  938. singleQuotes,
  939. doubleQuotes);
  940. current = argFormatEnd;
  941. }
  942. }
  943. ////////////////////////////////////////////////////////////////////////////////
  944. template <CFormatter TFormatter, class... TArgs>
  945. void RunFormatter(
  946. TStringBuilderBase* builder,
  947. TBasicFormatString<TArgs...> formatString,
  948. const TFormatter& formatter)
  949. {
  950. auto isValidLocations = [] (const auto& t) {
  951. return std::get<0>(t) != std::get<1>(t);
  952. };
  953. // Generally marker is simply "%v" e.g. 2 symbols.
  954. // We assume it is used to insert something for roughly 5 symbols
  955. // of size.
  956. builder->Preallocate(std::size(formatString.Get()) + sizeof...(TArgs) * (5 - 2));
  957. // Empty marker positions -- fallback to the normal impl.
  958. if constexpr (sizeof...(TArgs) != 0) {
  959. if (!isValidLocations(formatString.Markers[0])) {
  960. RunFormatterFullScan(builder, formatString.Get(), formatter);
  961. return;
  962. }
  963. } else {
  964. if (formatString.Escapes[0] == -2) {
  965. RunFormatterFullScan(builder, formatString.Get(), formatter);
  966. return;
  967. }
  968. }
  969. int escapesFound = 0;
  970. int currentPos = 0;
  971. auto beginIt = formatString.Get().begin();
  972. auto size = formatString.Get().size();
  973. const auto& [markers, escapes] = std::tie(formatString.Markers, formatString.Escapes);
  974. auto appendVerbatim = [&] (int offsetToEnd) {
  975. builder->AppendString(TStringBuf{beginIt + currentPos, beginIt + offsetToEnd});
  976. };
  977. auto processEscape = [&] () mutable {
  978. // OpenMP doesn't support structured bindings :(.
  979. auto escapePos = formatString.Escapes[escapesFound];
  980. // Append everything that's present until %%.
  981. appendVerbatim(escapePos);
  982. // Append '%'.
  983. builder->AppendChar('%');
  984. // Advance position to first '%' pos + 2.
  985. currentPos = escapePos + 2;
  986. };
  987. int argIndex = 0;
  988. while(argIndex < std::ssize(markers)) {
  989. auto [markerStart, markerEnd] = markers[argIndex];
  990. if (
  991. escapes[escapesFound] != -1 &&
  992. escapes[escapesFound] - currentPos < markerStart - currentPos)
  993. {
  994. // Escape sequence is closer.
  995. processEscape();
  996. ++escapesFound;
  997. } else {
  998. // Normal marker is closer.
  999. // Append everything that's present until marker start.
  1000. appendVerbatim(markerStart);
  1001. // Parsing format string.
  1002. // We skip '%' here since spec does not contain it.
  1003. auto spec = TStringBuf{beginIt + markerStart + 1, beginIt + markerEnd};
  1004. bool singleQuotes = false;
  1005. bool doubleQuotes = false;
  1006. for (auto c : spec) {
  1007. if (c == 'q') {
  1008. singleQuotes = true;
  1009. }
  1010. if (c == 'Q') {
  1011. doubleQuotes = true;
  1012. }
  1013. }
  1014. RunFormatterAt(
  1015. formatter,
  1016. argIndex,
  1017. builder,
  1018. spec,
  1019. singleQuotes,
  1020. doubleQuotes);
  1021. // Advance position past marker's end.
  1022. currentPos = markerEnd;
  1023. ++argIndex;
  1024. continue;
  1025. }
  1026. // Check if the number of escapes we have found has exceeded the recorded limit
  1027. // e.g. we have to manually scan the rest of the formatString.
  1028. if (escapesFound == std::ssize(escapes)) {
  1029. break;
  1030. }
  1031. }
  1032. // Process remaining escapes.
  1033. while (escapesFound < std::ssize(escapes)) {
  1034. if (escapes[escapesFound] == -1) {
  1035. break;
  1036. }
  1037. processEscape();
  1038. ++escapesFound;
  1039. }
  1040. // We either ran out of markers or reached the limit of allowed
  1041. // escape sequences.
  1042. // Happy path: no limit on escape sequences.
  1043. if (escapesFound != std::ssize(escapes)) {
  1044. // Append whatever's left until the end.
  1045. appendVerbatim(size);
  1046. return;
  1047. }
  1048. // Sad path: we have to fully parse remainder of format.
  1049. RunFormatterFullScan(builder, TStringBuf{beginIt + currentPos, beginIt + size}, formatter, argIndex);
  1050. }
  1051. ////////////////////////////////////////////////////////////////////////////////
  1052. // For benchmarking purposes.
  1053. template <class... TArgs>
  1054. TString FormatOld(TFormatString<TArgs...> format, TArgs&&... args)
  1055. {
  1056. TStringBuilder builder;
  1057. if constexpr ((CFormattable<TArgs> && ...)) {
  1058. NYT::NDetail::TValueFormatter<0, TArgs...> formatter(args...);
  1059. NYT::NDetail::RunFormatterFullScan(&builder, format.Get(), formatter);
  1060. }
  1061. return builder.Flush();
  1062. }
  1063. } // namespace NDetail
  1064. ////////////////////////////////////////////////////////////////////////////////
  1065. template <class... TArgs>
  1066. void Format(TStringBuilderBase* builder, TFormatString<TArgs...> format, TArgs&&... args)
  1067. {
  1068. // NB(arkady-e1ppa): "if constexpr" is done in order to prevent
  1069. // compiler from emitting "No matching function to call"
  1070. // when arguments are not formattable.
  1071. // Compiler would crash in TFormatString ctor
  1072. // anyway (e.g. program would not compile) but
  1073. // for some reason it does look ahead and emits
  1074. // a second error.
  1075. if constexpr ((CFormattable<TArgs> && ...)) {
  1076. NYT::NDetail::TValueFormatter<0, TArgs...> formatter(args...);
  1077. NYT::NDetail::RunFormatter(builder, format, formatter);
  1078. }
  1079. }
  1080. template <class... TArgs>
  1081. TString Format(TFormatString<TArgs...> format, TArgs&&... args)
  1082. {
  1083. TStringBuilder builder;
  1084. Format(&builder, format, std::forward<TArgs>(args)...);
  1085. return builder.Flush();
  1086. }
  1087. ////////////////////////////////////////////////////////////////////////////////
  1088. template <size_t Length, class TVector>
  1089. void FormatVector(
  1090. TStringBuilderBase* builder,
  1091. const char (&format)[Length],
  1092. const TVector& vec)
  1093. {
  1094. NYT::NDetail::TRangeFormatter<typename TVector::value_type> formatter(vec);
  1095. NYT::NDetail::RunFormatterFullScan(builder, format, formatter);
  1096. }
  1097. template <class TVector>
  1098. void FormatVector(
  1099. TStringBuilderBase* builder,
  1100. TStringBuf format,
  1101. const TVector& vec)
  1102. {
  1103. NYT::NDetail::TRangeFormatter<typename TVector::value_type> formatter(vec);
  1104. NYT::NDetail::RunFormatterFullScan(builder, format, formatter);
  1105. }
  1106. template <size_t Length, class TVector>
  1107. TString FormatVector(
  1108. const char (&format)[Length],
  1109. const TVector& vec)
  1110. {
  1111. TStringBuilder builder;
  1112. FormatVector(&builder, format, vec);
  1113. return builder.Flush();
  1114. }
  1115. template <class TVector>
  1116. TString FormatVector(
  1117. TStringBuf format,
  1118. const TVector& vec)
  1119. {
  1120. TStringBuilder builder;
  1121. FormatVector(&builder, format, vec);
  1122. return builder.Flush();
  1123. }
  1124. ////////////////////////////////////////////////////////////////////////////////
  1125. } // namespace NYT
  1126. #include <util/string/cast.h>
  1127. // util/string/cast.h extension for yt and std types only
  1128. // TODO(arkady-e1ppa): Abolish ::ToString in
  1129. // favour of either NYT::ToString or
  1130. // automatic formatting wherever it is needed.
  1131. namespace NPrivate {
  1132. ////////////////////////////////////////////////////////////////////////////////
  1133. template <class T>
  1134. requires (
  1135. (NYT::NDetail::IsNYTName<T>() ||
  1136. NYT::NDetail::IsStdName<T>()) &&
  1137. NYT::CFormattable<T>)
  1138. struct TToString<T, false>
  1139. {
  1140. static TString Cvt(const T& t)
  1141. {
  1142. return NYT::ToStringViaBuilder(t);
  1143. }
  1144. };
  1145. ////////////////////////////////////////////////////////////////////////////////
  1146. } // namespace NPrivate