mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-01-25 02:33:24 -03:00
move-only: Detail_CheckNumFormatSpecifiers and G_TRANSLATION_FUN
This is required for a future commit. Can be reviewed via the git options --color-moved=dimmed-zebra --color-moved-ws=ignore-all-space Also move util::detail::Hex to a proper namespace instead of an inline namespace so it doesn't conflict with the new util::detail namespace, and won't create other problems for callers trying to use the inline namespaces. Also fix a misleading comment in util_string_tests.cpp. Co-Authored-By: Ryan Ofsky <ryan@ofsky.org>
This commit is contained in:
parent
faff8403f0
commit
fa72646f2b
4 changed files with 88 additions and 85 deletions
|
@ -16,13 +16,13 @@ BOOST_AUTO_TEST_SUITE(util_string_tests)
|
||||||
template <unsigned NumArgs>
|
template <unsigned NumArgs>
|
||||||
inline void PassFmt(util::ConstevalFormatString<NumArgs> fmt)
|
inline void PassFmt(util::ConstevalFormatString<NumArgs> fmt)
|
||||||
{
|
{
|
||||||
// This was already executed at compile-time, but is executed again at run-time to avoid -Wunused.
|
// Execute compile-time check again at run-time to get code coverage stats
|
||||||
decltype(fmt)::Detail_CheckNumFormatSpecifiers(fmt.fmt);
|
util::detail::CheckNumFormatSpecifiers<NumArgs>(fmt.fmt);
|
||||||
}
|
}
|
||||||
template <unsigned WrongNumArgs>
|
template <unsigned WrongNumArgs>
|
||||||
inline void FailFmtWithError(const char* wrong_fmt, std::string_view error)
|
inline void FailFmtWithError(const char* wrong_fmt, std::string_view error)
|
||||||
{
|
{
|
||||||
BOOST_CHECK_EXCEPTION(util::ConstevalFormatString<WrongNumArgs>::Detail_CheckNumFormatSpecifiers(wrong_fmt), const char*, HasReason(error));
|
BOOST_CHECK_EXCEPTION(util::detail::CheckNumFormatSpecifiers<WrongNumArgs>(wrong_fmt), const char*, HasReason{error});
|
||||||
}
|
}
|
||||||
|
|
||||||
BOOST_AUTO_TEST_CASE(ConstevalFormatString_NumSpec)
|
BOOST_AUTO_TEST_CASE(ConstevalFormatString_NumSpec)
|
||||||
|
|
|
@ -377,6 +377,24 @@ consteval uint8_t ConstevalHexDigit(const char c)
|
||||||
throw "Only lowercase hex digits are allowed, for consistency";
|
throw "Only lowercase hex digits are allowed, for consistency";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
namespace detail {
|
||||||
|
template <size_t N>
|
||||||
|
struct Hex {
|
||||||
|
std::array<std::byte, N / 2> bytes{};
|
||||||
|
consteval Hex(const char (&hex_str)[N])
|
||||||
|
// 2 hex digits required per byte + implicit null terminator
|
||||||
|
requires(N % 2 == 1)
|
||||||
|
{
|
||||||
|
if (hex_str[N - 1]) throw "null terminator required";
|
||||||
|
for (std::size_t i = 0; i < bytes.size(); ++i) {
|
||||||
|
bytes[i] = static_cast<std::byte>(
|
||||||
|
(ConstevalHexDigit(hex_str[2 * i]) << 4) |
|
||||||
|
ConstevalHexDigit(hex_str[2 * i + 1]));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
} // namespace detail
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* ""_hex is a compile-time user-defined literal returning a
|
* ""_hex is a compile-time user-defined literal returning a
|
||||||
* `std::array<std::byte>`, equivalent to ParseHex(). Variants provided:
|
* `std::array<std::byte>`, equivalent to ParseHex(). Variants provided:
|
||||||
|
@ -407,25 +425,6 @@ consteval uint8_t ConstevalHexDigit(const char c)
|
||||||
* time/runtime barrier.
|
* time/runtime barrier.
|
||||||
*/
|
*/
|
||||||
inline namespace hex_literals {
|
inline namespace hex_literals {
|
||||||
namespace detail {
|
|
||||||
|
|
||||||
template <size_t N>
|
|
||||||
struct Hex {
|
|
||||||
std::array<std::byte, N / 2> bytes{};
|
|
||||||
consteval Hex(const char (&hex_str)[N])
|
|
||||||
// 2 hex digits required per byte + implicit null terminator
|
|
||||||
requires(N % 2 == 1)
|
|
||||||
{
|
|
||||||
if (hex_str[N - 1]) throw "null terminator required";
|
|
||||||
for (std::size_t i = 0; i < bytes.size(); ++i) {
|
|
||||||
bytes[i] = static_cast<std::byte>(
|
|
||||||
(ConstevalHexDigit(hex_str[2 * i]) << 4) |
|
|
||||||
ConstevalHexDigit(hex_str[2 * i + 1]));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
} // namespace detail
|
|
||||||
|
|
||||||
template <util::detail::Hex str>
|
template <util::detail::Hex str>
|
||||||
constexpr auto operator""_hex() { return str.bytes; }
|
constexpr auto operator""_hex() { return str.bytes; }
|
||||||
|
|
|
@ -17,19 +17,9 @@
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
namespace util {
|
namespace util {
|
||||||
/**
|
namespace detail {
|
||||||
* @brief A wrapper for a compile-time partially validated format string
|
|
||||||
*
|
|
||||||
* This struct can be used to enforce partial compile-time validation of format
|
|
||||||
* strings, to reduce the likelihood of tinyformat throwing exceptions at
|
|
||||||
* run-time. Validation is partial to try and prevent the most common errors
|
|
||||||
* while avoiding re-implementing the entire parsing logic.
|
|
||||||
*/
|
|
||||||
template <unsigned num_params>
|
template <unsigned num_params>
|
||||||
struct ConstevalFormatString {
|
constexpr static void CheckNumFormatSpecifiers(const char* str)
|
||||||
const char* const fmt;
|
|
||||||
consteval ConstevalFormatString(const char* str) : fmt{str} { Detail_CheckNumFormatSpecifiers(fmt); }
|
|
||||||
constexpr static void Detail_CheckNumFormatSpecifiers(const char* str)
|
|
||||||
{
|
{
|
||||||
unsigned count_normal{0}; // Number of "normal" specifiers, like %s
|
unsigned count_normal{0}; // Number of "normal" specifiers, like %s
|
||||||
unsigned count_pos{0}; // Max number in positional specifier, like %8$s
|
unsigned count_pos{0}; // Max number in positional specifier, like %8$s
|
||||||
|
@ -88,6 +78,20 @@ struct ConstevalFormatString {
|
||||||
unsigned count{count_normal | count_pos};
|
unsigned count{count_normal | count_pos};
|
||||||
if (num_params != count) throw "Format specifier count must match the argument count!";
|
if (num_params != count) throw "Format specifier count must match the argument count!";
|
||||||
}
|
}
|
||||||
|
} // namespace detail
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief A wrapper for a compile-time partially validated format string
|
||||||
|
*
|
||||||
|
* This struct can be used to enforce partial compile-time validation of format
|
||||||
|
* strings, to reduce the likelihood of tinyformat throwing exceptions at
|
||||||
|
* run-time. Validation is partial to try and prevent the most common errors
|
||||||
|
* while avoiding re-implementing the entire parsing logic.
|
||||||
|
*/
|
||||||
|
template <unsigned num_params>
|
||||||
|
struct ConstevalFormatString {
|
||||||
|
const char* const fmt;
|
||||||
|
consteval ConstevalFormatString(const char* str) : fmt{str} { detail::CheckNumFormatSpecifiers<num_params>(fmt); }
|
||||||
};
|
};
|
||||||
|
|
||||||
void ReplaceAll(std::string& in_out, const std::string& search, const std::string& substitute);
|
void ReplaceAll(std::string& in_out, const std::string& search, const std::string& substitute);
|
||||||
|
|
|
@ -10,6 +10,9 @@
|
||||||
#include <functional>
|
#include <functional>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
|
/** Translate a message to the native language of the user. */
|
||||||
|
const extern std::function<std::string(const char*)> G_TRANSLATION_FUN;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Bilingual messages:
|
* Bilingual messages:
|
||||||
* - in GUI: user's native language + untranslated (i.e. English)
|
* - in GUI: user's native language + untranslated (i.e. English)
|
||||||
|
@ -64,9 +67,6 @@ bilingual_str format(const bilingual_str& fmt, const Args&... args)
|
||||||
}
|
}
|
||||||
} // namespace tinyformat
|
} // namespace tinyformat
|
||||||
|
|
||||||
/** Translate a message to the native language of the user. */
|
|
||||||
const extern std::function<std::string(const char*)> G_TRANSLATION_FUN;
|
|
||||||
|
|
||||||
struct ConstevalStringLiteral {
|
struct ConstevalStringLiteral {
|
||||||
const char* const lit;
|
const char* const lit;
|
||||||
consteval ConstevalStringLiteral(const char* str) : lit{str} {}
|
consteval ConstevalStringLiteral(const char* str) : lit{str} {}
|
||||||
|
|
Loading…
Add table
Reference in a new issue