format-inl.h 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077
  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/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 R>
  193. concept CFormattableRange =
  194. NDetail::CKnownRange<R> &&
  195. CFormattable<typename R::value_type>;
  196. template <class R>
  197. concept CFormattableKVRange =
  198. NDetail::CKnownKVRange<R> &&
  199. CFormattable<typename R::key_type> &&
  200. CFormattable<typename R::value_type>;
  201. ////////////////////////////////////////////////////////////////////////////////
  202. // Specializations of TFormatArg for ranges
  203. template <class R>
  204. requires CFormattableRange<std::remove_cvref_t<R>>
  205. struct TFormatArg<R>
  206. : public TFormatArgBase
  207. {
  208. using TUnderlying = typename std::remove_cvref_t<R>::value_type;
  209. static constexpr auto ConversionSpecifiers = TFormatArg<TUnderlying>::ConversionSpecifiers;
  210. static constexpr auto FlagSpecifiers = TFormatArg<TUnderlying>::FlagSpecifiers;
  211. };
  212. ////////////////////////////////////////////////////////////////////////////////
  213. template <class TRange, class TFormatter>
  214. typename TFormattableView<TRange, TFormatter>::TBegin TFormattableView<TRange, TFormatter>::begin() const
  215. {
  216. return RangeBegin;
  217. }
  218. template <class TRange, class TFormatter>
  219. typename TFormattableView<TRange, TFormatter>::TEnd TFormattableView<TRange, TFormatter>::end() const
  220. {
  221. return RangeEnd;
  222. }
  223. template <class TRange, class TFormatter>
  224. TFormattableView<TRange, TFormatter> MakeFormattableView(
  225. const TRange& range,
  226. TFormatter&& formatter)
  227. {
  228. return TFormattableView<TRange, std::decay_t<TFormatter>>{range.begin(), range.end(), std::forward<TFormatter>(formatter)};
  229. }
  230. template <class TRange, class TFormatter>
  231. TFormattableView<TRange, TFormatter> MakeShrunkFormattableView(
  232. const TRange& range,
  233. TFormatter&& formatter,
  234. size_t limit)
  235. {
  236. return TFormattableView<TRange, std::decay_t<TFormatter>>{
  237. range.begin(),
  238. range.end(),
  239. std::forward<TFormatter>(formatter),
  240. limit};
  241. }
  242. template <class TFormatter>
  243. TFormatterWrapper<TFormatter> MakeFormatterWrapper(
  244. TFormatter&& formatter)
  245. {
  246. return TFormatterWrapper<TFormatter>{
  247. .Formatter = std::move(formatter)
  248. };
  249. }
  250. template <class... TArgs>
  251. TLazyMultiValueFormatter<TArgs...>::TLazyMultiValueFormatter(
  252. TStringBuf format,
  253. TArgs&&... args)
  254. : Format_(format)
  255. , Args_(std::forward<TArgs>(args)...)
  256. { }
  257. template <class... TArgs>
  258. auto MakeLazyMultiValueFormatter(TStringBuf format, TArgs&&... args)
  259. {
  260. return TLazyMultiValueFormatter<TArgs...>(format, std::forward<TArgs>(args)...);
  261. }
  262. ////////////////////////////////////////////////////////////////////////////////
  263. // Non-container objects.
  264. #define XX(valueType, castType, genericSpec) \
  265. inline void FormatValue(TStringBuilderBase* builder, valueType value, TStringBuf spec) \
  266. { \
  267. NYT::NDetail::FormatIntValue(builder, static_cast<castType>(value), spec, genericSpec); \
  268. }
  269. XX(i8, i32, TStringBuf("d"))
  270. XX(ui8, ui32, TStringBuf("u"))
  271. XX(i16, i32, TStringBuf("d"))
  272. XX(ui16, ui32, TStringBuf("u"))
  273. XX(i32, i32, TStringBuf("d"))
  274. XX(ui32, ui32, TStringBuf("u"))
  275. XX(long, i64, TStringBuf(PRIdLEAST64))
  276. XX(long long, i64, TStringBuf(PRIdLEAST64))
  277. XX(unsigned long, ui64, TStringBuf(PRIuLEAST64))
  278. XX(unsigned long long, ui64, TStringBuf(PRIuLEAST64))
  279. #undef XX
  280. #define XX(valueType, castType, genericSpec) \
  281. inline void FormatValue(TStringBuilderBase* builder, valueType value, TStringBuf spec) \
  282. { \
  283. NYT::NDetail::FormatValueViaSprintf(builder, static_cast<castType>(value), spec, genericSpec); \
  284. }
  285. XX(double, double, TStringBuf("lf"))
  286. XX(float, float, TStringBuf("f"))
  287. #undef XX
  288. // Pointer
  289. template <class T>
  290. void FormatValue(TStringBuilderBase* builder, T* value, TStringBuf spec)
  291. {
  292. NYT::NDetail::FormatPointerValue(builder, static_cast<const void*>(value), spec);
  293. }
  294. // TStringBuf
  295. inline void FormatValue(TStringBuilderBase* builder, TStringBuf value, TStringBuf spec)
  296. {
  297. if (!spec) {
  298. builder->AppendString(value);
  299. return;
  300. }
  301. // Parse alignment.
  302. bool alignLeft = false;
  303. const char* current = spec.begin();
  304. if (*current == '-') {
  305. alignLeft = true;
  306. ++current;
  307. }
  308. bool hasAlign = false;
  309. int alignSize = 0;
  310. while (*current >= '0' && *current <= '9') {
  311. hasAlign = true;
  312. alignSize = 10 * alignSize + (*current - '0');
  313. if (alignSize > 1000000) {
  314. builder->AppendString(TStringBuf("<alignment overflow>"));
  315. return;
  316. }
  317. ++current;
  318. }
  319. int padding = 0;
  320. bool padLeft = false;
  321. bool padRight = false;
  322. if (hasAlign) {
  323. padding = alignSize - value.size();
  324. if (padding < 0) {
  325. padding = 0;
  326. }
  327. padLeft = !alignLeft;
  328. padRight = alignLeft;
  329. }
  330. bool singleQuotes = false;
  331. bool doubleQuotes = false;
  332. bool escape = false;
  333. while (current < spec.end()) {
  334. switch (*current++) {
  335. case 'q':
  336. singleQuotes = true;
  337. break;
  338. case 'Q':
  339. doubleQuotes = true;
  340. break;
  341. case 'h':
  342. escape = true;
  343. break;
  344. }
  345. }
  346. if (padLeft) {
  347. builder->AppendChar(' ', padding);
  348. }
  349. if (singleQuotes || doubleQuotes || escape) {
  350. for (const char* valueCurrent = value.begin(); valueCurrent < value.end(); ++valueCurrent) {
  351. char ch = *valueCurrent;
  352. if (ch == '\n') {
  353. builder->AppendString("\\n");
  354. } else if (ch == '\t') {
  355. builder->AppendString("\\t");
  356. } else if (ch == '\\') {
  357. builder->AppendString("\\\\");
  358. } else if (ch < PrintableASCIILow || ch > PrintableASCIIHigh) {
  359. builder->AppendString("\\x");
  360. builder->AppendChar(IntToHexLowercase[static_cast<ui8>(ch) >> 4]);
  361. builder->AppendChar(IntToHexLowercase[static_cast<ui8>(ch) & 0xf]);
  362. } else if ((singleQuotes && ch == '\'') || (doubleQuotes && ch == '\"')) {
  363. builder->AppendChar('\\');
  364. builder->AppendChar(ch);
  365. } else {
  366. builder->AppendChar(ch);
  367. }
  368. }
  369. } else {
  370. builder->AppendString(value);
  371. }
  372. if (padRight) {
  373. builder->AppendChar(' ', padding);
  374. }
  375. }
  376. // TString
  377. inline void FormatValue(TStringBuilderBase* builder, const TString& value, TStringBuf spec)
  378. {
  379. FormatValue(builder, TStringBuf(value), spec);
  380. }
  381. // const char*
  382. inline void FormatValue(TStringBuilderBase* builder, const char* value, TStringBuf spec)
  383. {
  384. FormatValue(builder, TStringBuf(value), spec);
  385. }
  386. template <size_t N>
  387. inline void FormatValue(TStringBuilderBase* builder, const char (&value)[N], TStringBuf spec)
  388. {
  389. FormatValue(builder, TStringBuf(value), spec);
  390. }
  391. // char*
  392. inline void FormatValue(TStringBuilderBase* builder, char* value, TStringBuf spec)
  393. {
  394. FormatValue(builder, TStringBuf(value), spec);
  395. }
  396. // std::string
  397. inline void FormatValue(TStringBuilderBase* builder, const std::string& value, TStringBuf spec)
  398. {
  399. FormatValue(builder, TStringBuf(value), spec);
  400. }
  401. // std::string_view
  402. inline void FormatValue(TStringBuilderBase* builder, const std::string_view& value, TStringBuf spec)
  403. {
  404. FormatValue(builder, TStringBuf(value), spec);
  405. }
  406. #if __cplusplus >= 202302L
  407. // std::filesystem::path
  408. inline void FormatValue(TStringBuilderBase* builder, const std::filesystem::path& value, TStringBuf spec)
  409. {
  410. FormatValue(builder, std::string(value), spec);
  411. }
  412. #endif
  413. #ifdef __cpp_lib_source_location
  414. // std::source_location
  415. inline void FormatValue(TStringBuilderBase* builder, const std::source_location& location, TStringBuf /*spec*/)
  416. {
  417. if (location.file_name() != nullptr) {
  418. builder->AppendFormat(
  419. "%v:%v:%v",
  420. location.file_name(),
  421. location.line(),
  422. location.column());
  423. } else {
  424. builder->AppendString("<unknown>");
  425. }
  426. }
  427. #endif // __cpp_lib_source_location
  428. // TSourceLocation
  429. inline void FormatValue(TStringBuilderBase* builder, const TSourceLocation& location, TStringBuf /*spec*/)
  430. {
  431. if (location.GetFileName() != nullptr) {
  432. builder->AppendFormat(
  433. "%v:%v",
  434. location.GetFileName(),
  435. location.GetLine());
  436. } else {
  437. builder->AppendString("<unknown>");
  438. }
  439. }
  440. // std::monostate
  441. inline void FormatValue(TStringBuilderBase* builder, const std::monostate&, TStringBuf /*spec*/)
  442. {
  443. builder->AppendString(TStringBuf("<monostate>"));
  444. }
  445. // std::variant
  446. template <class... Ts>
  447. requires (CFormattable<Ts> && ...)
  448. void FormatValue(TStringBuilderBase* builder, const std::variant<Ts...>& variant, TStringBuf spec)
  449. {
  450. [&] <size_t... Ids> (std::index_sequence<Ids...>) {
  451. ([&] {
  452. if (variant.index() == Ids) {
  453. FormatValue(builder, std::get<Ids>(variant), spec);
  454. return false;
  455. }
  456. return true;
  457. } () && ...);
  458. } (std::index_sequence_for<Ts...>());
  459. }
  460. // char
  461. inline void FormatValue(TStringBuilderBase* builder, char value, TStringBuf spec)
  462. {
  463. FormatValue(builder, TStringBuf(&value, 1), spec);
  464. }
  465. // bool
  466. inline void FormatValue(TStringBuilderBase* builder, bool value, TStringBuf spec)
  467. {
  468. // Parse custom flags.
  469. bool lowercase = false;
  470. const char* current = spec.begin();
  471. while (current != spec.end()) {
  472. if (*current == 'l') {
  473. ++current;
  474. lowercase = true;
  475. } else if (NYT::NDetail::IsQuotationSpecSymbol(*current)) {
  476. ++current;
  477. } else
  478. break;
  479. }
  480. auto str = lowercase
  481. ? (value ? TStringBuf("true") : TStringBuf("false"))
  482. : (value ? TStringBuf("True") : TStringBuf("False"));
  483. builder->AppendString(str);
  484. }
  485. // TDuration
  486. inline void FormatValue(TStringBuilderBase* builder, TDuration value, TStringBuf /*spec*/)
  487. {
  488. builder->AppendFormat("%vus", value.MicroSeconds());
  489. }
  490. // TInstant
  491. inline void FormatValue(TStringBuilderBase* builder, TInstant value, TStringBuf spec)
  492. {
  493. // TODO(babenko): Optimize.
  494. FormatValue(builder, NYT::ToStringIgnoringFormatValue(value), spec);
  495. }
  496. // Enum
  497. template <class TEnum>
  498. requires (TEnumTraits<TEnum>::IsEnum)
  499. void FormatValue(TStringBuilderBase* builder, TEnum value, TStringBuf spec)
  500. {
  501. // Parse custom flags.
  502. bool lowercase = false;
  503. const char* current = spec.begin();
  504. while (current != spec.end()) {
  505. if (*current == 'l') {
  506. ++current;
  507. lowercase = true;
  508. } else if (NYT::NDetail::IsQuotationSpecSymbol(*current)) {
  509. ++current;
  510. } else {
  511. break;
  512. }
  513. }
  514. FormatEnum(builder, value, lowercase);
  515. }
  516. template <class TArcadiaEnum>
  517. requires (std::is_enum_v<TArcadiaEnum> && !TEnumTraits<TArcadiaEnum>::IsEnum)
  518. void FormatValue(TStringBuilderBase* builder, TArcadiaEnum value, TStringBuf /*spec*/)
  519. {
  520. // NB(arkady-e1ppa): This can catch normal enums which
  521. // just want to be serialized as numbers.
  522. // Unfortunately, we have no way of determining that other than
  523. // marking every relevant arcadia enum in the code by trait
  524. // or writing their complete trait and placing such trait in
  525. // every single file where it is formatted.
  526. // We gotta figure something out but until that
  527. // we will just have to make a string for such enums.
  528. // If only arcadia enums provided compile-time check
  529. // if enum is serializable :(((((.
  530. builder->AppendString(NYT::ToStringIgnoringFormatValue(value));
  531. }
  532. // Container objects.
  533. // NB(arkady-e1ppa): In order to support container combinations
  534. // we forward-declare them before defining.
  535. // TMaybe
  536. template <class T, class TPolicy>
  537. void FormatValue(TStringBuilderBase* builder, const TMaybe<T, TPolicy>& value, TStringBuf spec);
  538. // std::optional
  539. template <CFormattable T>
  540. void FormatValue(TStringBuilderBase* builder, const std::optional<T>& value, TStringBuf spec);
  541. // std::pair
  542. template <CFormattable A, CFormattable B>
  543. void FormatValue(TStringBuilderBase* builder, const std::pair<A, B>& value, TStringBuf spec);
  544. // std::tuple
  545. template <CFormattable... Ts>
  546. void FormatValue(TStringBuilderBase* builder, const std::tuple<Ts...>& value, TStringBuf spec);
  547. // TEnumIndexedArray
  548. template <class E, CFormattable T>
  549. void FormatValue(TStringBuilderBase* builder, const TEnumIndexedArray<E, T>& collection, TStringBuf spec);
  550. // One-valued ranges
  551. template <CFormattableRange TRange>
  552. void FormatValue(TStringBuilderBase* builder, const TRange& collection, TStringBuf spec);
  553. // Two-valued ranges
  554. template <CFormattableKVRange TRange>
  555. void FormatValue(TStringBuilderBase* builder, const TRange& collection, TStringBuf spec);
  556. // FormattableView
  557. template <class TRange, class TFormatter>
  558. void FormatValue(
  559. TStringBuilderBase* builder,
  560. const TFormattableView<TRange, TFormatter>& formattableView,
  561. TStringBuf spec);
  562. // TFormatterWrapper
  563. template <class TFormatter>
  564. void FormatValue(
  565. TStringBuilderBase* builder,
  566. const TFormatterWrapper<TFormatter>& wrapper,
  567. TStringBuf spec);
  568. // TLazyMultiValueFormatter
  569. template <class... TArgs>
  570. void FormatValue(
  571. TStringBuilderBase* builder,
  572. const TLazyMultiValueFormatter<TArgs...>& value,
  573. TStringBuf /*spec*/);
  574. // TMaybe
  575. template <class T, class TPolicy>
  576. void FormatValue(TStringBuilderBase* builder, const TMaybe<T, TPolicy>& value, TStringBuf spec)
  577. {
  578. FormatValue(builder, NYT::ToStringIgnoringFormatValue(value), spec);
  579. }
  580. // std::optional: nullopt
  581. inline void FormatValue(TStringBuilderBase* builder, std::nullopt_t, TStringBuf /*spec*/)
  582. {
  583. builder->AppendString(TStringBuf("<null>"));
  584. }
  585. // std::optional: generic T
  586. template <CFormattable T>
  587. void FormatValue(TStringBuilderBase* builder, const std::optional<T>& value, TStringBuf spec)
  588. {
  589. if (value.has_value()) {
  590. FormatValue(builder, *value, spec);
  591. } else {
  592. FormatValue(builder, std::nullopt, spec);
  593. }
  594. }
  595. // std::pair
  596. template <CFormattable A, CFormattable B>
  597. void FormatValue(TStringBuilderBase* builder, const std::pair<A, B>& value, TStringBuf spec)
  598. {
  599. builder->AppendChar('{');
  600. FormatValue(builder, value.first, spec);
  601. builder->AppendString(TStringBuf(", "));
  602. FormatValue(builder, value.second, spec);
  603. builder->AppendChar('}');
  604. }
  605. // std::tuple
  606. template <CFormattable... Ts>
  607. void FormatValue(TStringBuilderBase* builder, const std::tuple<Ts...>& value, TStringBuf spec)
  608. {
  609. builder->AppendChar('{');
  610. [&] <size_t... Idx> (std::index_sequence<Idx...>) {
  611. ([&] {
  612. FormatValue(builder, std::get<Idx>(value), spec);
  613. if constexpr (Idx != sizeof...(Ts)) {
  614. builder->AppendString(TStringBuf(", "));
  615. }
  616. } (), ...);
  617. } (std::index_sequence_for<Ts...>());
  618. builder->AppendChar('}');
  619. }
  620. // TEnumIndexedArray
  621. template <class E, CFormattable T>
  622. void FormatValue(TStringBuilderBase* builder, const TEnumIndexedArray<E, T>& collection, TStringBuf spec)
  623. {
  624. builder->AppendChar('{');
  625. bool firstItem = true;
  626. for (const auto& index : TEnumTraits<E>::GetDomainValues()) {
  627. if (!firstItem) {
  628. builder->AppendString(DefaultJoinToStringDelimiter);
  629. }
  630. FormatValue(builder, index, spec);
  631. builder->AppendString(": ");
  632. FormatValue(builder, collection[index], spec);
  633. firstItem = false;
  634. }
  635. builder->AppendChar('}');
  636. }
  637. // One-valued ranges
  638. template <CFormattableRange TRange>
  639. void FormatValue(TStringBuilderBase* builder, const TRange& collection, TStringBuf spec)
  640. {
  641. NYT::FormatRange(builder, collection, TSpecBoundFormatter(spec));
  642. }
  643. // Two-valued ranges
  644. template <CFormattableKVRange TRange>
  645. void FormatValue(TStringBuilderBase* builder, const TRange& collection, TStringBuf /*spec*/)
  646. {
  647. NYT::FormatKeyValueRange(builder, collection, TDefaultFormatter());
  648. }
  649. // FormattableView
  650. template <class TRange, class TFormatter>
  651. void FormatValue(
  652. TStringBuilderBase* builder,
  653. const TFormattableView<TRange, TFormatter>& formattableView,
  654. TStringBuf /*spec*/)
  655. {
  656. NYT::FormatRange(builder, formattableView, formattableView.Formatter, formattableView.Limit);
  657. }
  658. // TFormatterWrapper
  659. template <class TFormatter>
  660. void FormatValue(
  661. TStringBuilderBase* builder,
  662. const TFormatterWrapper<TFormatter>& wrapper,
  663. TStringBuf /*spec*/)
  664. {
  665. wrapper.Formatter(builder);
  666. }
  667. // TLazyMultiValueFormatter
  668. template <class... TArgs>
  669. void FormatValue(
  670. TStringBuilderBase* builder,
  671. const TLazyMultiValueFormatter<TArgs...>& value,
  672. TStringBuf /*spec*/)
  673. {
  674. std::apply(
  675. [&] <class... TInnerArgs> (TInnerArgs&&... args) {
  676. builder->AppendFormat(value.Format_, std::forward<TInnerArgs>(args)...);
  677. },
  678. value.Args_);
  679. }
  680. ////////////////////////////////////////////////////////////////////////////////
  681. namespace NDetail {
  682. template <size_t HeadPos, class... TArgs>
  683. class TValueFormatter;
  684. template <size_t HeadPos>
  685. class TValueFormatter<HeadPos>
  686. {
  687. public:
  688. void operator() (size_t /*index*/, TStringBuilderBase* builder, TStringBuf /*spec*/) const
  689. {
  690. builder->AppendString(TStringBuf("<missing argument>"));
  691. }
  692. };
  693. template <size_t HeadPos, class THead, class... TTail>
  694. class TValueFormatter<HeadPos, THead, TTail...>
  695. {
  696. public:
  697. explicit TValueFormatter(const THead& head, const TTail&... tail) noexcept
  698. : Head_(head)
  699. , TailFormatter_(tail...)
  700. { }
  701. void operator() (size_t index, TStringBuilderBase* builder, TStringBuf spec) const
  702. {
  703. YT_ASSERT(index >= HeadPos);
  704. if (index == HeadPos) {
  705. FormatValue(builder, Head_, spec);
  706. } else {
  707. TailFormatter_(index, builder, spec);
  708. }
  709. }
  710. private:
  711. const THead& Head_;
  712. TValueFormatter<HeadPos + 1, TTail...> TailFormatter_;
  713. };
  714. ////////////////////////////////////////////////////////////////////////////////
  715. template <class TRangeValue>
  716. class TRangeFormatter
  717. {
  718. public:
  719. template <class... TArgs>
  720. requires std::constructible_from<std::span<const TRangeValue>, TArgs...>
  721. explicit TRangeFormatter(TArgs&&... args) noexcept
  722. : Span_(std::forward<TArgs>(args)...)
  723. { }
  724. void operator() (size_t index, TStringBuilderBase* builder, TStringBuf spec) const
  725. {
  726. if (index >= Span_.size()) {
  727. builder->AppendString(TStringBuf("<missing argument>"));
  728. } else {
  729. FormatValue(builder, *(Span_.begin() + index), spec);
  730. }
  731. }
  732. private:
  733. std::span<const TRangeValue> Span_;
  734. };
  735. ////////////////////////////////////////////////////////////////////////////////
  736. template <class T>
  737. concept CFormatter = CInvocable<T, void(size_t, TStringBuilderBase*, TStringBuf)>;
  738. ////////////////////////////////////////////////////////////////////////////////
  739. template <CFormatter TFormatter>
  740. void RunFormatter(
  741. TStringBuilderBase* builder,
  742. TStringBuf format,
  743. const TFormatter& formatter)
  744. {
  745. size_t argIndex = 0;
  746. auto current = std::begin(format);
  747. auto end = std::end(format);
  748. while (true) {
  749. // Scan verbatim part until stop symbol.
  750. auto verbatimBegin = current;
  751. auto verbatimEnd = std::find(current, end, IntroductorySymbol);
  752. // Copy verbatim part, if any.
  753. size_t verbatimSize = verbatimEnd - verbatimBegin;
  754. if (verbatimSize > 0) {
  755. builder->AppendString(TStringBuf(verbatimBegin, verbatimSize));
  756. }
  757. // Handle stop symbol.
  758. current = verbatimEnd;
  759. if (current == end) {
  760. break;
  761. }
  762. YT_ASSERT(*current == IntroductorySymbol);
  763. ++current;
  764. if (*current == IntroductorySymbol) {
  765. // Verbatim %.
  766. builder->AppendChar(IntroductorySymbol);
  767. ++current;
  768. continue;
  769. }
  770. // Scan format part until stop symbol.
  771. auto argFormatBegin = current;
  772. auto argFormatEnd = argFormatBegin;
  773. bool singleQuotes = false;
  774. bool doubleQuotes = false;
  775. while (
  776. argFormatEnd != end &&
  777. *argFormatEnd != GenericSpecSymbol && // value in generic format
  778. *argFormatEnd != 'd' && // others are standard specifiers supported by printf
  779. *argFormatEnd != 'i' &&
  780. *argFormatEnd != 'u' &&
  781. *argFormatEnd != 'o' &&
  782. *argFormatEnd != 'x' &&
  783. *argFormatEnd != 'X' &&
  784. *argFormatEnd != 'f' &&
  785. *argFormatEnd != 'F' &&
  786. *argFormatEnd != 'e' &&
  787. *argFormatEnd != 'E' &&
  788. *argFormatEnd != 'g' &&
  789. *argFormatEnd != 'G' &&
  790. *argFormatEnd != 'a' &&
  791. *argFormatEnd != 'A' &&
  792. *argFormatEnd != 'c' &&
  793. *argFormatEnd != 's' &&
  794. *argFormatEnd != 'p' &&
  795. *argFormatEnd != 'n')
  796. {
  797. switch (*argFormatEnd) {
  798. case 'q':
  799. singleQuotes = true;
  800. break;
  801. case 'Q':
  802. doubleQuotes = true;
  803. break;
  804. case 'h':
  805. break;
  806. }
  807. ++argFormatEnd;
  808. }
  809. // Handle end of format string.
  810. if (argFormatEnd != end) {
  811. ++argFormatEnd;
  812. }
  813. // 'n' means 'nothing'; skip the argument.
  814. if (*argFormatBegin != 'n') {
  815. // Format argument.
  816. TStringBuf argFormat(argFormatBegin, argFormatEnd);
  817. if (singleQuotes) {
  818. builder->AppendChar('\'');
  819. }
  820. if (doubleQuotes) {
  821. builder->AppendChar('"');
  822. }
  823. formatter(argIndex++, builder, argFormat);
  824. if (singleQuotes) {
  825. builder->AppendChar('\'');
  826. }
  827. if (doubleQuotes) {
  828. builder->AppendChar('"');
  829. }
  830. }
  831. current = argFormatEnd;
  832. }
  833. }
  834. } // namespace NDetail
  835. ////////////////////////////////////////////////////////////////////////////////
  836. template <class... TArgs>
  837. void Format(TStringBuilderBase* builder, TFormatString<TArgs...> format, TArgs&&... args)
  838. {
  839. // NB(arkady-e1ppa): "if constexpr" is done in order to prevent
  840. // compiler from emitting "No matching function to call"
  841. // when arguments are not formattable.
  842. // Compiler would crash in TFormatString ctor
  843. // anyway (e.g. program would not compile) but
  844. // for some reason it does look ahead and emits
  845. // a second error.
  846. if constexpr ((CFormattable<TArgs> && ...)) {
  847. NYT::NDetail::TValueFormatter<0, TArgs...> formatter(args...);
  848. NYT::NDetail::RunFormatter(builder, format.Get(), formatter);
  849. }
  850. }
  851. template <class... TArgs>
  852. TString Format(TFormatString<TArgs...> format, TArgs&&... args)
  853. {
  854. TStringBuilder builder;
  855. Format(&builder, format, std::forward<TArgs>(args)...);
  856. return builder.Flush();
  857. }
  858. ////////////////////////////////////////////////////////////////////////////////
  859. template <size_t Length, class TVector>
  860. void FormatVector(
  861. TStringBuilderBase* builder,
  862. const char (&format)[Length],
  863. const TVector& vec)
  864. {
  865. NYT::NDetail::TRangeFormatter<typename TVector::value_type> formatter(vec);
  866. NYT::NDetail::RunFormatter(builder, format, formatter);
  867. }
  868. template <class TVector>
  869. void FormatVector(
  870. TStringBuilderBase* builder,
  871. TStringBuf format,
  872. const TVector& vec)
  873. {
  874. NYT::NDetail::TRangeFormatter<typename TVector::value_type> formatter(vec);
  875. NYT::NDetail::RunFormatter(builder, format, formatter);
  876. }
  877. template <size_t Length, class TVector>
  878. TString FormatVector(
  879. const char (&format)[Length],
  880. const TVector& vec)
  881. {
  882. TStringBuilder builder;
  883. FormatVector(&builder, format, vec);
  884. return builder.Flush();
  885. }
  886. template <class TVector>
  887. TString FormatVector(
  888. TStringBuf format,
  889. const TVector& vec)
  890. {
  891. TStringBuilder builder;
  892. FormatVector(&builder, format, vec);
  893. return builder.Flush();
  894. }
  895. ////////////////////////////////////////////////////////////////////////////////
  896. } // namespace NYT
  897. #include <util/string/cast.h>
  898. // util/string/cast.h extension for yt and std types only
  899. // TODO(arkady-e1ppa): Abolish ::ToString in
  900. // favour of either NYT::ToString or
  901. // automatic formatting wherever it is needed.
  902. namespace NPrivate {
  903. ////////////////////////////////////////////////////////////////////////////////
  904. template <class T>
  905. requires (
  906. (NYT::NDetail::IsNYTName<T>() ||
  907. NYT::NDetail::IsStdName<T>()) &&
  908. NYT::CFormattable<T>)
  909. struct TToString<T, false>
  910. {
  911. static TString Cvt(const T& t)
  912. {
  913. return NYT::ToStringViaBuilder(t);
  914. }
  915. };
  916. ////////////////////////////////////////////////////////////////////////////////
  917. } // namespace NPrivate