mirror of
https://github.com/fmtlib/fmt.git
synced 2024-12-01 22:20:06 +00:00
Move float_spec_handler to internal namespace and update asserts
This commit is contained in:
parent
7e1cb3237a
commit
f26446290b
@ -34,7 +34,6 @@
|
|||||||
#define FMT_FORMAT_H_
|
#define FMT_FORMAT_H_
|
||||||
|
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <cassert>
|
|
||||||
#include <cmath>
|
#include <cmath>
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
@ -92,7 +91,7 @@ FMT_END_NAMESPACE
|
|||||||
# define FMT_THROW(x) \
|
# define FMT_THROW(x) \
|
||||||
do { \
|
do { \
|
||||||
static_cast<void>(sizeof(x)); \
|
static_cast<void>(sizeof(x)); \
|
||||||
assert(false); \
|
FMT_ASSERT(false, ""); \
|
||||||
} while (false)
|
} while (false)
|
||||||
# endif
|
# endif
|
||||||
#endif
|
#endif
|
||||||
@ -148,7 +147,7 @@ inline uint32_t clz(uint32_t x) {
|
|||||||
unsigned long r = 0;
|
unsigned long r = 0;
|
||||||
_BitScanReverse(&r, x);
|
_BitScanReverse(&r, x);
|
||||||
|
|
||||||
assert(x != 0);
|
FMT_ASSERT(x != 0, "");
|
||||||
// Static analysis complains about using uninitialized data
|
// Static analysis complains about using uninitialized data
|
||||||
// "r", but the only way that can happen is if "x" is 0,
|
// "r", but the only way that can happen is if "x" is 0,
|
||||||
// which the callers guarantee to not happen.
|
// which the callers guarantee to not happen.
|
||||||
@ -173,7 +172,7 @@ inline uint32_t clzll(uint64_t x) {
|
|||||||
_BitScanReverse(&r, static_cast<uint32_t>(x));
|
_BitScanReverse(&r, static_cast<uint32_t>(x));
|
||||||
# endif
|
# endif
|
||||||
|
|
||||||
assert(x != 0);
|
FMT_ASSERT(x != 0, "");
|
||||||
// Static analysis complains about using uninitialized data
|
// Static analysis complains about using uninitialized data
|
||||||
// "r", but the only way that can happen is if "x" is 0,
|
// "r", but the only way that can happen is if "x" is 0,
|
||||||
// which the callers guarantee to not happen.
|
// which the callers guarantee to not happen.
|
||||||
@ -616,7 +615,7 @@ class basic_memory_buffer : private Allocator, public internal::buffer<T> {
|
|||||||
\endrst
|
\endrst
|
||||||
*/
|
*/
|
||||||
basic_memory_buffer& operator=(basic_memory_buffer&& other) FMT_NOEXCEPT {
|
basic_memory_buffer& operator=(basic_memory_buffer&& other) FMT_NOEXCEPT {
|
||||||
assert(this != &other);
|
FMT_ASSERT(this != &other, "");
|
||||||
deallocate();
|
deallocate();
|
||||||
move(other);
|
move(other);
|
||||||
return *this;
|
return *this;
|
||||||
@ -1329,6 +1328,49 @@ class float_type_checker : private ErrorHandler {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct float_spec_handler {
|
||||||
|
char type;
|
||||||
|
bool upper;
|
||||||
|
bool fixed;
|
||||||
|
bool as_percentage;
|
||||||
|
bool use_locale;
|
||||||
|
|
||||||
|
explicit float_spec_handler(char t)
|
||||||
|
: type(t),
|
||||||
|
upper(false),
|
||||||
|
fixed(false),
|
||||||
|
as_percentage(false),
|
||||||
|
use_locale(false) {}
|
||||||
|
|
||||||
|
void on_general() {
|
||||||
|
if (type == 'G') upper = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void on_exp() {
|
||||||
|
if (type == 'E') upper = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void on_fixed() {
|
||||||
|
fixed = true;
|
||||||
|
if (type == 'F') upper = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void on_percent() {
|
||||||
|
fixed = true;
|
||||||
|
as_percentage = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void on_hex() {
|
||||||
|
if (type == 'A') upper = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void on_num() { use_locale = true; }
|
||||||
|
|
||||||
|
FMT_NORETURN void on_error() {
|
||||||
|
FMT_THROW(format_error("invalid type specifier"));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
template <typename ErrorHandler>
|
template <typename ErrorHandler>
|
||||||
class char_specs_checker : public ErrorHandler {
|
class char_specs_checker : public ErrorHandler {
|
||||||
private:
|
private:
|
||||||
@ -1948,7 +1990,7 @@ template <typename Char> FMT_CONSTEXPR bool is_name_start(Char c) {
|
|||||||
template <typename Char, typename ErrorHandler>
|
template <typename Char, typename ErrorHandler>
|
||||||
FMT_CONSTEXPR int parse_nonnegative_int(const Char*& begin, const Char* end,
|
FMT_CONSTEXPR int parse_nonnegative_int(const Char*& begin, const Char* end,
|
||||||
ErrorHandler&& eh) {
|
ErrorHandler&& eh) {
|
||||||
assert(begin != end && '0' <= *begin && *begin <= '9');
|
FMT_ASSERT(begin != end && '0' <= *begin && *begin <= '9', "");
|
||||||
if (*begin == '0') {
|
if (*begin == '0') {
|
||||||
++begin;
|
++begin;
|
||||||
return 0;
|
return 0;
|
||||||
@ -2298,7 +2340,7 @@ class dynamic_specs_handler
|
|||||||
template <typename Char, typename IDHandler>
|
template <typename Char, typename IDHandler>
|
||||||
FMT_CONSTEXPR const Char* parse_arg_id(const Char* begin, const Char* end,
|
FMT_CONSTEXPR const Char* parse_arg_id(const Char* begin, const Char* end,
|
||||||
IDHandler&& handler) {
|
IDHandler&& handler) {
|
||||||
assert(begin != end);
|
FMT_ASSERT(begin != end, "");
|
||||||
Char c = *begin;
|
Char c = *begin;
|
||||||
if (c == '}' || c == ':') return handler(), begin;
|
if (c == '}' || c == ':') return handler(), begin;
|
||||||
if (c >= '0' && c <= '9') {
|
if (c >= '0' && c <= '9') {
|
||||||
@ -2777,55 +2819,12 @@ class FMT_API system_error : public std::runtime_error {
|
|||||||
FMT_API void format_system_error(internal::buffer<char>& out, int error_code,
|
FMT_API void format_system_error(internal::buffer<char>& out, int error_code,
|
||||||
fmt::string_view message) FMT_NOEXCEPT;
|
fmt::string_view message) FMT_NOEXCEPT;
|
||||||
|
|
||||||
struct float_spec_handler {
|
|
||||||
char type;
|
|
||||||
bool upper;
|
|
||||||
bool fixed;
|
|
||||||
bool as_percentage;
|
|
||||||
bool use_locale;
|
|
||||||
|
|
||||||
explicit float_spec_handler(char t)
|
|
||||||
: type(t),
|
|
||||||
upper(false),
|
|
||||||
fixed(false),
|
|
||||||
as_percentage(false),
|
|
||||||
use_locale(false) {}
|
|
||||||
|
|
||||||
void on_general() {
|
|
||||||
if (type == 'G') upper = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
void on_exp() {
|
|
||||||
if (type == 'E') upper = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
void on_fixed() {
|
|
||||||
fixed = true;
|
|
||||||
if (type == 'F') upper = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
void on_percent() {
|
|
||||||
fixed = true;
|
|
||||||
as_percentage = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
void on_hex() {
|
|
||||||
if (type == 'A') upper = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
void on_num() { use_locale = true; }
|
|
||||||
|
|
||||||
FMT_NORETURN void on_error() {
|
|
||||||
FMT_THROW(format_error("invalid type specifier"));
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
template <typename Range>
|
template <typename Range>
|
||||||
template <typename T, bool USE_GRISU>
|
template <typename T, bool USE_GRISU>
|
||||||
void internal::basic_writer<Range>::write_fp(T value,
|
void internal::basic_writer<Range>::write_fp(T value,
|
||||||
const format_specs& specs) {
|
const format_specs& specs) {
|
||||||
// Check type.
|
// Check type.
|
||||||
float_spec_handler handler(static_cast<char>(specs.type));
|
float_spec_handler handler(specs.type);
|
||||||
handle_float_type_spec(handler.type, handler);
|
handle_float_type_spec(handler.type, handler);
|
||||||
|
|
||||||
auto sign = specs.sign;
|
auto sign = specs.sign;
|
||||||
|
Loading…
Reference in New Issue
Block a user