format_string.h 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. #pragma once
  2. #include "format_analyser.h"
  3. #include <util/generic/strbuf.h>
  4. #if (!__clang__ || __clang_major__ < 16)
  5. #define YT_DISABLE_FORMAT_STATIC_ANALYSIS
  6. #endif
  7. namespace NYT {
  8. ////////////////////////////////////////////////////////////////////////////////
  9. // Explicitly create TRuntimeFormat if you wish to
  10. // use runtime/non-literal value as format.
  11. class TRuntimeFormat
  12. {
  13. public:
  14. explicit TRuntimeFormat(TStringBuf fmt);
  15. TStringBuf Get() const noexcept;
  16. private:
  17. TStringBuf Format_;
  18. };
  19. // This class used to properly bind to
  20. // string literals and allow compile-time parsing/checking
  21. // of those. If you need a runtime format, use TRuntimeFormat.
  22. template <class... TArgs>
  23. class TBasicFormatString
  24. {
  25. public:
  26. // Can be used to perform compile-time check of format.
  27. template <class T>
  28. requires std::constructible_from<std::string_view, T>
  29. consteval TBasicFormatString(const T& fmt);
  30. TBasicFormatString(TRuntimeFormat fmt);
  31. TStringBuf Get() const noexcept;
  32. static consteval void CheckFormattability();
  33. // Data used for compile-time slicing of the format string.
  34. NDetail::TFormatAnalyser::TMarkerLocations<TArgs...> Markers = {};
  35. NDetail::TFormatAnalyser::TEscapeLocations Escapes = {};
  36. private:
  37. std::string_view Format_;
  38. template <class T>
  39. static void CrashCompilerClassIsNotFormattable();
  40. };
  41. // Used to properly infer template arguments in Format.
  42. template <class... TArgs>
  43. using TFormatString = TBasicFormatString<std::type_identity_t<TArgs>...>;
  44. ////////////////////////////////////////////////////////////////////////////////
  45. template <class T>
  46. concept CStringLiteral = requires (T& t) {
  47. [] (const char*) { } (t);
  48. };
  49. ////////////////////////////////////////////////////////////////////////////////
  50. } // namespace NYT
  51. #define FORMAT_STRING_INL_H_
  52. #include "format_string-inl.h"
  53. #undef FORMAT_STRING_INL_H_