From 50e716737d2dded9d89ba98b31469a074c1badfc Mon Sep 17 00:00:00 2001 From: Victor Zverovich Date: Sat, 18 Feb 2017 06:52:52 -0800 Subject: [PATCH] StringRef -> string_view, LongLong -> long_long --- fmt/format.cc | 24 ++--- fmt/format.h | 191 ++++++++++++++++++--------------------- fmt/ostream.h | 2 +- fmt/posix.cc | 6 +- fmt/posix.h | 2 +- fmt/printf.h | 4 +- test/format-impl-test.cc | 6 +- test/format-test.cc | 18 ++-- test/gtest-extra.cc | 2 +- test/gtest-extra.h | 2 +- test/posix-mock-test.cc | 4 +- test/posix-test.cc | 2 +- test/printf-test.cc | 40 ++++---- test/util-test.cc | 44 ++++----- 14 files changed, 167 insertions(+), 180 deletions(-) diff --git a/fmt/format.cc b/fmt/format.cc index 222cc71f..563a8f7d 100644 --- a/fmt/format.cc +++ b/fmt/format.cc @@ -106,7 +106,7 @@ inline int fmt_snprintf(char *buffer, size_t size, const char *format, ...) { const char RESET_COLOR[] = "\x1b[0m"; -typedef void (*FormatFunc)(buffer &, int, StringRef); +typedef void (*FormatFunc)(buffer &, int, string_view); // Portable thread-safe version of strerror. // Sets buffer to point to a string describing the error code. @@ -177,7 +177,7 @@ int safe_strerror( } void format_error_code(buffer &out, int error_code, - StringRef message) FMT_NOEXCEPT { + string_view message) FMT_NOEXCEPT { // Report error code making sure that the output fits into // INLINE_BUFFER_SIZE to avoid dynamic memory allocation and potential // bad_alloc. @@ -204,7 +204,7 @@ void format_error_code(buffer &out, int error_code, } void report_error(FormatFunc func, int error_code, - StringRef message) FMT_NOEXCEPT { + string_view message) FMT_NOEXCEPT { internal::MemoryBuffer full_message; func(full_message, error_code, message); // Use Writer::data instead of Writer::c_str to avoid potential memory @@ -279,10 +279,10 @@ template const uint64_t internal::BasicData::POWERS_OF_10_64[] = { 0, FMT_POWERS_OF_10(1), - FMT_POWERS_OF_10(ULongLong(1000000000)), + FMT_POWERS_OF_10(ulong_long(1000000000)), // Multiply several constants instead of using a single long long constant // to avoid warnings about C++98 not supporting long long. - ULongLong(1000000000) * ULongLong(1000000000) * 10 + ulong_long(1000000000) * ulong_long(1000000000) * 10 }; FMT_FUNC void internal::report_unknown_type(char code, const char *type) { @@ -298,7 +298,7 @@ FMT_FUNC void internal::report_unknown_type(char code, const char *type) { #if FMT_USE_WINDOWS_H -FMT_FUNC internal::UTF8ToUTF16::UTF8ToUTF16(StringRef s) { +FMT_FUNC internal::UTF8ToUTF16::UTF8ToUTF16(string_view s) { static const char ERROR_MSG[] = "cannot convert string from UTF-8 to UTF-16"; if (s.size() > INT_MAX) FMT_THROW(WindowsError(ERROR_INVALID_PARAMETER, ERROR_MSG)); @@ -315,14 +315,14 @@ FMT_FUNC internal::UTF8ToUTF16::UTF8ToUTF16(StringRef s) { buffer_[length] = 0; } -FMT_FUNC internal::UTF16ToUTF8::UTF16ToUTF8(WStringRef s) { +FMT_FUNC internal::UTF16ToUTF8::UTF16ToUTF8(wstring_view s) { if (int error_code = convert(s)) { FMT_THROW(WindowsError(error_code, "cannot convert string from UTF-16 to UTF-8")); } } -FMT_FUNC int internal::UTF16ToUTF8::convert(WStringRef s) { +FMT_FUNC int internal::UTF16ToUTF8::convert(wstring_view s) { if (s.size() > INT_MAX) return ERROR_INVALID_PARAMETER; int s_size = static_cast(s.size()); @@ -348,7 +348,7 @@ FMT_FUNC void WindowsError::init( } FMT_FUNC void internal::format_windows_error( - buffer &out, int error_code, StringRef message) FMT_NOEXCEPT { + buffer &out, int error_code, string_view message) FMT_NOEXCEPT { FMT_TRY { MemoryBuffer buffer; buffer.resize(INLINE_BUFFER_SIZE); @@ -380,7 +380,7 @@ FMT_FUNC void internal::format_windows_error( #endif // FMT_USE_WINDOWS_H FMT_FUNC void format_system_error( - buffer &out, int error_code, StringRef message) FMT_NOEXCEPT { + buffer &out, int error_code, string_view message) FMT_NOEXCEPT { FMT_TRY { internal::MemoryBuffer buffer; buffer.resize(internal::INLINE_BUFFER_SIZE); @@ -408,14 +408,14 @@ void FixedBuffer::grow(std::size_t) { } FMT_FUNC void report_system_error( - int error_code, fmt::StringRef message) FMT_NOEXCEPT { + int error_code, fmt::string_view message) FMT_NOEXCEPT { // 'fmt::' is for bcc32. report_error(format_system_error, error_code, message); } #if FMT_USE_WINDOWS_H FMT_FUNC void report_windows_error( - int error_code, fmt::StringRef message) FMT_NOEXCEPT { + int error_code, fmt::string_view message) FMT_NOEXCEPT { // 'fmt::' is for bcc32. report_error(internal::format_windows_error, error_code, message); } diff --git a/fmt/format.h b/fmt/format.h index fff6adfa..3eb8b1fe 100644 --- a/fmt/format.h +++ b/fmt/format.h @@ -82,15 +82,19 @@ typedef __int64 intmax_t; # define FMT_GCC_EXTENSION __extension__ # if FMT_GCC_VERSION >= 406 # pragma GCC diagnostic push + // Disable the warning about "long long" which is sometimes reported even // when using __extension__. # pragma GCC diagnostic ignored "-Wlong-long" + // Disable the warning about declaration shadowing because it affects too // many valid cases. # pragma GCC diagnostic ignored "-Wshadow" + // Disable the warning about implicit conversions that may change the sign of // an integer; silencing it otherwise would require many explicit casts. # pragma GCC diagnostic ignored "-Wsign-conversion" + # endif # if __cplusplus >= 201103L || defined __GXX_EXPERIMENTAL_CXX0X__ # define FMT_HAS_GXX_CXX11 1 @@ -357,8 +361,8 @@ namespace fmt { // Fix the warning about long long on older versions of GCC // that don't support the diagnostic pragma. -FMT_GCC_EXTENSION typedef long long LongLong; -FMT_GCC_EXTENSION typedef unsigned long long ULongLong; +FMT_GCC_EXTENSION typedef long long long_long; +FMT_GCC_EXTENSION typedef unsigned long long ulong_long; #if FMT_USE_RVALUE_REFERENCES using std::move; @@ -390,39 +394,21 @@ typedef basic_context wcontext; /** \rst - A string reference. It can be constructed from a C string or ``std::string``. - - You can use one of the following typedefs for common character types: - - +------------+-------------------------+ - | Type | Definition | - +============+=========================+ - | StringRef | BasicStringRef | - +------------+-------------------------+ - | WStringRef | BasicStringRef | - +------------+-------------------------+ - - This class is most useful as a parameter type to allow passing - different types of strings to a function, for example:: - - template - std::string format(StringRef format_str, const Args & ... args); - - format("{}", 42); - format(std::string("{}"), 42); + An implementation of ``std::basic_string_view`` for pre-C++17. It provides a + subset of the API. \endrst */ template -class BasicStringRef { +class basic_string_view { private: const Char *data_; std::size_t size_; public: - BasicStringRef() : data_(0), size_(0) {} + basic_string_view() : data_(0), size_(0) {} /** Constructs a string reference object from a C string and a size. */ - BasicStringRef(const Char *s, std::size_t size) : data_(s), size_(size) {} + basic_string_view(const Char *s, std::size_t size) : data_(s), size_(size) {} /** \rst @@ -430,7 +416,7 @@ class BasicStringRef { the size with ``std::char_traits::length``. \endrst */ - BasicStringRef(const Char *s) + basic_string_view(const Char *s) : data_(s), size_(std::char_traits::length(s)) {} /** @@ -438,7 +424,7 @@ class BasicStringRef { Constructs a string reference from an ``std::string`` object. \endrst */ - BasicStringRef(const std::basic_string &s) + basic_string_view(const std::basic_string &s) : data_(s.c_str()), size_(s.size()) {} /** @@ -457,7 +443,7 @@ class BasicStringRef { std::size_t size() const { return size_; } // Lexicographically compare this string reference to other. - int compare(BasicStringRef other) const { + int compare(basic_string_view other) const { std::size_t size = size_ < other.size_ ? size_ : other.size_; int result = std::char_traits::compare(data_, other.data_, size); if (result == 0) @@ -465,28 +451,28 @@ class BasicStringRef { return result; } - friend bool operator==(BasicStringRef lhs, BasicStringRef rhs) { + friend bool operator==(basic_string_view lhs, basic_string_view rhs) { return lhs.compare(rhs) == 0; } - friend bool operator!=(BasicStringRef lhs, BasicStringRef rhs) { + friend bool operator!=(basic_string_view lhs, basic_string_view rhs) { return lhs.compare(rhs) != 0; } - friend bool operator<(BasicStringRef lhs, BasicStringRef rhs) { + friend bool operator<(basic_string_view lhs, basic_string_view rhs) { return lhs.compare(rhs) < 0; } - friend bool operator<=(BasicStringRef lhs, BasicStringRef rhs) { + friend bool operator<=(basic_string_view lhs, basic_string_view rhs) { return lhs.compare(rhs) <= 0; } - friend bool operator>(BasicStringRef lhs, BasicStringRef rhs) { + friend bool operator>(basic_string_view lhs, basic_string_view rhs) { return lhs.compare(rhs) > 0; } - friend bool operator>=(BasicStringRef lhs, BasicStringRef rhs) { + friend bool operator>=(basic_string_view lhs, basic_string_view rhs) { return lhs.compare(rhs) >= 0; } }; -typedef BasicStringRef StringRef; -typedef BasicStringRef WStringRef; +typedef basic_string_view string_view; +typedef basic_string_view wstring_view; /** \rst @@ -559,7 +545,7 @@ FMT_SPECIALIZE_MAKE_UNSIGNED(signed char, unsigned char); FMT_SPECIALIZE_MAKE_UNSIGNED(short, unsigned short); FMT_SPECIALIZE_MAKE_UNSIGNED(int, unsigned); FMT_SPECIALIZE_MAKE_UNSIGNED(long, unsigned long); -FMT_SPECIALIZE_MAKE_UNSIGNED(LongLong, ULongLong); +FMT_SPECIALIZE_MAKE_UNSIGNED(long_long, ulong_long); // Casts nonnegative integer to unsigned. template @@ -895,13 +881,13 @@ struct NoThousandsSep { // A functor that adds a thousands separator. class ThousandsSep { private: - fmt::StringRef sep_; + fmt::string_view sep_; // Index of a decimal digit with the least significant digit having index 0. unsigned digit_index_; public: - explicit ThousandsSep(fmt::StringRef sep) : sep_(sep), digit_index_(0) {} + explicit ThousandsSep(fmt::string_view sep) : sep_(sep), digit_index_(0) {} template void operator()(Char *&buffer) { @@ -962,8 +948,8 @@ class UTF8ToUTF16 { MemoryBuffer buffer_; public: - FMT_API explicit UTF8ToUTF16(StringRef s); - operator WStringRef() const { return WStringRef(&buffer_[0], size()); } + FMT_API explicit UTF8ToUTF16(string_view s); + operator wstring_view() const { return wstring_view(&buffer_[0], size()); } size_t size() const { return buffer_.size() - 1; } const wchar_t *c_str() const { return &buffer_[0]; } std::wstring str() const { return std::wstring(&buffer_[0], size()); } @@ -977,8 +963,8 @@ class UTF16ToUTF8 { public: UTF16ToUTF8() {} - FMT_API explicit UTF16ToUTF8(WStringRef s); - operator StringRef() const { return StringRef(&buffer_[0], size()); } + FMT_API explicit UTF16ToUTF8(wstring_view s); + operator string_view() const { return string_view(&buffer_[0], size()); } size_t size() const { return buffer_.size() - 1; } const char *c_str() const { return &buffer_[0]; } std::string str() const { return std::string(&buffer_[0], size()); } @@ -986,11 +972,11 @@ class UTF16ToUTF8 { // Performs conversion returning a system error code instead of // throwing exception on conversion error. This method may still throw // in case of memory allocation error. - FMT_API int convert(WStringRef s); + FMT_API int convert(wstring_view s); }; FMT_API void format_windows_error(fmt::buffer &out, int error_code, - fmt::StringRef message) FMT_NOEXCEPT; + fmt::string_view message) FMT_NOEXCEPT; #endif template @@ -1032,7 +1018,7 @@ template T &get(); // These are non-members to workaround an overload resolution bug in bcc32. -Yes &convert(fmt::ULongLong); +Yes &convert(fmt::ulong_long); No &convert(...); template @@ -1119,8 +1105,8 @@ template <> constexpr Type gettype() { return sizeof(unsigned long) == sizeof(unsigned) ? UINT : ULONG_LONG; } -template <> constexpr Type gettype() { return LONG_LONG; } -template <> constexpr Type gettype() { return ULONG_LONG; } +template <> constexpr Type gettype() { return LONG_LONG; } +template <> constexpr Type gettype() { return ULONG_LONG; } template <> constexpr Type gettype() { return DOUBLE; } template <> constexpr Type gettype() { return DOUBLE; } template <> constexpr Type gettype() { return LONG_DOUBLE; } @@ -1139,12 +1125,12 @@ template <> constexpr Type gettype() { return CSTRING; } template <> constexpr Type gettype() { return CSTRING; } template <> constexpr Type gettype() { return CSTRING; } template <> constexpr Type gettype() { return STRING; } -template <> constexpr Type gettype() { return STRING; } +template <> constexpr Type gettype() { return STRING; } template <> constexpr Type gettype() { return CSTRING; } template <> constexpr Type gettype() { return TSTRING; } template <> constexpr Type gettype() { return TSTRING; } template <> constexpr Type gettype() { return TSTRING; } -template <> constexpr Type gettype() { return TSTRING; } +template <> constexpr Type gettype() { return TSTRING; } template <> constexpr Type gettype() { return POINTER; } template <> constexpr Type gettype() { return POINTER; } @@ -1158,8 +1144,8 @@ class value { union { int int_value; unsigned uint_value; - LongLong long_long_value; - ULongLong ulong_long_value; + long_long long_long_value; + ulong_long ulong_long_value; double double_value; long double long_double_value; const void *pointer; @@ -1193,14 +1179,14 @@ class value { value(typename WCharHelper::Unsupported); value(typename WCharHelper::Unsupported); value(typename WCharHelper::Unsupported); - value(typename WCharHelper::Unsupported); + value(typename WCharHelper::Unsupported); - void set_string(StringRef str) { + void set_string(string_view str) { this->string.value = str.data(); this->string.size = str.size(); } - void set_string(WStringRef str) { + void set_string(wstring_view str) { this->tstring.value = str.data(); this->tstring.size = str.size(); } @@ -1247,8 +1233,8 @@ class value { this->ulong_long_value = value; } - FMT_MAKE_VALUE(LongLong, long_long_value, LONG_LONG) - FMT_MAKE_VALUE(ULongLong, ulong_long_value, ULONG_LONG) + FMT_MAKE_VALUE(long_long, long_long_value, LONG_LONG) + FMT_MAKE_VALUE(ulong_long, ulong_long_value, ULONG_LONG) FMT_MAKE_VALUE(float, double_value, DOUBLE) FMT_MAKE_VALUE(double, double_value, DOUBLE) FMT_MAKE_VALUE(long double, long_double_value, LONG_DOUBLE) @@ -1277,7 +1263,7 @@ class value { FMT_MAKE_VALUE(unsigned char *, ustring.value, CSTRING) FMT_MAKE_VALUE(const unsigned char *, ustring.value, CSTRING) FMT_MAKE_STR_VALUE(const std::string &, STRING) - FMT_MAKE_STR_VALUE(StringRef, STRING) + FMT_MAKE_STR_VALUE(string_view, STRING) FMT_MAKE_VALUE_(CStringRef, string.value, CSTRING, value.c_str()) #define FMT_MAKE_WSTR_VALUE(Type, TYPE) \ @@ -1289,7 +1275,7 @@ class value { FMT_MAKE_WSTR_VALUE(wchar_t *, TSTRING) FMT_MAKE_WSTR_VALUE(const wchar_t *, TSTRING) FMT_MAKE_WSTR_VALUE(const std::wstring &, TSTRING) - FMT_MAKE_WSTR_VALUE(WStringRef, TSTRING) + FMT_MAKE_WSTR_VALUE(wstring_view, TSTRING) FMT_MAKE_VALUE(void *, pointer, POINTER) FMT_MAKE_VALUE(const void *, pointer, POINTER) @@ -1406,9 +1392,9 @@ typename std::result_of::type case internal::CSTRING: return vis(arg.value_.string.value); case internal::STRING: - return vis(StringRef(arg.value_.string.value, arg.value_.string.size)); + return vis(string_view(arg.value_.string.value, arg.value_.string.size)); case internal::TSTRING: - return vis(BasicStringRef( + return vis(basic_string_view( arg.value_.tstring.value, arg.value_.tstring.size)); case internal::POINTER: return vis(arg.value_.pointer); @@ -1436,12 +1422,12 @@ template struct LConvCheck { // We check if ``lconv`` contains ``thousands_sep`` because on Android // ``lconv`` is stubbed as an empty struct. template -inline StringRef thousands_sep( +inline string_view thousands_sep( LConv *lc, LConvCheck = 0) { return lc->thousands_sep; } -inline fmt::StringRef thousands_sep(...) { return ""; } +inline fmt::string_view thousands_sep(...) { return ""; } #define FMT_CONCAT(a, b) a##b @@ -1476,10 +1462,10 @@ template struct NamedArg : basic_arg { typedef typename Context::char_type Char; - BasicStringRef name; + basic_string_view name; template - NamedArg(BasicStringRef argname, const T &value) + NamedArg(basic_string_view argname, const T &value) : basic_arg(make_arg(value)), name(argname) {} }; @@ -1788,7 +1774,7 @@ class ArgMap { private: typedef typename Context::char_type Char; typedef std::vector< - std::pair, basic_arg > > MapType; + std::pair, basic_arg > > MapType; typedef typename MapType::value_type Pair; MapType map_; @@ -1797,7 +1783,7 @@ class ArgMap { void init(const basic_args &args); const basic_arg - *find(const fmt::BasicStringRef &name) const { + *find(const fmt::basic_string_view &name) const { // The list is unsorted, so just return the first matching name. for (typename MapType::const_iterator it = map_.begin(), end = map_.end(); it != end; ++it) { @@ -1871,7 +1857,7 @@ class ArgFormatterBase { } template - void write_str(BasicStringRef value, + void write_str(basic_string_view value, typename EnableIf< std::is_same::value && std::is_same::value, int>::type = 0) { @@ -1879,7 +1865,7 @@ class ArgFormatterBase { } template - void write_str(BasicStringRef value, + void write_str(basic_string_view value, typename EnableIf< !std::is_same::value || !std::is_same::value, int>::type = 0) { @@ -1891,12 +1877,12 @@ class ArgFormatterBase { format_specs &spec() { return spec_; } void write(bool value) { - writer_.write_str(StringRef(value ? "true" : "false"), spec_); + writer_.write_str(string_view(value ? "true" : "false"), spec_); } void write(const char *value) { writer_.write_str( - StringRef(value, value != 0 ? std::strlen(value) : 0), spec_); + string_view(value, value != 0 ? std::strlen(value) : 0), spec_); } public: @@ -1959,11 +1945,11 @@ class ArgFormatterBase { write(value); } - void operator()(StringRef value) { + void operator()(string_view value) { writer_.write_str(value, spec_); } - void operator()(BasicStringRef value) { + void operator()(basic_string_view value) { write_str(value); } @@ -2078,7 +2064,7 @@ class basic_context : // Checks if manual indexing is used and returns the argument with // specified name. - format_arg get_arg(BasicStringRef name, const char *&error); + format_arg get_arg(basic_string_view name, const char *&error); public: /** @@ -2156,7 +2142,7 @@ class SystemError : public internal::RuntimeError { \endrst */ FMT_API void format_system_error(fmt::buffer &out, int error_code, - fmt::StringRef message) FMT_NOEXCEPT; + fmt::string_view message) FMT_NOEXCEPT; namespace internal { // Named format specifier. @@ -2268,7 +2254,7 @@ class basic_writer { CharPtr write_str(const StrChar *s, std::size_t size, const AlignSpec &spec); template - void write_str(BasicStringRef str, const format_specs &spec); + void write_str(basic_string_view str, const format_specs &spec); // This following methods are private to disallow writing wide characters // and strings to a char buffer. If you want to print a wide string as a @@ -2343,7 +2329,7 @@ class basic_writer { void write(long value) { write_decimal(value); } - void write(LongLong value) { + void write(long_long value) { write_decimal(value); } @@ -2388,18 +2374,19 @@ class basic_writer { Writes *value* to the buffer. \endrst */ - void write(fmt::BasicStringRef value) { + void write(fmt::basic_string_view value) { const Char *str = value.data(); buffer_.append(str, str + value.size()); } - void write(typename internal::WCharHelper::Supported value) { + void write( + typename internal::WCharHelper::Supported value) { const char *str = value.data(); buffer_.append(str, str + value.size()); } template - void write(BasicStringRef str, FormatSpecs... specs) { + void write(basic_string_view str, FormatSpecs... specs) { write_str(str, format_specs(specs...)); } @@ -2434,7 +2421,7 @@ typename basic_writer::CharPtr basic_writer::write_str( template template void basic_writer::write_str( - BasicStringRef s, const format_specs &spec) { + basic_string_view s, const format_specs &spec) { // Check if StrChar is convertible to Char. internal::CharTraits::convert(StrChar()); if (spec.type_ && spec.type_ != 's') @@ -2606,7 +2593,7 @@ void basic_writer::write_int(T value, Spec spec) { } case 'n': { unsigned num_digits = internal::count_digits(abs_value); - fmt::StringRef sep = internal::thousands_sep(std::localeconv()); + fmt::string_view sep = internal::thousands_sep(std::localeconv()); unsigned size = static_cast( num_digits + sep.size() * ((num_digits - 1) / 3)); CharPtr p = prepare_int_buffer(size, spec, prefix, prefix_size) + 1; @@ -2880,7 +2867,7 @@ class FixedBuffer : public basic_buffer { // Reports a system error without throwing an exception. // Can be used to report errors from destructors. FMT_API void report_system_error(int error_code, - StringRef message) FMT_NOEXCEPT; + string_view message) FMT_NOEXCEPT; #if FMT_USE_WINDOWS_H @@ -2927,7 +2914,7 @@ class WindowsError : public SystemError { // Reports a Windows error without throwing an exception. // Can be used to report errors from destructors. FMT_API void report_windows_error(int error_code, - StringRef message) FMT_NOEXCEPT; + string_view message) FMT_NOEXCEPT; #endif @@ -3041,12 +3028,12 @@ class FormatInt { private: // Buffer should be large enough to hold all digits (digits10 + 1), // a sign and a null character. - enum {BUFFER_SIZE = std::numeric_limits::digits10 + 3}; + enum {BUFFER_SIZE = std::numeric_limits::digits10 + 3}; mutable char buffer_[BUFFER_SIZE]; char *str_; // Formats value in reverse and returns the number of digits. - char *format_decimal(ULongLong value) { + char *format_decimal(ulong_long value) { char *buffer_end = buffer_ + BUFFER_SIZE - 1; while (value >= 100) { // Integer division is slow so do it for a group of two digits instead @@ -3067,8 +3054,8 @@ class FormatInt { return buffer_end; } - void FormatSigned(LongLong value) { - ULongLong abs_value = static_cast(value); + void FormatSigned(long_long value) { + ulong_long abs_value = static_cast(value); bool negative = value < 0; if (negative) abs_value = 0 - abs_value; @@ -3080,10 +3067,10 @@ class FormatInt { public: explicit FormatInt(int value) { FormatSigned(value); } explicit FormatInt(long value) { FormatSigned(value); } - explicit FormatInt(LongLong value) { FormatSigned(value); } + explicit FormatInt(long_long value) { FormatSigned(value); } explicit FormatInt(unsigned value) : str_(format_decimal(value)) {} explicit FormatInt(unsigned long value) : str_(format_decimal(value)) {} - explicit FormatInt(ULongLong value) : str_(format_decimal(value)) {} + explicit FormatInt(ulong_long value) : str_(format_decimal(value)) {} /** Returns the number of characters written to the output buffer. */ std::size_t size() const { @@ -3150,22 +3137,22 @@ inline void format_decimal(char *&buffer, T value) { \endrst */ template -inline internal::NamedArg arg(StringRef name, const T &arg) { +inline internal::NamedArg arg(string_view name, const T &arg) { return internal::NamedArg(name, arg); } template -inline internal::NamedArg arg(WStringRef name, const T &arg) { +inline internal::NamedArg arg(wstring_view name, const T &arg) { return internal::NamedArg(name, arg); } // The following two functions are deleted intentionally to disable // nested named arguments as in ``format("{}", arg("a", arg("b", 42)))``. template -void arg(StringRef, const internal::NamedArg&) +void arg(string_view, const internal::NamedArg&) FMT_DELETED_OR_UNDEFINED; template -void arg(WStringRef, const internal::NamedArg&) +void arg(wstring_view, const internal::NamedArg&) FMT_DELETED_OR_UNDEFINED; } @@ -3269,7 +3256,7 @@ struct IsInteger { struct WidthHandler { template - typename std::enable_if::value, ULongLong>::type + typename std::enable_if::value, ulong_long>::type operator()(T value) { if (is_negative(value)) FMT_THROW(format_error("negative width")); @@ -3277,7 +3264,7 @@ struct WidthHandler { } template - typename std::enable_if::value, ULongLong>::type + typename std::enable_if::value, ulong_long>::type operator()(T value) { FMT_THROW(format_error("width is not integer")); return 0; @@ -3286,7 +3273,7 @@ struct WidthHandler { struct PrecisionHandler { template - typename std::enable_if::value, ULongLong>::type + typename std::enable_if::value, ulong_long>::type operator()(T value) { if (is_negative(value)) FMT_THROW(format_error("negative precision")); @@ -3294,7 +3281,7 @@ struct PrecisionHandler { } template - typename std::enable_if::value, ULongLong>::type + typename std::enable_if::value, ulong_long>::type operator()(T value) { FMT_THROW(format_error("precision is not integer")); return 0; @@ -3305,7 +3292,7 @@ struct PrecisionHandler { template inline typename basic_context::format_arg basic_context::get_arg( - BasicStringRef name, const char *&error) { + basic_string_view name, const char *&error) { if (this->check_no_auto_index(error)) { map_.init(this->args()); if (const format_arg *arg = map_.find(name)) @@ -3336,7 +3323,7 @@ inline typename basic_context::format_arg c = *++s; } while (internal::is_name_start(c) || ('0' <= c && c <= '9')); const char *error = 0; - format_arg arg = get_arg(BasicStringRef(start, s - start), error); + format_arg arg = get_arg(basic_string_view(start, s - start), error); if (error) FMT_THROW(format_error(error)); return arg; @@ -3424,7 +3411,7 @@ void do_format_arg(basic_buffer &buffer, basic_arg arg, auto width_arg = ctx.parse_arg_id(); if (*s++ != '}') FMT_THROW(format_error("invalid format string")); - ULongLong width = visit(internal::WidthHandler(), width_arg); + ulong_long width = visit(internal::WidthHandler(), width_arg); if (width > (std::numeric_limits::max)()) FMT_THROW(format_error("number is too big")); spec.width_ = static_cast(width); @@ -3441,7 +3428,7 @@ void do_format_arg(basic_buffer &buffer, basic_arg arg, auto precision_arg = ctx.parse_arg_id(); if (*s++ != '}') FMT_THROW(format_error("invalid format string")); - ULongLong precision = + ulong_long precision = visit(internal::PrecisionHandler(), precision_arg); if (precision > (std::numeric_limits::max)()) FMT_THROW(format_error("number is too big")); diff --git a/fmt/ostream.h b/fmt/ostream.h index 528ee16a..f99b9a71 100644 --- a/fmt/ostream.h +++ b/fmt/ostream.h @@ -85,7 +85,7 @@ void format_value(basic_buffer &buf, const T &value, basic_context &ctx) { internal::MemoryBuffer buffer; internal::format_value(buffer, value); - BasicStringRef str(buffer.data(), buffer.size()); + basic_string_view str(buffer.data(), buffer.size()); do_format_arg< ArgFormatter >( buf, internal::make_arg< basic_context >(str), ctx); } diff --git a/fmt/posix.cc b/fmt/posix.cc index bd25f61a..260f6341 100644 --- a/fmt/posix.cc +++ b/fmt/posix.cc @@ -124,7 +124,7 @@ void fmt::File::close() { throw SystemError(errno, "cannot close file"); } -fmt::LongLong fmt::File::size() const { +fmt::long_long fmt::File::size() const { #ifdef _WIN32 // Use GetFileSize instead of GetFileSizeEx for the case when _WIN32_WINNT // is less than 0x0500 as is the case with some default MinGW builds. @@ -137,14 +137,14 @@ fmt::LongLong fmt::File::size() const { if (error != NO_ERROR) throw WindowsError(GetLastError(), "cannot get file size"); } - fmt::ULongLong long_size = size_upper; + fmt::ulong_long long_size = size_upper; return (long_size << sizeof(DWORD) * CHAR_BIT) | size_lower; #else typedef struct stat Stat; Stat file_stat = Stat(); if (FMT_POSIX_CALL(fstat(fd_, &file_stat)) == -1) throw SystemError(errno, "cannot get file attributes"); - FMT_STATIC_ASSERT(sizeof(fmt::LongLong) >= sizeof(file_stat.st_size), + FMT_STATIC_ASSERT(sizeof(fmt::long_long) >= sizeof(file_stat.st_size), "return type of File::size is not large enough"); return file_stat.st_size; #endif diff --git a/fmt/posix.h b/fmt/posix.h index afc69584..45cf3c7c 100644 --- a/fmt/posix.h +++ b/fmt/posix.h @@ -274,7 +274,7 @@ class File { // Returns the file size. The size has signed type for consistency with // stat::st_size. - LongLong size() const; + long_long size() const; // Attempts to read count bytes from the file into the specified buffer. std::size_t read(void *buffer, std::size_t count); diff --git a/fmt/printf.h b/fmt/printf.h index 0b77ae34..a13684c5 100644 --- a/fmt/printf.h +++ b/fmt/printf.h @@ -119,7 +119,7 @@ class ArgConverter { // glibc's printf doesn't sign extend arguments of smaller types: // std::printf("%lld", -42); // prints "4294967254" // but we don't have to do the same because it's a UB. - arg_ = internal::make_arg(static_cast(value)); + arg_ = internal::make_arg(static_cast(value)); } else { arg_ = internal::make_arg( static_cast::Type>(value)); @@ -459,7 +459,7 @@ void printf_context::format(basic_buffer &buffer) { break; case 'l': if (*s == 'l') - convert_arg(arg, *++s); + convert_arg(arg, *++s); else convert_arg(arg, *s); break; diff --git a/test/format-impl-test.cc b/test/format-impl-test.cc index 6c86a484..9102db03 100644 --- a/test/format-impl-test.cc +++ b/test/format-impl-test.cc @@ -55,11 +55,11 @@ struct ValueExtractor { }; TEST(FormatTest, ArgConverter) { - fmt::LongLong value = std::numeric_limits::max(); + fmt::long_long value = std::numeric_limits::max(); auto arg = fmt::internal::make_arg(value); visit(fmt::internal::ArgConverter< - fmt::LongLong, fmt::context>(arg, 'd'), arg); - EXPECT_EQ(value, visit(ValueExtractor(), arg)); + fmt::long_long, fmt::context>(arg, 'd'), arg); + EXPECT_EQ(value, visit(ValueExtractor(), arg)); } TEST(FormatTest, FormatNegativeNaN) { diff --git a/test/format-test.cc b/test/format-test.cc index 56ea7136..bf2b153e 100644 --- a/test/format-test.cc +++ b/test/format-test.cc @@ -73,7 +73,7 @@ using std::size_t; using fmt::basic_writer; using fmt::format; using fmt::format_error; -using fmt::StringRef; +using fmt::string_view; using fmt::CStringRef; using fmt::MemoryWriter; using fmt::WMemoryWriter; @@ -148,16 +148,16 @@ struct WriteChecker { EXPECT_PRED_FORMAT1(WriteChecker(), value) } // namespace -TEST(StringRefTest, Ctor) { - EXPECT_STREQ("abc", StringRef("abc").data()); - EXPECT_EQ(3u, StringRef("abc").size()); +TEST(StringViewTest, Ctor) { + EXPECT_STREQ("abc", string_view("abc").data()); + EXPECT_EQ(3u, string_view("abc").size()); - EXPECT_STREQ("defg", StringRef(std::string("defg")).data()); - EXPECT_EQ(4u, StringRef(std::string("defg")).size()); + EXPECT_STREQ("defg", string_view(std::string("defg")).data()); + EXPECT_EQ(4u, string_view(std::string("defg")).size()); } -TEST(StringRefTest, ConvertToString) { - std::string s = StringRef("abc").to_string(); +TEST(StringViewTest, ConvertToString) { + std::string s = string_view("abc").to_string(); EXPECT_EQ("abc", s); } @@ -1320,7 +1320,7 @@ TEST(FormatterTest, FormatString) { } TEST(FormatterTest, FormatStringRef) { - EXPECT_EQ("test", format("{0}", StringRef("test"))); + EXPECT_EQ("test", format("{0}", string_view("test"))); } TEST(FormatterTest, FormatCStringRef) { diff --git a/test/gtest-extra.cc b/test/gtest-extra.cc index 5708eef7..325216d8 100644 --- a/test/gtest-extra.cc +++ b/test/gtest-extra.cc @@ -103,7 +103,7 @@ std::string read(File &f, std::size_t count) { #endif // FMT_USE_FILE_DESCRIPTORS -std::string format_system_error(int error_code, fmt::StringRef message) { +std::string format_system_error(int error_code, fmt::string_view message) { fmt::internal::MemoryBuffer out; fmt::format_system_error(out, error_code, message); return to_string(out); diff --git a/test/gtest-extra.h b/test/gtest-extra.h index 5f7fe29d..0f38dd31 100644 --- a/test/gtest-extra.h +++ b/test/gtest-extra.h @@ -81,7 +81,7 @@ FMT_TEST_THROW_(statement, expected_exception, \ expected_message, GTEST_NONFATAL_FAILURE_) -std::string format_system_error(int error_code, fmt::StringRef message); +std::string format_system_error(int error_code, fmt::string_view message); #define EXPECT_SYSTEM_ERROR(statement, error_code, message) \ EXPECT_THROW_MSG(statement, fmt::SystemError, \ diff --git a/test/posix-mock-test.cc b/test/posix-mock-test.cc index a87f8633..6b7104fc 100644 --- a/test/posix-mock-test.cc +++ b/test/posix-mock-test.cc @@ -213,7 +213,7 @@ int (test::fileno)(FILE *stream) { # define EXPECT_EQ_POSIX(expected, actual) #endif -void write_file(fmt::CStringRef filename, fmt::StringRef content) { +void write_file(fmt::CStringRef filename, fmt::string_view content) { fmt::BufferedFile f(filename, "w"); f.print("{}", content); } @@ -277,7 +277,7 @@ TEST(FileTest, Size) { write_file("test", content); File f("test", File::RDONLY); EXPECT_GE(f.size(), 0); - EXPECT_EQ(content.size(), static_cast(f.size())); + EXPECT_EQ(content.size(), static_cast(f.size())); #ifdef _WIN32 fmt::internal::MemoryBuffer message; fmt::internal::format_windows_error( diff --git a/test/posix-test.cc b/test/posix-test.cc index e6332bf0..c4b6f4dd 100644 --- a/test/posix-test.cc +++ b/test/posix-test.cc @@ -65,7 +65,7 @@ File open_file() { } // Attempts to write a string to a file. -void write(File &f, fmt::StringRef s) { +void write(File &f, fmt::string_view s) { std::size_t num_chars_left = s.size(); const char *ptr = s.data(); do { diff --git a/test/printf-test.cc b/test/printf-test.cc index e8df2181..65bcca72 100644 --- a/test/printf-test.cc +++ b/test/printf-test.cc @@ -40,7 +40,7 @@ using fmt::format_error; const unsigned BIG_NUM = INT_MAX + 1u; // Makes format string argument positional. -std::string make_positional(fmt::StringRef format) { +std::string make_positional(fmt::string_view format) { std::string s(format.to_string()); s.replace(s.find('%'), 1, "%1$"); return s; @@ -269,8 +269,8 @@ TEST(PrintfTest, DynamicPrecision) { "argument index out of range"); EXPECT_THROW_MSG(fmt::sprintf("%.*d", BIG_NUM, 42), format_error, "number is too big"); - if (sizeof(fmt::LongLong) != sizeof(int)) { - fmt::LongLong prec = static_cast(INT_MIN) - 1; + if (sizeof(fmt::long_long) != sizeof(int)) { + fmt::long_long prec = static_cast(INT_MIN) - 1; EXPECT_THROW_MSG(fmt::sprintf("%.*d", prec, 42), format_error, "number is too big"); } @@ -288,16 +288,16 @@ SPECIALIZE_MAKE_SIGNED(unsigned char, signed char); SPECIALIZE_MAKE_SIGNED(unsigned short, short); SPECIALIZE_MAKE_SIGNED(unsigned, int); SPECIALIZE_MAKE_SIGNED(unsigned long, long); -SPECIALIZE_MAKE_SIGNED(fmt::ULongLong, fmt::LongLong); +SPECIALIZE_MAKE_SIGNED(fmt::ulong_long, fmt::long_long); // Test length format specifier ``length_spec``. template void TestLength(const char *length_spec, U value) { - fmt::LongLong signed_value = 0; - fmt::ULongLong unsigned_value = 0; + fmt::long_long signed_value = 0; + fmt::ulong_long unsigned_value = 0; // Apply integer promotion to the argument. using std::numeric_limits; - fmt::ULongLong max = numeric_limits::max(); + fmt::ulong_long max = numeric_limits::max(); using fmt::internal::const_check; if (const_check(max <= static_cast(numeric_limits::max()))) { signed_value = static_cast(value); @@ -308,7 +308,7 @@ void TestLength(const char *length_spec, U value) { } using fmt::internal::MakeUnsigned; if (sizeof(U) <= sizeof(int) && sizeof(int) < sizeof(T)) { - signed_value = static_cast(value); + signed_value = static_cast(value); unsigned_value = static_cast::Type>(value); } else { signed_value = static_cast::Type>(value); @@ -339,20 +339,20 @@ void TestLength(const char *length_spec) { TestLength(length_spec, -42); TestLength(length_spec, min); TestLength(length_spec, max); - TestLength(length_spec, fmt::LongLong(min) - 1); - fmt::ULongLong long_long_max = std::numeric_limits::max(); - if (static_cast(max) < long_long_max) - TestLength(length_spec, fmt::LongLong(max) + 1); + TestLength(length_spec, fmt::long_long(min) - 1); + fmt::ulong_long long_long_max = std::numeric_limits::max(); + if (static_cast(max) < long_long_max) + TestLength(length_spec, fmt::long_long(max) + 1); TestLength(length_spec, std::numeric_limits::min()); TestLength(length_spec, std::numeric_limits::max()); TestLength(length_spec, std::numeric_limits::min()); TestLength(length_spec, std::numeric_limits::max()); TestLength(length_spec, std::numeric_limits::min()); TestLength(length_spec, std::numeric_limits::max()); - TestLength(length_spec, std::numeric_limits::min()); - TestLength(length_spec, std::numeric_limits::max()); - TestLength(length_spec, std::numeric_limits::min()); - TestLength(length_spec, std::numeric_limits::max()); + TestLength(length_spec, std::numeric_limits::min()); + TestLength(length_spec, std::numeric_limits::max()); + TestLength(length_spec, std::numeric_limits::min()); + TestLength(length_spec, std::numeric_limits::max()); } TEST(PrintfTest, Length) { @@ -363,8 +363,8 @@ TEST(PrintfTest, Length) { TestLength("h"); TestLength("l"); TestLength("l"); - TestLength("ll"); - TestLength("ll"); + TestLength("ll"); + TestLength("ll"); TestLength("j"); TestLength("z"); TestLength("t"); @@ -388,10 +388,10 @@ TEST(PrintfTest, Int) { EXPECT_PRINTF(fmt::format("{:X}", u), "%X", -42); } -TEST(PrintfTest, LongLong) { +TEST(PrintfTest, long_long) { // fmt::printf allows passing long long arguments to %d without length // specifiers. - fmt::LongLong max = std::numeric_limits::max(); + fmt::long_long max = std::numeric_limits::max(); EXPECT_PRINTF(fmt::format("{}", max), "%d", max); } diff --git a/test/util-test.cc b/test/util-test.cc index 370a704f..5978d914 100644 --- a/test/util-test.cc +++ b/test/util-test.cc @@ -54,7 +54,7 @@ using fmt::basic_arg; using fmt::basic_buffer; -using fmt::StringRef; +using fmt::string_view; using fmt::internal::MemoryBuffer; using fmt::internal::value; @@ -488,8 +488,8 @@ VISIT_TYPE(unsigned short, unsigned); VISIT_TYPE(long, int); VISIT_TYPE(unsigned long, unsigned); #else -VISIT_TYPE(long, fmt::LongLong); -VISIT_TYPE(unsigned long, fmt::ULongLong); +VISIT_TYPE(long, fmt::long_long); +VISIT_TYPE(unsigned long, fmt::ulong_long); #endif VISIT_TYPE(float, double); @@ -511,7 +511,7 @@ class NumericArgTest : public testing::Test {}; typedef ::testing::Types< bool, signed char, unsigned char, signed, unsigned short, - int, unsigned, long, unsigned long, fmt::LongLong, fmt::ULongLong, + int, unsigned, long, unsigned long, fmt::long_long, fmt::ulong_long, float, double, long double> Types; TYPED_TEST_CASE(NumericArgTest, Types); @@ -546,7 +546,7 @@ TEST(UtilTest, StringArg) { CHECK_ARG_(wchar_t, cstr, str); CHECK_ARG(cstr); - StringRef sref(str); + string_view sref(str); CHECK_ARG_(char, sref, std::string(str)); CHECK_ARG_(wchar_t, sref, std::string(str)); CHECK_ARG(sref); @@ -557,11 +557,11 @@ TEST(UtilTest, WStringArg) { wchar_t *str = str_data; const wchar_t *cstr = str; - fmt::WStringRef sref(str); + fmt::wstring_view sref(str); CHECK_ARG_(wchar_t, sref, str); CHECK_ARG_(wchar_t, sref, cstr); CHECK_ARG_(wchar_t, sref, std::wstring(str)); - CHECK_ARG_(wchar_t, sref, fmt::WStringRef(str)); + CHECK_ARG_(wchar_t, sref, fmt::wstring_view(str)); } TEST(UtilTest, PointerArg) { @@ -612,7 +612,7 @@ void test_count_digits() { TEST(UtilTest, StringRef) { // Test that StringRef::size() returns string length, not buffer size. char str[100] = "some string"; - EXPECT_EQ(std::strlen(str), StringRef(str).size()); + EXPECT_EQ(std::strlen(str), string_view(str).size()); EXPECT_LT(std::strlen(str), sizeof(str)); } @@ -623,18 +623,18 @@ void CheckOp() { std::size_t num_inputs = sizeof(inputs) / sizeof(*inputs); for (std::size_t i = 0; i < num_inputs; ++i) { for (std::size_t j = 0; j < num_inputs; ++j) { - StringRef lhs(inputs[i]), rhs(inputs[j]); - EXPECT_EQ(Op()(lhs.compare(rhs), 0), Op()(lhs, rhs)); + string_view lhs(inputs[i]), rhs(inputs[j]); + EXPECT_EQ(Op()(lhs.compare(rhs), 0), Op()(lhs, rhs)); } } } TEST(UtilTest, StringRefCompare) { - EXPECT_EQ(0, StringRef("foo").compare(StringRef("foo"))); - EXPECT_GT(StringRef("fop").compare(StringRef("foo")), 0); - EXPECT_LT(StringRef("foo").compare(StringRef("fop")), 0); - EXPECT_GT(StringRef("foo").compare(StringRef("fo")), 0); - EXPECT_LT(StringRef("fo").compare(StringRef("foo")), 0); + EXPECT_EQ(0, string_view("foo").compare(string_view("foo"))); + EXPECT_GT(string_view("fop").compare(string_view("foo")), 0); + EXPECT_LT(string_view("foo").compare(string_view("fop")), 0); + EXPECT_GT(string_view("foo").compare(string_view("fo")), 0); + EXPECT_LT(string_view("fo").compare(string_view("foo")), 0); CheckOp(); CheckOp(); CheckOp(); @@ -666,7 +666,7 @@ TEST(UtilTest, UTF8ToUTF16) { template void check_utf_conversion_error( const char *message, - fmt::BasicStringRef str = fmt::BasicStringRef(0, 0)) { + fmt::basic_string_view str = fmt::basic_string_view(0, 0)) { fmt::internal::MemoryBuffer out; fmt::internal::format_windows_error(out, ERROR_INVALID_PARAMETER, message); fmt::SystemError error(0, ""); @@ -688,19 +688,19 @@ TEST(UtilTest, UTF8ToUTF16Error) { const char *message = "cannot convert string from UTF-8 to UTF-16"; check_utf_conversion_error(message); check_utf_conversion_error( - message, fmt::StringRef("foo", INT_MAX + 1u)); + message, fmt::string_view("foo", INT_MAX + 1u)); } TEST(UtilTest, UTF16ToUTF8Convert) { fmt::internal::UTF16ToUTF8 u; - EXPECT_EQ(ERROR_INVALID_PARAMETER, u.convert(fmt::WStringRef(0, 0))); + EXPECT_EQ(ERROR_INVALID_PARAMETER, u.convert(fmt::wstring_view(0, 0))); EXPECT_EQ(ERROR_INVALID_PARAMETER, - u.convert(fmt::WStringRef(L"foo", INT_MAX + 1u))); + u.convert(fmt::wstring_view(L"foo", INT_MAX + 1u))); } #endif // _WIN32 typedef void (*FormatErrorMessage)( - fmt::buffer &out, int error_code, StringRef message); + fmt::buffer &out, int error_code, string_view message); template void check_throw_error(int error_code, FormatErrorMessage format) { @@ -723,7 +723,7 @@ TEST(UtilTest, FormatSystemError) { to_string(message)); message.clear(); fmt::format_system_error( - message, EDOM, fmt::StringRef(0, std::numeric_limits::max())); + message, EDOM, fmt::string_view(0, std::numeric_limits::max())); EXPECT_EQ(fmt::format("error {}", EDOM), to_string(message)); } @@ -760,7 +760,7 @@ TEST(UtilTest, FormatWindowsError) { actual_message.clear(); fmt::internal::format_windows_error( actual_message, ERROR_FILE_EXISTS, - fmt::StringRef(0, std::numeric_limits::max())); + fmt::string_view(0, std::numeric_limits::max())); EXPECT_EQ(fmt::format("error {}", ERROR_FILE_EXISTS), fmt::to_string(actual_message)); }