format-inl.h 40 KB

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