format_string.h 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. private:
  34. std::string_view Format_;
  35. template <class T>
  36. static void CrashCompilerClassIsNotFormattable();
  37. };
  38. // Used to properly infer template arguments in Format.
  39. template <class... TArgs>
  40. using TFormatString = TBasicFormatString<std::type_identity_t<TArgs>...>;
  41. ////////////////////////////////////////////////////////////////////////////////
  42. template <class T>
  43. concept CStringLiteral = requires (T& t) {
  44. [] (const char*) { } (t);
  45. };
  46. ////////////////////////////////////////////////////////////////////////////////
  47. } // namespace NYT
  48. #define FORMAT_STRING_INL_H_
  49. #include "format_string-inl.h"
  50. #undef FORMAT_STRING_INL_H_