format-inl.h 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066
  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/small_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/wrapper_traits.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. namespace NYT {
  24. ////////////////////////////////////////////////////////////////////////////////
  25. inline char* TStringBuilderBase::Preallocate(size_t size)
  26. {
  27. Reserve(size + GetLength());
  28. return Current_;
  29. }
  30. inline void TStringBuilderBase::Reserve(size_t size)
  31. {
  32. if (Y_UNLIKELY(End_ - Begin_ < static_cast<ssize_t>(size))) {
  33. size_t length = GetLength();
  34. auto newLength = std::max(size, MinBufferLength);
  35. DoReserve(newLength);
  36. Current_ = Begin_ + length;
  37. }
  38. }
  39. inline size_t TStringBuilderBase::GetLength() const
  40. {
  41. return Current_ ? Current_ - Begin_ : 0;
  42. }
  43. inline TStringBuf TStringBuilderBase::GetBuffer() const
  44. {
  45. return TStringBuf(Begin_, Current_);
  46. }
  47. inline void TStringBuilderBase::Advance(size_t size)
  48. {
  49. Current_ += size;
  50. YT_ASSERT(Current_ <= End_);
  51. }
  52. inline void TStringBuilderBase::AppendChar(char ch)
  53. {
  54. *Preallocate(1) = ch;
  55. Advance(1);
  56. }
  57. inline void TStringBuilderBase::AppendChar(char ch, int n)
  58. {
  59. YT_ASSERT(n >= 0);
  60. if (Y_LIKELY(0 != n)) {
  61. char* dst = Preallocate(n);
  62. ::memset(dst, ch, n);
  63. Advance(n);
  64. }
  65. }
  66. inline void TStringBuilderBase::AppendString(TStringBuf str)
  67. {
  68. if (Y_LIKELY(str)) {
  69. char* dst = Preallocate(str.length());
  70. ::memcpy(dst, str.begin(), str.length());
  71. Advance(str.length());
  72. }
  73. }
  74. inline void TStringBuilderBase::AppendString(const char* str)
  75. {
  76. AppendString(TStringBuf(str));
  77. }
  78. inline void TStringBuilderBase::Reset()
  79. {
  80. Begin_ = Current_ = End_ = nullptr;
  81. DoReset();
  82. }
  83. template <class... TArgs>
  84. void TStringBuilderBase::AppendFormat(TStringBuf format, TArgs&& ... args)
  85. {
  86. Format(this, TRuntimeFormat{format}, std::forward<TArgs>(args)...);
  87. }
  88. template <size_t Length, class... TArgs>
  89. void TStringBuilderBase::AppendFormat(const char (&format)[Length], TArgs&& ... args)
  90. {
  91. Format(this, TRuntimeFormat{format}, std::forward<TArgs>(args)...);
  92. }
  93. ////////////////////////////////////////////////////////////////////////////////
  94. inline TString TStringBuilder::Flush()
  95. {
  96. Buffer_.resize(GetLength());
  97. auto result = std::move(Buffer_);
  98. Reset();
  99. return result;
  100. }
  101. inline void TStringBuilder::DoReset()
  102. {
  103. Buffer_ = {};
  104. }
  105. inline void TStringBuilder::DoReserve(size_t newLength)
  106. {
  107. Buffer_.ReserveAndResize(newLength);
  108. auto capacity = Buffer_.capacity();
  109. Buffer_.ReserveAndResize(capacity);
  110. Begin_ = &*Buffer_.begin();
  111. End_ = Begin_ + capacity;
  112. }
  113. inline void FormatValue(TStringBuilderBase* builder, const TStringBuilder& value, TStringBuf /*spec*/)
  114. {
  115. builder->AppendString(value.GetBuffer());
  116. }
  117. ////////////////////////////////////////////////////////////////////////////////
  118. template <class T>
  119. TString ToStringViaBuilder(const T& value, TStringBuf spec)
  120. {
  121. TStringBuilder builder;
  122. FormatValue(&builder, value, spec);
  123. return builder.Flush();
  124. }
  125. ////////////////////////////////////////////////////////////////////////////////
  126. // Compatibility for users of NYT::ToString(nyt_type).
  127. template <CFormattable T>
  128. TString ToString(const T& t)
  129. {
  130. return ToStringViaBuilder(t);
  131. }
  132. // Sometime we want to implement
  133. // FormatValue using util's ToString
  134. // However, if we inside the FormatValue
  135. // we cannot just call the ToString since
  136. // in this scope T is already CFormattable
  137. // and ToString will call the very
  138. // FormatValue we are implementing,
  139. // causing an infinite recursion loop.
  140. // This method is basically a call to
  141. // util's ToString default implementation.
  142. template <class T>
  143. TString ToStringIgnoringFormatValue(const T& t)
  144. {
  145. TString s;
  146. ::TStringOutput o(s);
  147. o << t;
  148. return s;
  149. }
  150. ////////////////////////////////////////////////////////////////////////////////
  151. // Helper functions for formatting.
  152. namespace NDetail {
  153. constexpr inline char IntroductorySymbol = '%';
  154. constexpr inline char GenericSpecSymbol = 'v';
  155. inline bool IsQuotationSpecSymbol(char symbol)
  156. {
  157. return symbol == 'Q' || symbol == 'q';
  158. }
  159. ////////////////////////////////////////////////////////////////////////////////
  160. template <class TValue>
  161. void FormatValueViaSprintf(
  162. TStringBuilderBase* builder,
  163. TValue value,
  164. TStringBuf spec,
  165. TStringBuf genericSpec);
  166. template <class TValue>
  167. void FormatIntValue(
  168. TStringBuilderBase* builder,
  169. TValue value,
  170. TStringBuf spec,
  171. TStringBuf genericSpec);
  172. void FormatPointerValue(
  173. TStringBuilderBase* builder,
  174. const void* value,
  175. TStringBuf spec);
  176. ////////////////////////////////////////////////////////////////////////////////
  177. // Helper concepts for matching the correct overload.
  178. // NB(arkady-e1ppa): We prefer to hardcode the known types
  179. // so that someone doesn't accidentally implement the
  180. // "SimpleRange" concept and have a non-trivial
  181. // formatting procedure at the same time.
  182. template <class R>
  183. concept CKnownRange =
  184. requires (R r) { [] <class... Ts> (std::vector<Ts...>) { } (r); } ||
  185. requires (R r) { [] <class T, size_t E> (std::span<T, E>) { } (r); } ||
  186. requires (R r) { [] <class T, size_t N> (TCompactVector<T, N>) { } (r); } ||
  187. requires (R r) { [] <class... Ts> (std::set<Ts...>) { } (r); } ||
  188. requires (R r) { [] <class... Ts> (THashSet<Ts...>) { } (r); } ||
  189. requires (R r) { [] <class... Ts> (THashMultiSet<Ts...>) { } (r); };
  190. ////////////////////////////////////////////////////////////////////////////////
  191. template <class R>
  192. concept CKnownKVRange =
  193. requires (R r) { [] <class... Ts> (std::map<Ts...>) { } (r); } ||
  194. requires (R r) { [] <class... Ts> (std::multimap<Ts...>) { } (r); } ||
  195. requires (R r) { [] <class... Ts> (THashMap<Ts...>) { } (r); } ||
  196. requires (R r) { [] <class... Ts> (THashMultiMap<Ts...>) { } (r); };
  197. } // namespace NDetail
  198. ////////////////////////////////////////////////////////////////////////////////
  199. template <class TRange, class TFormatter>
  200. void FormatRange(TStringBuilderBase* builder, const TRange& range, const TFormatter& formatter, size_t limit = std::numeric_limits<size_t>::max())
  201. {
  202. builder->AppendChar('[');
  203. size_t index = 0;
  204. for (const auto& item : range) {
  205. if (index > 0) {
  206. builder->AppendString(DefaultJoinToStringDelimiter);
  207. }
  208. if (index == limit) {
  209. builder->AppendString(DefaultRangeEllipsisFormat);
  210. break;
  211. }
  212. formatter(builder, item);
  213. ++index;
  214. }
  215. builder->AppendChar(']');
  216. }
  217. ////////////////////////////////////////////////////////////////////////////////
  218. template <class TRange, class TFormatter>
  219. void FormatKeyValueRange(TStringBuilderBase* builder, const TRange& range, const TFormatter& formatter, size_t limit = std::numeric_limits<size_t>::max())
  220. {
  221. builder->AppendChar('{');
  222. size_t index = 0;
  223. for (const auto& item : range) {
  224. if (index > 0) {
  225. builder->AppendString(DefaultJoinToStringDelimiter);
  226. }
  227. if (index == limit) {
  228. builder->AppendString(DefaultRangeEllipsisFormat);
  229. break;
  230. }
  231. formatter(builder, item.first);
  232. builder->AppendString(DefaultKeyValueDelimiter);
  233. formatter(builder, item.second);
  234. ++index;
  235. }
  236. builder->AppendChar('}');
  237. }
  238. ////////////////////////////////////////////////////////////////////////////////
  239. template <class R>
  240. concept CFormattableRange =
  241. NDetail::CKnownRange<R> &&
  242. CFormattable<typename R::value_type>;
  243. template <class R>
  244. concept CFormattableKVRange =
  245. NDetail::CKnownKVRange<R> &&
  246. CFormattable<typename R::key_type> &&
  247. CFormattable<typename R::value_type>;
  248. ////////////////////////////////////////////////////////////////////////////////
  249. template <class TRange, class TFormatter>
  250. typename TFormattableView<TRange, TFormatter>::TBegin TFormattableView<TRange, TFormatter>::begin() const
  251. {
  252. return RangeBegin;
  253. }
  254. template <class TRange, class TFormatter>
  255. typename TFormattableView<TRange, TFormatter>::TEnd TFormattableView<TRange, TFormatter>::end() const
  256. {
  257. return RangeEnd;
  258. }
  259. template <class TRange, class TFormatter>
  260. TFormattableView<TRange, TFormatter> MakeFormattableView(
  261. const TRange& range,
  262. TFormatter&& formatter)
  263. {
  264. return TFormattableView<TRange, std::decay_t<TFormatter>>{range.begin(), range.end(), std::forward<TFormatter>(formatter)};
  265. }
  266. template <class TRange, class TFormatter>
  267. TFormattableView<TRange, TFormatter> MakeShrunkFormattableView(
  268. const TRange& range,
  269. TFormatter&& formatter,
  270. size_t limit)
  271. {
  272. return TFormattableView<TRange, std::decay_t<TFormatter>>{
  273. range.begin(),
  274. range.end(),
  275. std::forward<TFormatter>(formatter),
  276. limit};
  277. }
  278. template <class TFormatter>
  279. TFormatterWrapper<TFormatter> MakeFormatterWrapper(
  280. TFormatter&& formatter)
  281. {
  282. return TFormatterWrapper<TFormatter>{
  283. .Formatter = std::move(formatter)
  284. };
  285. }
  286. template <class... TArgs>
  287. TLazyMultiValueFormatter<TArgs...>::TLazyMultiValueFormatter(
  288. TStringBuf format,
  289. TArgs&&... args)
  290. : Format_(format)
  291. , Args_(std::forward<TArgs>(args)...)
  292. { }
  293. template <class... TArgs>
  294. auto MakeLazyMultiValueFormatter(TStringBuf format, TArgs&&... args)
  295. {
  296. return TLazyMultiValueFormatter<TArgs...>(format, std::forward<TArgs>(args)...);
  297. }
  298. ////////////////////////////////////////////////////////////////////////////////
  299. // Non-container objects.
  300. #define XX(valueType, castType, genericSpec) \
  301. inline void FormatValue(TStringBuilderBase* builder, valueType value, TStringBuf spec) \
  302. { \
  303. NYT::NDetail::FormatIntValue(builder, static_cast<castType>(value), spec, genericSpec); \
  304. }
  305. XX(i8, i32, TStringBuf("d"))
  306. XX(ui8, ui32, TStringBuf("u"))
  307. XX(i16, i32, TStringBuf("d"))
  308. XX(ui16, ui32, TStringBuf("u"))
  309. XX(i32, i32, TStringBuf("d"))
  310. XX(ui32, ui32, TStringBuf("u"))
  311. XX(long, i64, TStringBuf(PRIdLEAST64))
  312. XX(long long, i64, TStringBuf(PRIdLEAST64))
  313. XX(unsigned long, ui64, TStringBuf(PRIuLEAST64))
  314. XX(unsigned long long, ui64, TStringBuf(PRIuLEAST64))
  315. #undef XX
  316. #define XX(valueType, castType, genericSpec) \
  317. inline void FormatValue(TStringBuilderBase* builder, valueType value, TStringBuf spec) \
  318. { \
  319. NYT::NDetail::FormatValueViaSprintf(builder, static_cast<castType>(value), spec, genericSpec); \
  320. }
  321. XX(double, double, TStringBuf("lf"))
  322. XX(float, float, TStringBuf("f"))
  323. #undef XX
  324. // Pointer
  325. template <class T>
  326. void FormatValue(TStringBuilderBase* builder, T* value, TStringBuf spec)
  327. {
  328. NYT::NDetail::FormatPointerValue(builder, static_cast<const void*>(value), spec);
  329. }
  330. // TStringBuf
  331. inline void FormatValue(TStringBuilderBase* builder, TStringBuf value, TStringBuf spec)
  332. {
  333. if (!spec) {
  334. builder->AppendString(value);
  335. return;
  336. }
  337. // Parse alignment.
  338. bool alignLeft = false;
  339. const char* current = spec.begin();
  340. if (*current == '-') {
  341. alignLeft = true;
  342. ++current;
  343. }
  344. bool hasAlign = false;
  345. int alignSize = 0;
  346. while (*current >= '0' && *current <= '9') {
  347. hasAlign = true;
  348. alignSize = 10 * alignSize + (*current - '0');
  349. if (alignSize > 1000000) {
  350. builder->AppendString(TStringBuf("<alignment overflow>"));
  351. return;
  352. }
  353. ++current;
  354. }
  355. int padding = 0;
  356. bool padLeft = false;
  357. bool padRight = false;
  358. if (hasAlign) {
  359. padding = alignSize - value.size();
  360. if (padding < 0) {
  361. padding = 0;
  362. }
  363. padLeft = !alignLeft;
  364. padRight = alignLeft;
  365. }
  366. bool singleQuotes = false;
  367. bool doubleQuotes = false;
  368. bool escape = false;
  369. while (current < spec.end()) {
  370. switch (*current++) {
  371. case 'q':
  372. singleQuotes = true;
  373. break;
  374. case 'Q':
  375. doubleQuotes = true;
  376. break;
  377. case 'h':
  378. escape = true;
  379. break;
  380. }
  381. }
  382. if (padLeft) {
  383. builder->AppendChar(' ', padding);
  384. }
  385. if (singleQuotes || doubleQuotes || escape) {
  386. for (const char* valueCurrent = value.begin(); valueCurrent < value.end(); ++valueCurrent) {
  387. char ch = *valueCurrent;
  388. if (ch == '\n') {
  389. builder->AppendString("\\n");
  390. } else if (ch == '\t') {
  391. builder->AppendString("\\t");
  392. } else if (ch == '\\') {
  393. builder->AppendString("\\\\");
  394. } else if (ch < PrintableASCIILow || ch > PrintableASCIIHigh) {
  395. builder->AppendString("\\x");
  396. builder->AppendChar(IntToHexLowercase[static_cast<ui8>(ch) >> 4]);
  397. builder->AppendChar(IntToHexLowercase[static_cast<ui8>(ch) & 0xf]);
  398. } else if ((singleQuotes && ch == '\'') || (doubleQuotes && ch == '\"')) {
  399. builder->AppendChar('\\');
  400. builder->AppendChar(ch);
  401. } else {
  402. builder->AppendChar(ch);
  403. }
  404. }
  405. } else {
  406. builder->AppendString(value);
  407. }
  408. if (padRight) {
  409. builder->AppendChar(' ', padding);
  410. }
  411. }
  412. // TString
  413. inline void FormatValue(TStringBuilderBase* builder, const TString& value, TStringBuf spec)
  414. {
  415. FormatValue(builder, TStringBuf(value), spec);
  416. }
  417. // const char*
  418. inline void FormatValue(TStringBuilderBase* builder, const char* value, TStringBuf spec)
  419. {
  420. FormatValue(builder, TStringBuf(value), spec);
  421. }
  422. template <size_t N>
  423. inline void FormatValue(TStringBuilderBase* builder, const char (&value)[N], TStringBuf spec)
  424. {
  425. FormatValue(builder, TStringBuf(value), spec);
  426. }
  427. // char*
  428. inline void FormatValue(TStringBuilderBase* builder, char* value, TStringBuf spec)
  429. {
  430. FormatValue(builder, TStringBuf(value), spec);
  431. }
  432. // std::string
  433. inline void FormatValue(TStringBuilderBase* builder, const std::string& value, TStringBuf spec)
  434. {
  435. FormatValue(builder, TStringBuf(value), spec);
  436. }
  437. // std::string_view
  438. inline void FormatValue(TStringBuilderBase* builder, const std::string_view& value, TStringBuf spec)
  439. {
  440. FormatValue(builder, TStringBuf(value), spec);
  441. }
  442. #if __cplusplus >= 202302L
  443. // std::filesystem::path
  444. inline void FormatValue(TStringBuilderBase* builder, const std::filesystem::path& value, TStringBuf spec)
  445. {
  446. FormatValue(builder, std::string(value), spec);
  447. }
  448. #endif
  449. // char
  450. inline void FormatValue(TStringBuilderBase* builder, char value, TStringBuf spec)
  451. {
  452. FormatValue(builder, TStringBuf(&value, 1), spec);
  453. }
  454. // bool
  455. inline void FormatValue(TStringBuilderBase* builder, bool value, TStringBuf spec)
  456. {
  457. // Parse custom flags.
  458. bool lowercase = false;
  459. const char* current = spec.begin();
  460. while (current != spec.end()) {
  461. if (*current == 'l') {
  462. ++current;
  463. lowercase = true;
  464. } else if (NYT::NDetail::IsQuotationSpecSymbol(*current)) {
  465. ++current;
  466. } else
  467. break;
  468. }
  469. auto str = lowercase
  470. ? (value ? TStringBuf("true") : TStringBuf("false"))
  471. : (value ? TStringBuf("True") : TStringBuf("False"));
  472. builder->AppendString(str);
  473. }
  474. // TDuration
  475. inline void FormatValue(TStringBuilderBase* builder, TDuration value, TStringBuf /*spec*/)
  476. {
  477. builder->AppendFormat("%vus", value.MicroSeconds());
  478. }
  479. // TInstant
  480. inline void FormatValue(TStringBuilderBase* builder, TInstant value, TStringBuf spec)
  481. {
  482. // TODO(babenko): Optimize.
  483. FormatValue(builder, NYT::ToStringIgnoringFormatValue(value), spec);
  484. }
  485. // Enum
  486. template <class TEnum>
  487. requires (TEnumTraits<TEnum>::IsEnum)
  488. void FormatValue(TStringBuilderBase* builder, TEnum value, TStringBuf spec)
  489. {
  490. // Parse custom flags.
  491. bool lowercase = false;
  492. const char* current = spec.begin();
  493. while (current != spec.end()) {
  494. if (*current == 'l') {
  495. ++current;
  496. lowercase = true;
  497. } else if (NYT::NDetail::IsQuotationSpecSymbol(*current)) {
  498. ++current;
  499. } else {
  500. break;
  501. }
  502. }
  503. FormatEnum(builder, value, lowercase);
  504. }
  505. template <class TArcadiaEnum>
  506. requires (std::is_enum_v<TArcadiaEnum> && !TEnumTraits<TArcadiaEnum>::IsEnum)
  507. void FormatValue(TStringBuilderBase* builder, TArcadiaEnum value, TStringBuf /*spec*/)
  508. {
  509. // NB(arkady-e1ppa): This can catch normal enums which
  510. // just want to be serialized as numbers.
  511. // Unfortunately, we have no way of determining that other than
  512. // marking every relevant arcadia enum in the code by trait
  513. // or writing their complete trait and placing such trait in
  514. // every single file where it is formatted.
  515. // We gotta figure something out but until that
  516. // we will just have to make a string for such enums.
  517. // If only arcadia enums provided compile-time check
  518. // if enum is serializable :(((((.
  519. builder->AppendString(NYT::ToStringIgnoringFormatValue(value));
  520. }
  521. // Container objects.
  522. // NB(arkady-e1ppa): In order to support container combinations
  523. // we forward-declare them before defining.
  524. // TMaybe
  525. template <class T, class TPolicy>
  526. void FormatValue(TStringBuilderBase* builder, const TMaybe<T, TPolicy>& value, TStringBuf spec);
  527. // std::optional
  528. template <CFormattable T>
  529. void FormatValue(TStringBuilderBase* builder, const std::optional<T>& value, TStringBuf spec);
  530. // std::pair
  531. template <CFormattable A, CFormattable B>
  532. void FormatValue(TStringBuilderBase* builder, const std::pair<A, B>& value, TStringBuf spec);
  533. // std::tuple
  534. template <CFormattable... Ts>
  535. void FormatValue(TStringBuilderBase* builder, const std::tuple<Ts...>& value, TStringBuf spec);
  536. // TEnumIndexedArray
  537. template <class E, CFormattable T>
  538. void FormatValue(TStringBuilderBase* builder, const TEnumIndexedArray<E, T>& collection, TStringBuf spec);
  539. // One-valued ranges
  540. template <CFormattableRange TRange>
  541. void FormatValue(TStringBuilderBase* builder, const TRange& collection, TStringBuf spec);
  542. // Two-valued ranges
  543. template <CFormattableKVRange TRange>
  544. void FormatValue(TStringBuilderBase* builder, const TRange& collection, TStringBuf spec);
  545. // FormattableView
  546. template <class TRange, class TFormatter>
  547. void FormatValue(
  548. TStringBuilderBase* builder,
  549. const TFormattableView<TRange, TFormatter>& formattableView,
  550. TStringBuf spec);
  551. // TFormatterWrapper
  552. template <class TFormatter>
  553. void FormatValue(
  554. TStringBuilderBase* builder,
  555. const TFormatterWrapper<TFormatter>& wrapper,
  556. TStringBuf spec);
  557. // TLazyMultiValueFormatter
  558. template <class... TArgs>
  559. void FormatValue(
  560. TStringBuilderBase* builder,
  561. const TLazyMultiValueFormatter<TArgs...>& value,
  562. TStringBuf /*spec*/);
  563. // TMaybe
  564. template <class T, class TPolicy>
  565. void FormatValue(TStringBuilderBase* builder, const TMaybe<T, TPolicy>& value, TStringBuf spec)
  566. {
  567. FormatValue(builder, NYT::ToStringIgnoringFormatValue(value), spec);
  568. }
  569. // std::optional: nullopt
  570. inline void FormatValue(TStringBuilderBase* builder, std::nullopt_t, TStringBuf /*spec*/)
  571. {
  572. builder->AppendString(TStringBuf("<null>"));
  573. }
  574. // std::optional: generic T
  575. template <CFormattable T>
  576. void FormatValue(TStringBuilderBase* builder, const std::optional<T>& value, TStringBuf spec)
  577. {
  578. if (value.has_value()) {
  579. FormatValue(builder, *value, spec);
  580. } else {
  581. FormatValue(builder, std::nullopt, spec);
  582. }
  583. }
  584. // std::pair
  585. template <CFormattable A, CFormattable B>
  586. void FormatValue(TStringBuilderBase* builder, const std::pair<A, B>& value, TStringBuf spec)
  587. {
  588. builder->AppendChar('{');
  589. FormatValue(builder, value.first, spec);
  590. builder->AppendString(TStringBuf(", "));
  591. FormatValue(builder, value.second, spec);
  592. builder->AppendChar('}');
  593. }
  594. // std::tuple
  595. template <CFormattable... Ts>
  596. void FormatValue(TStringBuilderBase* builder, const std::tuple<Ts...>& value, TStringBuf spec)
  597. {
  598. builder->AppendChar('{');
  599. [&] <size_t... Idx> (std::index_sequence<Idx...>) {
  600. ([&] {
  601. FormatValue(builder, std::get<Idx>(value), spec);
  602. if constexpr (Idx != sizeof...(Ts)) {
  603. builder->AppendString(TStringBuf(", "));
  604. }
  605. } (), ...);
  606. } (std::index_sequence_for<Ts...>());
  607. builder->AppendChar('}');
  608. }
  609. // TEnumIndexedArray
  610. template <class E, CFormattable T>
  611. void FormatValue(TStringBuilderBase* builder, const TEnumIndexedArray<E, T>& collection, TStringBuf spec)
  612. {
  613. builder->AppendChar('{');
  614. bool firstItem = true;
  615. for (const auto& index : TEnumTraits<E>::GetDomainValues()) {
  616. if (!firstItem) {
  617. builder->AppendString(DefaultJoinToStringDelimiter);
  618. }
  619. FormatValue(builder, index, spec);
  620. builder->AppendString(": ");
  621. FormatValue(builder, collection[index], spec);
  622. firstItem = false;
  623. }
  624. builder->AppendChar('}');
  625. }
  626. // One-valued ranges
  627. template <CFormattableRange TRange>
  628. void FormatValue(TStringBuilderBase* builder, const TRange& collection, TStringBuf /*spec*/)
  629. {
  630. NYT::FormatRange(builder, collection, TDefaultFormatter());
  631. }
  632. // Two-valued ranges
  633. template <CFormattableKVRange TRange>
  634. void FormatValue(TStringBuilderBase* builder, const TRange& collection, TStringBuf /*spec*/)
  635. {
  636. NYT::FormatKeyValueRange(builder, collection, TDefaultFormatter());
  637. }
  638. // FormattableView
  639. template <class TRange, class TFormatter>
  640. void FormatValue(
  641. TStringBuilderBase* builder,
  642. const TFormattableView<TRange, TFormatter>& formattableView,
  643. TStringBuf /*spec*/)
  644. {
  645. NYT::FormatRange(builder, formattableView, formattableView.Formatter, formattableView.Limit);
  646. }
  647. // TFormatterWrapper
  648. template <class TFormatter>
  649. void FormatValue(
  650. TStringBuilderBase* builder,
  651. const TFormatterWrapper<TFormatter>& wrapper,
  652. TStringBuf /*spec*/)
  653. {
  654. wrapper.Formatter(builder);
  655. }
  656. // TLazyMultiValueFormatter
  657. template <class... TArgs>
  658. void FormatValue(
  659. TStringBuilderBase* builder,
  660. const TLazyMultiValueFormatter<TArgs...>& value,
  661. TStringBuf /*spec*/)
  662. {
  663. std::apply(
  664. [&] <class... TInnerArgs> (TInnerArgs&&... args) {
  665. builder->AppendFormat(value.Format_, std::forward<TInnerArgs>(args)...);
  666. },
  667. value.Args_);
  668. }
  669. ////////////////////////////////////////////////////////////////////////////////
  670. namespace NDetail {
  671. template <size_t HeadPos, class... TArgs>
  672. class TValueFormatter;
  673. template <size_t HeadPos>
  674. class TValueFormatter<HeadPos>
  675. {
  676. public:
  677. void operator() (size_t /*index*/, TStringBuilderBase* builder, TStringBuf /*spec*/) const
  678. {
  679. builder->AppendString(TStringBuf("<missing argument>"));
  680. }
  681. };
  682. template <size_t HeadPos, class THead, class... TTail>
  683. class TValueFormatter<HeadPos, THead, TTail...>
  684. {
  685. public:
  686. explicit TValueFormatter(const THead& head, const TTail&... tail) noexcept
  687. : Head_(head)
  688. , TailFormatter_(tail...)
  689. { }
  690. void operator() (size_t index, TStringBuilderBase* builder, TStringBuf spec) const
  691. {
  692. YT_ASSERT(index >= HeadPos);
  693. if (index == HeadPos) {
  694. FormatValue(builder, Head_, spec);
  695. } else {
  696. TailFormatter_(index, builder, spec);
  697. }
  698. }
  699. private:
  700. const THead& Head_;
  701. TValueFormatter<HeadPos + 1, TTail...> TailFormatter_;
  702. };
  703. ////////////////////////////////////////////////////////////////////////////////
  704. template <class TRangeValue>
  705. class TRangeFormatter
  706. {
  707. public:
  708. template <class... TArgs>
  709. requires std::constructible_from<std::span<const TRangeValue>, TArgs...>
  710. explicit TRangeFormatter(TArgs&&... args) noexcept
  711. : Span_(std::forward<TArgs>(args)...)
  712. { }
  713. void operator() (size_t index, TStringBuilderBase* builder, TStringBuf spec) const
  714. {
  715. if (index >= Span_.size()) {
  716. builder->AppendString(TStringBuf("<missing argument>"));
  717. } else {
  718. FormatValue(builder, *(Span_.begin() + index), spec);
  719. }
  720. }
  721. private:
  722. std::span<const TRangeValue> Span_;
  723. };
  724. ////////////////////////////////////////////////////////////////////////////////
  725. template <class T>
  726. concept CFormatter = CInvocable<T, void(size_t, TStringBuilderBase*, TStringBuf)>;
  727. ////////////////////////////////////////////////////////////////////////////////
  728. template <CFormatter TFormatter>
  729. void RunFormatter(
  730. TStringBuilderBase* builder,
  731. TStringBuf format,
  732. const TFormatter& formatter)
  733. {
  734. size_t argIndex = 0;
  735. auto current = std::begin(format);
  736. auto end = std::end(format);
  737. while (true) {
  738. // Scan verbatim part until stop symbol.
  739. auto verbatimBegin = current;
  740. auto verbatimEnd = std::find(current, end, IntroductorySymbol);
  741. // Copy verbatim part, if any.
  742. size_t verbatimSize = verbatimEnd - verbatimBegin;
  743. if (verbatimSize > 0) {
  744. builder->AppendString(TStringBuf(verbatimBegin, verbatimSize));
  745. }
  746. // Handle stop symbol.
  747. current = verbatimEnd;
  748. if (current == end) {
  749. break;
  750. }
  751. YT_ASSERT(*current == IntroductorySymbol);
  752. ++current;
  753. if (*current == IntroductorySymbol) {
  754. // Verbatim %.
  755. builder->AppendChar(IntroductorySymbol);
  756. ++current;
  757. continue;
  758. }
  759. // Scan format part until stop symbol.
  760. auto argFormatBegin = current;
  761. auto argFormatEnd = argFormatBegin;
  762. bool singleQuotes = false;
  763. bool doubleQuotes = false;
  764. while (
  765. argFormatEnd != end &&
  766. *argFormatEnd != GenericSpecSymbol && // value in generic format
  767. *argFormatEnd != 'd' && // others are standard specifiers supported by printf
  768. *argFormatEnd != 'i' &&
  769. *argFormatEnd != 'u' &&
  770. *argFormatEnd != 'o' &&
  771. *argFormatEnd != 'x' &&
  772. *argFormatEnd != 'X' &&
  773. *argFormatEnd != 'f' &&
  774. *argFormatEnd != 'F' &&
  775. *argFormatEnd != 'e' &&
  776. *argFormatEnd != 'E' &&
  777. *argFormatEnd != 'g' &&
  778. *argFormatEnd != 'G' &&
  779. *argFormatEnd != 'a' &&
  780. *argFormatEnd != 'A' &&
  781. *argFormatEnd != 'c' &&
  782. *argFormatEnd != 's' &&
  783. *argFormatEnd != 'p' &&
  784. *argFormatEnd != 'n')
  785. {
  786. switch (*argFormatEnd) {
  787. case 'q':
  788. singleQuotes = true;
  789. break;
  790. case 'Q':
  791. doubleQuotes = true;
  792. break;
  793. case 'h':
  794. break;
  795. }
  796. ++argFormatEnd;
  797. }
  798. // Handle end of format string.
  799. if (argFormatEnd != end) {
  800. ++argFormatEnd;
  801. }
  802. // 'n' means 'nothing'; skip the argument.
  803. if (*argFormatBegin != 'n') {
  804. // Format argument.
  805. TStringBuf argFormat(argFormatBegin, argFormatEnd);
  806. if (singleQuotes) {
  807. builder->AppendChar('\'');
  808. }
  809. if (doubleQuotes) {
  810. builder->AppendChar('"');
  811. }
  812. formatter(argIndex++, builder, argFormat);
  813. if (singleQuotes) {
  814. builder->AppendChar('\'');
  815. }
  816. if (doubleQuotes) {
  817. builder->AppendChar('"');
  818. }
  819. }
  820. current = argFormatEnd;
  821. }
  822. }
  823. } // namespace NDetail
  824. ////////////////////////////////////////////////////////////////////////////////
  825. template <class... TArgs>
  826. void Format(TStringBuilderBase* builder, TFormatString<TArgs...> format, TArgs&&... args)
  827. {
  828. // NB(arkady-e1ppa): "if constexpr" is done in order to prevent
  829. // compiler from emitting "No matching function to call"
  830. // when arguments are not formattable.
  831. // Compiler would crash in TFormatString ctor
  832. // anyway (e.g. program would not compile) but
  833. // for some reason it does look ahead and emits
  834. // a second error.
  835. if constexpr ((CFormattable<TArgs> && ...)) {
  836. NYT::NDetail::TValueFormatter<0, TArgs...> formatter(args...);
  837. NYT::NDetail::RunFormatter(builder, format.Get(), formatter);
  838. }
  839. }
  840. template <class... TArgs>
  841. TString Format(TFormatString<TArgs...> format, TArgs&&... args)
  842. {
  843. TStringBuilder builder;
  844. Format(&builder, format, std::forward<TArgs>(args)...);
  845. return builder.Flush();
  846. }
  847. ////////////////////////////////////////////////////////////////////////////////
  848. template <size_t Length, class TVector>
  849. void FormatVector(
  850. TStringBuilderBase* builder,
  851. const char (&format)[Length],
  852. const TVector& vec)
  853. {
  854. NYT::NDetail::TRangeFormatter<typename TVector::value_type> formatter(vec);
  855. NYT::NDetail::RunFormatter(builder, format, formatter);
  856. }
  857. template <class TVector>
  858. void FormatVector(
  859. TStringBuilderBase* builder,
  860. TStringBuf format,
  861. const TVector& vec)
  862. {
  863. NYT::NDetail::TRangeFormatter<typename TVector::value_type> formatter(vec);
  864. NYT::NDetail::RunFormatter(builder, format, formatter);
  865. }
  866. template <size_t Length, class TVector>
  867. TString FormatVector(
  868. const char (&format)[Length],
  869. const TVector& vec)
  870. {
  871. TStringBuilder builder;
  872. FormatVector(&builder, format, vec);
  873. return builder.Flush();
  874. }
  875. template <class TVector>
  876. TString FormatVector(
  877. TStringBuf format,
  878. const TVector& vec)
  879. {
  880. TStringBuilder builder;
  881. FormatVector(&builder, format, vec);
  882. return builder.Flush();
  883. }
  884. ////////////////////////////////////////////////////////////////////////////////
  885. } // namespace NYT
  886. #include <util/string/cast.h>
  887. // util/string/cast.h extension for yt and std types only
  888. // TODO(arkady-e1ppa): Abolish ::ToString in
  889. // favour of either NYT::ToString or
  890. // automatic formatting wherever it is needed.
  891. namespace NPrivate {
  892. ////////////////////////////////////////////////////////////////////////////////
  893. template <class T>
  894. requires (
  895. (NYT::NDetail::IsNYTName<T>() ||
  896. NYT::NDetail::IsStdName<T>()) &&
  897. NYT::CFormattable<T>)
  898. struct TToString<T, false>
  899. {
  900. static TString Cvt(const T& t)
  901. {
  902. return NYT::ToStringViaBuilder(t);
  903. }
  904. };
  905. ////////////////////////////////////////////////////////////////////////////////
  906. } // namespace NPrivate