StringRef -> string_view, LongLong -> long_long

This commit is contained in:
Victor Zverovich 2017-02-18 06:52:52 -08:00
parent e022c21ddc
commit 50e716737d
14 changed files with 167 additions and 180 deletions

View File

@ -106,7 +106,7 @@ inline int fmt_snprintf(char *buffer, size_t size, const char *format, ...) {
const char RESET_COLOR[] = "\x1b[0m"; 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. // Portable thread-safe version of strerror.
// Sets buffer to point to a string describing the error code. // 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, 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 // Report error code making sure that the output fits into
// INLINE_BUFFER_SIZE to avoid dynamic memory allocation and potential // INLINE_BUFFER_SIZE to avoid dynamic memory allocation and potential
// bad_alloc. // bad_alloc.
@ -204,7 +204,7 @@ void format_error_code(buffer &out, int error_code,
} }
void report_error(FormatFunc func, int error_code, void report_error(FormatFunc func, int error_code,
StringRef message) FMT_NOEXCEPT { string_view message) FMT_NOEXCEPT {
internal::MemoryBuffer<char> full_message; internal::MemoryBuffer<char> full_message;
func(full_message, error_code, message); func(full_message, error_code, message);
// Use Writer::data instead of Writer::c_str to avoid potential memory // Use Writer::data instead of Writer::c_str to avoid potential memory
@ -279,10 +279,10 @@ template <typename T>
const uint64_t internal::BasicData<T>::POWERS_OF_10_64[] = { const uint64_t internal::BasicData<T>::POWERS_OF_10_64[] = {
0, 0,
FMT_POWERS_OF_10(1), 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 // Multiply several constants instead of using a single long long constant
// to avoid warnings about C++98 not supporting long long. // 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) { 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 #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"; static const char ERROR_MSG[] = "cannot convert string from UTF-8 to UTF-16";
if (s.size() > INT_MAX) if (s.size() > INT_MAX)
FMT_THROW(WindowsError(ERROR_INVALID_PARAMETER, ERROR_MSG)); FMT_THROW(WindowsError(ERROR_INVALID_PARAMETER, ERROR_MSG));
@ -315,14 +315,14 @@ FMT_FUNC internal::UTF8ToUTF16::UTF8ToUTF16(StringRef s) {
buffer_[length] = 0; buffer_[length] = 0;
} }
FMT_FUNC internal::UTF16ToUTF8::UTF16ToUTF8(WStringRef s) { FMT_FUNC internal::UTF16ToUTF8::UTF16ToUTF8(wstring_view s) {
if (int error_code = convert(s)) { if (int error_code = convert(s)) {
FMT_THROW(WindowsError(error_code, FMT_THROW(WindowsError(error_code,
"cannot convert string from UTF-16 to UTF-8")); "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) if (s.size() > INT_MAX)
return ERROR_INVALID_PARAMETER; return ERROR_INVALID_PARAMETER;
int s_size = static_cast<int>(s.size()); int s_size = static_cast<int>(s.size());
@ -348,7 +348,7 @@ FMT_FUNC void WindowsError::init(
} }
FMT_FUNC void internal::format_windows_error( 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 { FMT_TRY {
MemoryBuffer<wchar_t> buffer; MemoryBuffer<wchar_t> buffer;
buffer.resize(INLINE_BUFFER_SIZE); buffer.resize(INLINE_BUFFER_SIZE);
@ -380,7 +380,7 @@ FMT_FUNC void internal::format_windows_error(
#endif // FMT_USE_WINDOWS_H #endif // FMT_USE_WINDOWS_H
FMT_FUNC void format_system_error( 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 { FMT_TRY {
internal::MemoryBuffer<char, internal::INLINE_BUFFER_SIZE> buffer; internal::MemoryBuffer<char, internal::INLINE_BUFFER_SIZE> buffer;
buffer.resize(internal::INLINE_BUFFER_SIZE); buffer.resize(internal::INLINE_BUFFER_SIZE);
@ -408,14 +408,14 @@ void FixedBuffer<Char>::grow(std::size_t) {
} }
FMT_FUNC void report_system_error( 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. // 'fmt::' is for bcc32.
report_error(format_system_error, error_code, message); report_error(format_system_error, error_code, message);
} }
#if FMT_USE_WINDOWS_H #if FMT_USE_WINDOWS_H
FMT_FUNC void report_windows_error( 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. // 'fmt::' is for bcc32.
report_error(internal::format_windows_error, error_code, message); report_error(internal::format_windows_error, error_code, message);
} }

View File

@ -82,15 +82,19 @@ typedef __int64 intmax_t;
# define FMT_GCC_EXTENSION __extension__ # define FMT_GCC_EXTENSION __extension__
# if FMT_GCC_VERSION >= 406 # if FMT_GCC_VERSION >= 406
# pragma GCC diagnostic push # pragma GCC diagnostic push
// Disable the warning about "long long" which is sometimes reported even // Disable the warning about "long long" which is sometimes reported even
// when using __extension__. // when using __extension__.
# pragma GCC diagnostic ignored "-Wlong-long" # pragma GCC diagnostic ignored "-Wlong-long"
// Disable the warning about declaration shadowing because it affects too // Disable the warning about declaration shadowing because it affects too
// many valid cases. // many valid cases.
# pragma GCC diagnostic ignored "-Wshadow" # pragma GCC diagnostic ignored "-Wshadow"
// Disable the warning about implicit conversions that may change the sign of // Disable the warning about implicit conversions that may change the sign of
// an integer; silencing it otherwise would require many explicit casts. // an integer; silencing it otherwise would require many explicit casts.
# pragma GCC diagnostic ignored "-Wsign-conversion" # pragma GCC diagnostic ignored "-Wsign-conversion"
# endif # endif
# if __cplusplus >= 201103L || defined __GXX_EXPERIMENTAL_CXX0X__ # if __cplusplus >= 201103L || defined __GXX_EXPERIMENTAL_CXX0X__
# define FMT_HAS_GXX_CXX11 1 # define FMT_HAS_GXX_CXX11 1
@ -357,8 +361,8 @@ namespace fmt {
// Fix the warning about long long on older versions of GCC // Fix the warning about long long on older versions of GCC
// that don't support the diagnostic pragma. // that don't support the diagnostic pragma.
FMT_GCC_EXTENSION typedef long long LongLong; FMT_GCC_EXTENSION typedef long long long_long;
FMT_GCC_EXTENSION typedef unsigned long long ULongLong; FMT_GCC_EXTENSION typedef unsigned long long ulong_long;
#if FMT_USE_RVALUE_REFERENCES #if FMT_USE_RVALUE_REFERENCES
using std::move; using std::move;
@ -390,39 +394,21 @@ typedef basic_context<wchar_t> wcontext;
/** /**
\rst \rst
A string reference. It can be constructed from a C string or ``std::string``. An implementation of ``std::basic_string_view`` for pre-C++17. It provides a
subset of the API.
You can use one of the following typedefs for common character types:
+------------+-------------------------+
| Type | Definition |
+============+=========================+
| StringRef | BasicStringRef<char> |
+------------+-------------------------+
| WStringRef | BasicStringRef<wchar_t> |
+------------+-------------------------+
This class is most useful as a parameter type to allow passing
different types of strings to a function, for example::
template <typename... Args>
std::string format(StringRef format_str, const Args & ... args);
format("{}", 42);
format(std::string("{}"), 42);
\endrst \endrst
*/ */
template <typename Char> template <typename Char>
class BasicStringRef { class basic_string_view {
private: private:
const Char *data_; const Char *data_;
std::size_t size_; std::size_t size_;
public: 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. */ /** 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 \rst
@ -430,7 +416,7 @@ class BasicStringRef {
the size with ``std::char_traits<Char>::length``. the size with ``std::char_traits<Char>::length``.
\endrst \endrst
*/ */
BasicStringRef(const Char *s) basic_string_view(const Char *s)
: data_(s), size_(std::char_traits<Char>::length(s)) {} : data_(s), size_(std::char_traits<Char>::length(s)) {}
/** /**
@ -438,7 +424,7 @@ class BasicStringRef {
Constructs a string reference from an ``std::string`` object. Constructs a string reference from an ``std::string`` object.
\endrst \endrst
*/ */
BasicStringRef(const std::basic_string<Char> &s) basic_string_view(const std::basic_string<Char> &s)
: data_(s.c_str()), size_(s.size()) {} : data_(s.c_str()), size_(s.size()) {}
/** /**
@ -457,7 +443,7 @@ class BasicStringRef {
std::size_t size() const { return size_; } std::size_t size() const { return size_; }
// Lexicographically compare this string reference to other. // 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_; std::size_t size = size_ < other.size_ ? size_ : other.size_;
int result = std::char_traits<Char>::compare(data_, other.data_, size); int result = std::char_traits<Char>::compare(data_, other.data_, size);
if (result == 0) if (result == 0)
@ -465,28 +451,28 @@ class BasicStringRef {
return result; 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; 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; 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; 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; 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; 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; return lhs.compare(rhs) >= 0;
} }
}; };
typedef BasicStringRef<char> StringRef; typedef basic_string_view<char> string_view;
typedef BasicStringRef<wchar_t> WStringRef; typedef basic_string_view<wchar_t> wstring_view;
/** /**
\rst \rst
@ -559,7 +545,7 @@ FMT_SPECIALIZE_MAKE_UNSIGNED(signed char, unsigned char);
FMT_SPECIALIZE_MAKE_UNSIGNED(short, unsigned short); FMT_SPECIALIZE_MAKE_UNSIGNED(short, unsigned short);
FMT_SPECIALIZE_MAKE_UNSIGNED(int, unsigned); FMT_SPECIALIZE_MAKE_UNSIGNED(int, unsigned);
FMT_SPECIALIZE_MAKE_UNSIGNED(long, unsigned long); 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. // Casts nonnegative integer to unsigned.
template <typename Int> template <typename Int>
@ -895,13 +881,13 @@ struct NoThousandsSep {
// A functor that adds a thousands separator. // A functor that adds a thousands separator.
class ThousandsSep { class ThousandsSep {
private: private:
fmt::StringRef sep_; fmt::string_view sep_;
// Index of a decimal digit with the least significant digit having index 0. // Index of a decimal digit with the least significant digit having index 0.
unsigned digit_index_; unsigned digit_index_;
public: public:
explicit ThousandsSep(fmt::StringRef sep) : sep_(sep), digit_index_(0) {} explicit ThousandsSep(fmt::string_view sep) : sep_(sep), digit_index_(0) {}
template <typename Char> template <typename Char>
void operator()(Char *&buffer) { void operator()(Char *&buffer) {
@ -962,8 +948,8 @@ class UTF8ToUTF16 {
MemoryBuffer<wchar_t, INLINE_BUFFER_SIZE> buffer_; MemoryBuffer<wchar_t, INLINE_BUFFER_SIZE> buffer_;
public: public:
FMT_API explicit UTF8ToUTF16(StringRef s); FMT_API explicit UTF8ToUTF16(string_view s);
operator WStringRef() const { return WStringRef(&buffer_[0], size()); } operator wstring_view() const { return wstring_view(&buffer_[0], size()); }
size_t size() const { return buffer_.size() - 1; } size_t size() const { return buffer_.size() - 1; }
const wchar_t *c_str() const { return &buffer_[0]; } const wchar_t *c_str() const { return &buffer_[0]; }
std::wstring str() const { return std::wstring(&buffer_[0], size()); } std::wstring str() const { return std::wstring(&buffer_[0], size()); }
@ -977,8 +963,8 @@ class UTF16ToUTF8 {
public: public:
UTF16ToUTF8() {} UTF16ToUTF8() {}
FMT_API explicit UTF16ToUTF8(WStringRef s); FMT_API explicit UTF16ToUTF8(wstring_view s);
operator StringRef() const { return StringRef(&buffer_[0], size()); } operator string_view() const { return string_view(&buffer_[0], size()); }
size_t size() const { return buffer_.size() - 1; } size_t size() const { return buffer_.size() - 1; }
const char *c_str() const { return &buffer_[0]; } const char *c_str() const { return &buffer_[0]; }
std::string str() const { return std::string(&buffer_[0], size()); } 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 // Performs conversion returning a system error code instead of
// throwing exception on conversion error. This method may still throw // throwing exception on conversion error. This method may still throw
// in case of memory allocation error. // 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_API void format_windows_error(fmt::buffer &out, int error_code,
fmt::StringRef message) FMT_NOEXCEPT; fmt::string_view message) FMT_NOEXCEPT;
#endif #endif
template<bool B, class T = void> template<bool B, class T = void>
@ -1032,7 +1018,7 @@ template <typename T>
T &get(); T &get();
// These are non-members to workaround an overload resolution bug in bcc32. // These are non-members to workaround an overload resolution bug in bcc32.
Yes &convert(fmt::ULongLong); Yes &convert(fmt::ulong_long);
No &convert(...); No &convert(...);
template<typename T, bool ENABLE_CONVERSION> template<typename T, bool ENABLE_CONVERSION>
@ -1119,8 +1105,8 @@ template <> constexpr Type gettype<unsigned long>() {
return sizeof(unsigned long) == sizeof(unsigned) ? return sizeof(unsigned long) == sizeof(unsigned) ?
UINT : ULONG_LONG; UINT : ULONG_LONG;
} }
template <> constexpr Type gettype<LongLong>() { return LONG_LONG; } template <> constexpr Type gettype<long_long>() { return LONG_LONG; }
template <> constexpr Type gettype<ULongLong>() { return ULONG_LONG; } template <> constexpr Type gettype<ulong_long>() { return ULONG_LONG; }
template <> constexpr Type gettype<float>() { return DOUBLE; } template <> constexpr Type gettype<float>() { return DOUBLE; }
template <> constexpr Type gettype<double>() { return DOUBLE; } template <> constexpr Type gettype<double>() { return DOUBLE; }
template <> constexpr Type gettype<long double>() { return LONG_DOUBLE; } template <> constexpr Type gettype<long double>() { return LONG_DOUBLE; }
@ -1139,12 +1125,12 @@ template <> constexpr Type gettype<const signed char *>() { return CSTRING; }
template <> constexpr Type gettype<unsigned char *>() { return CSTRING; } template <> constexpr Type gettype<unsigned char *>() { return CSTRING; }
template <> constexpr Type gettype<const unsigned char *>() { return CSTRING; } template <> constexpr Type gettype<const unsigned char *>() { return CSTRING; }
template <> constexpr Type gettype<std::string>() { return STRING; } template <> constexpr Type gettype<std::string>() { return STRING; }
template <> constexpr Type gettype<StringRef>() { return STRING; } template <> constexpr Type gettype<string_view>() { return STRING; }
template <> constexpr Type gettype<CStringRef>() { return CSTRING; } template <> constexpr Type gettype<CStringRef>() { return CSTRING; }
template <> constexpr Type gettype<wchar_t *>() { return TSTRING; } template <> constexpr Type gettype<wchar_t *>() { return TSTRING; }
template <> constexpr Type gettype<const wchar_t *>() { return TSTRING; } template <> constexpr Type gettype<const wchar_t *>() { return TSTRING; }
template <> constexpr Type gettype<std::wstring>() { return TSTRING; } template <> constexpr Type gettype<std::wstring>() { return TSTRING; }
template <> constexpr Type gettype<WStringRef>() { return TSTRING; } template <> constexpr Type gettype<wstring_view>() { return TSTRING; }
template <> constexpr Type gettype<void *>() { return POINTER; } template <> constexpr Type gettype<void *>() { return POINTER; }
template <> constexpr Type gettype<const void *>() { return POINTER; } template <> constexpr Type gettype<const void *>() { return POINTER; }
@ -1158,8 +1144,8 @@ class value {
union { union {
int int_value; int int_value;
unsigned uint_value; unsigned uint_value;
LongLong long_long_value; long_long long_long_value;
ULongLong ulong_long_value; ulong_long ulong_long_value;
double double_value; double double_value;
long double long_double_value; long double long_double_value;
const void *pointer; const void *pointer;
@ -1193,14 +1179,14 @@ class value {
value(typename WCharHelper<wchar_t *, Char>::Unsupported); value(typename WCharHelper<wchar_t *, Char>::Unsupported);
value(typename WCharHelper<const wchar_t *, Char>::Unsupported); value(typename WCharHelper<const wchar_t *, Char>::Unsupported);
value(typename WCharHelper<const std::wstring &, Char>::Unsupported); value(typename WCharHelper<const std::wstring &, Char>::Unsupported);
value(typename WCharHelper<WStringRef, Char>::Unsupported); value(typename WCharHelper<wstring_view, Char>::Unsupported);
void set_string(StringRef str) { void set_string(string_view str) {
this->string.value = str.data(); this->string.value = str.data();
this->string.size = str.size(); this->string.size = str.size();
} }
void set_string(WStringRef str) { void set_string(wstring_view str) {
this->tstring.value = str.data(); this->tstring.value = str.data();
this->tstring.size = str.size(); this->tstring.size = str.size();
} }
@ -1247,8 +1233,8 @@ class value {
this->ulong_long_value = value; this->ulong_long_value = value;
} }
FMT_MAKE_VALUE(LongLong, long_long_value, LONG_LONG) FMT_MAKE_VALUE(long_long, long_long_value, LONG_LONG)
FMT_MAKE_VALUE(ULongLong, ulong_long_value, ULONG_LONG) FMT_MAKE_VALUE(ulong_long, ulong_long_value, ULONG_LONG)
FMT_MAKE_VALUE(float, double_value, DOUBLE) FMT_MAKE_VALUE(float, double_value, DOUBLE)
FMT_MAKE_VALUE(double, double_value, DOUBLE) FMT_MAKE_VALUE(double, double_value, DOUBLE)
FMT_MAKE_VALUE(long double, long_double_value, LONG_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(unsigned char *, ustring.value, CSTRING)
FMT_MAKE_VALUE(const 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(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()) FMT_MAKE_VALUE_(CStringRef, string.value, CSTRING, value.c_str())
#define FMT_MAKE_WSTR_VALUE(Type, TYPE) \ #define FMT_MAKE_WSTR_VALUE(Type, TYPE) \
@ -1289,7 +1275,7 @@ class value {
FMT_MAKE_WSTR_VALUE(wchar_t *, TSTRING) FMT_MAKE_WSTR_VALUE(wchar_t *, TSTRING)
FMT_MAKE_WSTR_VALUE(const wchar_t *, TSTRING) FMT_MAKE_WSTR_VALUE(const wchar_t *, TSTRING)
FMT_MAKE_WSTR_VALUE(const std::wstring &, 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(void *, pointer, POINTER)
FMT_MAKE_VALUE(const void *, pointer, POINTER) FMT_MAKE_VALUE(const void *, pointer, POINTER)
@ -1406,9 +1392,9 @@ typename std::result_of<Visitor(int)>::type
case internal::CSTRING: case internal::CSTRING:
return vis(arg.value_.string.value); return vis(arg.value_.string.value);
case internal::STRING: 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: case internal::TSTRING:
return vis(BasicStringRef<Char>( return vis(basic_string_view<Char>(
arg.value_.tstring.value, arg.value_.tstring.size)); arg.value_.tstring.value, arg.value_.tstring.size));
case internal::POINTER: case internal::POINTER:
return vis(arg.value_.pointer); return vis(arg.value_.pointer);
@ -1436,12 +1422,12 @@ template <typename T, T> struct LConvCheck {
// We check if ``lconv`` contains ``thousands_sep`` because on Android // We check if ``lconv`` contains ``thousands_sep`` because on Android
// ``lconv`` is stubbed as an empty struct. // ``lconv`` is stubbed as an empty struct.
template <typename LConv> template <typename LConv>
inline StringRef thousands_sep( inline string_view thousands_sep(
LConv *lc, LConvCheck<char *LConv::*, &LConv::thousands_sep> = 0) { LConv *lc, LConvCheck<char *LConv::*, &LConv::thousands_sep> = 0) {
return lc->thousands_sep; return lc->thousands_sep;
} }
inline fmt::StringRef thousands_sep(...) { return ""; } inline fmt::string_view thousands_sep(...) { return ""; }
#define FMT_CONCAT(a, b) a##b #define FMT_CONCAT(a, b) a##b
@ -1476,10 +1462,10 @@ template <typename Context>
struct NamedArg : basic_arg<Context> { struct NamedArg : basic_arg<Context> {
typedef typename Context::char_type Char; typedef typename Context::char_type Char;
BasicStringRef<Char> name; basic_string_view<Char> name;
template <typename T> template <typename T>
NamedArg(BasicStringRef<Char> argname, const T &value) NamedArg(basic_string_view<Char> argname, const T &value)
: basic_arg<Context>(make_arg<Context>(value)), name(argname) {} : basic_arg<Context>(make_arg<Context>(value)), name(argname) {}
}; };
@ -1788,7 +1774,7 @@ class ArgMap {
private: private:
typedef typename Context::char_type Char; typedef typename Context::char_type Char;
typedef std::vector< typedef std::vector<
std::pair<fmt::BasicStringRef<Char>, basic_arg<Context> > > MapType; std::pair<fmt::basic_string_view<Char>, basic_arg<Context> > > MapType;
typedef typename MapType::value_type Pair; typedef typename MapType::value_type Pair;
MapType map_; MapType map_;
@ -1797,7 +1783,7 @@ class ArgMap {
void init(const basic_args<Context> &args); void init(const basic_args<Context> &args);
const basic_arg<Context> const basic_arg<Context>
*find(const fmt::BasicStringRef<Char> &name) const { *find(const fmt::basic_string_view<Char> &name) const {
// The list is unsorted, so just return the first matching name. // The list is unsorted, so just return the first matching name.
for (typename MapType::const_iterator it = map_.begin(), end = map_.end(); for (typename MapType::const_iterator it = map_.begin(), end = map_.end();
it != end; ++it) { it != end; ++it) {
@ -1871,7 +1857,7 @@ class ArgFormatterBase {
} }
template <typename StrChar> template <typename StrChar>
void write_str(BasicStringRef<StrChar> value, void write_str(basic_string_view<StrChar> value,
typename EnableIf< typename EnableIf<
std::is_same<Char, wchar_t>::value && std::is_same<Char, wchar_t>::value &&
std::is_same<StrChar, wchar_t>::value, int>::type = 0) { std::is_same<StrChar, wchar_t>::value, int>::type = 0) {
@ -1879,7 +1865,7 @@ class ArgFormatterBase {
} }
template <typename StrChar> template <typename StrChar>
void write_str(BasicStringRef<StrChar> value, void write_str(basic_string_view<StrChar> value,
typename EnableIf< typename EnableIf<
!std::is_same<Char, wchar_t>::value || !std::is_same<Char, wchar_t>::value ||
!std::is_same<StrChar, wchar_t>::value, int>::type = 0) { !std::is_same<StrChar, wchar_t>::value, int>::type = 0) {
@ -1891,12 +1877,12 @@ class ArgFormatterBase {
format_specs &spec() { return spec_; } format_specs &spec() { return spec_; }
void write(bool value) { 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) { void write(const char *value) {
writer_.write_str( writer_.write_str(
StringRef(value, value != 0 ? std::strlen(value) : 0), spec_); string_view(value, value != 0 ? std::strlen(value) : 0), spec_);
} }
public: public:
@ -1959,11 +1945,11 @@ class ArgFormatterBase {
write(value); write(value);
} }
void operator()(StringRef value) { void operator()(string_view value) {
writer_.write_str(value, spec_); writer_.write_str(value, spec_);
} }
void operator()(BasicStringRef<wchar_t> value) { void operator()(basic_string_view<wchar_t> value) {
write_str(value); write_str(value);
} }
@ -2078,7 +2064,7 @@ class basic_context :
// Checks if manual indexing is used and returns the argument with // Checks if manual indexing is used and returns the argument with
// specified name. // specified name.
format_arg get_arg(BasicStringRef<Char> name, const char *&error); format_arg get_arg(basic_string_view<Char> name, const char *&error);
public: public:
/** /**
@ -2156,7 +2142,7 @@ class SystemError : public internal::RuntimeError {
\endrst \endrst
*/ */
FMT_API void format_system_error(fmt::buffer &out, int error_code, 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 { namespace internal {
// Named format specifier. // Named format specifier.
@ -2268,7 +2254,7 @@ class basic_writer {
CharPtr write_str(const StrChar *s, std::size_t size, const AlignSpec &spec); CharPtr write_str(const StrChar *s, std::size_t size, const AlignSpec &spec);
template <typename StrChar> template <typename StrChar>
void write_str(BasicStringRef<StrChar> str, const format_specs &spec); void write_str(basic_string_view<StrChar> str, const format_specs &spec);
// This following methods are private to disallow writing wide characters // 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 // 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) { void write(long value) {
write_decimal(value); write_decimal(value);
} }
void write(LongLong value) { void write(long_long value) {
write_decimal(value); write_decimal(value);
} }
@ -2388,18 +2374,19 @@ class basic_writer {
Writes *value* to the buffer. Writes *value* to the buffer.
\endrst \endrst
*/ */
void write(fmt::BasicStringRef<Char> value) { void write(fmt::basic_string_view<Char> value) {
const Char *str = value.data(); const Char *str = value.data();
buffer_.append(str, str + value.size()); buffer_.append(str, str + value.size());
} }
void write(typename internal::WCharHelper<StringRef, Char>::Supported value) { void write(
typename internal::WCharHelper<string_view, Char>::Supported value) {
const char *str = value.data(); const char *str = value.data();
buffer_.append(str, str + value.size()); buffer_.append(str, str + value.size());
} }
template <typename... FormatSpecs> template <typename... FormatSpecs>
void write(BasicStringRef<Char> str, FormatSpecs... specs) { void write(basic_string_view<Char> str, FormatSpecs... specs) {
write_str(str, format_specs(specs...)); write_str(str, format_specs(specs...));
} }
@ -2434,7 +2421,7 @@ typename basic_writer<Char>::CharPtr basic_writer<Char>::write_str(
template <typename Char> template <typename Char>
template <typename StrChar> template <typename StrChar>
void basic_writer<Char>::write_str( void basic_writer<Char>::write_str(
BasicStringRef<StrChar> s, const format_specs &spec) { basic_string_view<StrChar> s, const format_specs &spec) {
// Check if StrChar is convertible to Char. // Check if StrChar is convertible to Char.
internal::CharTraits<Char>::convert(StrChar()); internal::CharTraits<Char>::convert(StrChar());
if (spec.type_ && spec.type_ != 's') if (spec.type_ && spec.type_ != 's')
@ -2606,7 +2593,7 @@ void basic_writer<Char>::write_int(T value, Spec spec) {
} }
case 'n': { case 'n': {
unsigned num_digits = internal::count_digits(abs_value); 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<unsigned>( unsigned size = static_cast<unsigned>(
num_digits + sep.size() * ((num_digits - 1) / 3)); num_digits + sep.size() * ((num_digits - 1) / 3));
CharPtr p = prepare_int_buffer(size, spec, prefix, prefix_size) + 1; CharPtr p = prepare_int_buffer(size, spec, prefix, prefix_size) + 1;
@ -2880,7 +2867,7 @@ class FixedBuffer : public basic_buffer<Char> {
// Reports a system error without throwing an exception. // Reports a system error without throwing an exception.
// Can be used to report errors from destructors. // Can be used to report errors from destructors.
FMT_API void report_system_error(int error_code, FMT_API void report_system_error(int error_code,
StringRef message) FMT_NOEXCEPT; string_view message) FMT_NOEXCEPT;
#if FMT_USE_WINDOWS_H #if FMT_USE_WINDOWS_H
@ -2927,7 +2914,7 @@ class WindowsError : public SystemError {
// Reports a Windows error without throwing an exception. // Reports a Windows error without throwing an exception.
// Can be used to report errors from destructors. // Can be used to report errors from destructors.
FMT_API void report_windows_error(int error_code, FMT_API void report_windows_error(int error_code,
StringRef message) FMT_NOEXCEPT; string_view message) FMT_NOEXCEPT;
#endif #endif
@ -3041,12 +3028,12 @@ class FormatInt {
private: private:
// Buffer should be large enough to hold all digits (digits10 + 1), // Buffer should be large enough to hold all digits (digits10 + 1),
// a sign and a null character. // a sign and a null character.
enum {BUFFER_SIZE = std::numeric_limits<ULongLong>::digits10 + 3}; enum {BUFFER_SIZE = std::numeric_limits<ulong_long>::digits10 + 3};
mutable char buffer_[BUFFER_SIZE]; mutable char buffer_[BUFFER_SIZE];
char *str_; char *str_;
// Formats value in reverse and returns the number of digits. // 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; char *buffer_end = buffer_ + BUFFER_SIZE - 1;
while (value >= 100) { while (value >= 100) {
// Integer division is slow so do it for a group of two digits instead // Integer division is slow so do it for a group of two digits instead
@ -3067,8 +3054,8 @@ class FormatInt {
return buffer_end; return buffer_end;
} }
void FormatSigned(LongLong value) { void FormatSigned(long_long value) {
ULongLong abs_value = static_cast<ULongLong>(value); ulong_long abs_value = static_cast<ulong_long>(value);
bool negative = value < 0; bool negative = value < 0;
if (negative) if (negative)
abs_value = 0 - abs_value; abs_value = 0 - abs_value;
@ -3080,10 +3067,10 @@ class FormatInt {
public: public:
explicit FormatInt(int value) { FormatSigned(value); } explicit FormatInt(int value) { FormatSigned(value); }
explicit FormatInt(long 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 value) : str_(format_decimal(value)) {}
explicit FormatInt(unsigned long 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. */ /** Returns the number of characters written to the output buffer. */
std::size_t size() const { std::size_t size() const {
@ -3150,22 +3137,22 @@ inline void format_decimal(char *&buffer, T value) {
\endrst \endrst
*/ */
template <typename T> template <typename T>
inline internal::NamedArg<context> arg(StringRef name, const T &arg) { inline internal::NamedArg<context> arg(string_view name, const T &arg) {
return internal::NamedArg<context>(name, arg); return internal::NamedArg<context>(name, arg);
} }
template <typename T> template <typename T>
inline internal::NamedArg<wcontext> arg(WStringRef name, const T &arg) { inline internal::NamedArg<wcontext> arg(wstring_view name, const T &arg) {
return internal::NamedArg<wcontext>(name, arg); return internal::NamedArg<wcontext>(name, arg);
} }
// The following two functions are deleted intentionally to disable // The following two functions are deleted intentionally to disable
// nested named arguments as in ``format("{}", arg("a", arg("b", 42)))``. // nested named arguments as in ``format("{}", arg("a", arg("b", 42)))``.
template <typename Context> template <typename Context>
void arg(StringRef, const internal::NamedArg<Context>&) void arg(string_view, const internal::NamedArg<Context>&)
FMT_DELETED_OR_UNDEFINED; FMT_DELETED_OR_UNDEFINED;
template <typename Context> template <typename Context>
void arg(WStringRef, const internal::NamedArg<Context>&) void arg(wstring_view, const internal::NamedArg<Context>&)
FMT_DELETED_OR_UNDEFINED; FMT_DELETED_OR_UNDEFINED;
} }
@ -3269,7 +3256,7 @@ struct IsInteger {
struct WidthHandler { struct WidthHandler {
template <typename T> template <typename T>
typename std::enable_if<IsInteger<T>::value, ULongLong>::type typename std::enable_if<IsInteger<T>::value, ulong_long>::type
operator()(T value) { operator()(T value) {
if (is_negative(value)) if (is_negative(value))
FMT_THROW(format_error("negative width")); FMT_THROW(format_error("negative width"));
@ -3277,7 +3264,7 @@ struct WidthHandler {
} }
template <typename T> template <typename T>
typename std::enable_if<!IsInteger<T>::value, ULongLong>::type typename std::enable_if<!IsInteger<T>::value, ulong_long>::type
operator()(T value) { operator()(T value) {
FMT_THROW(format_error("width is not integer")); FMT_THROW(format_error("width is not integer"));
return 0; return 0;
@ -3286,7 +3273,7 @@ struct WidthHandler {
struct PrecisionHandler { struct PrecisionHandler {
template <typename T> template <typename T>
typename std::enable_if<IsInteger<T>::value, ULongLong>::type typename std::enable_if<IsInteger<T>::value, ulong_long>::type
operator()(T value) { operator()(T value) {
if (is_negative(value)) if (is_negative(value))
FMT_THROW(format_error("negative precision")); FMT_THROW(format_error("negative precision"));
@ -3294,7 +3281,7 @@ struct PrecisionHandler {
} }
template <typename T> template <typename T>
typename std::enable_if<!IsInteger<T>::value, ULongLong>::type typename std::enable_if<!IsInteger<T>::value, ulong_long>::type
operator()(T value) { operator()(T value) {
FMT_THROW(format_error("precision is not integer")); FMT_THROW(format_error("precision is not integer"));
return 0; return 0;
@ -3305,7 +3292,7 @@ struct PrecisionHandler {
template <typename Char> template <typename Char>
inline typename basic_context<Char>::format_arg inline typename basic_context<Char>::format_arg
basic_context<Char>::get_arg( basic_context<Char>::get_arg(
BasicStringRef<Char> name, const char *&error) { basic_string_view<Char> name, const char *&error) {
if (this->check_no_auto_index(error)) { if (this->check_no_auto_index(error)) {
map_.init(this->args()); map_.init(this->args());
if (const format_arg *arg = map_.find(name)) if (const format_arg *arg = map_.find(name))
@ -3336,7 +3323,7 @@ inline typename basic_context<Char>::format_arg
c = *++s; c = *++s;
} while (internal::is_name_start(c) || ('0' <= c && c <= '9')); } while (internal::is_name_start(c) || ('0' <= c && c <= '9'));
const char *error = 0; const char *error = 0;
format_arg arg = get_arg(BasicStringRef<Char>(start, s - start), error); format_arg arg = get_arg(basic_string_view<Char>(start, s - start), error);
if (error) if (error)
FMT_THROW(format_error(error)); FMT_THROW(format_error(error));
return arg; return arg;
@ -3424,7 +3411,7 @@ void do_format_arg(basic_buffer<Char> &buffer, basic_arg<Context> arg,
auto width_arg = ctx.parse_arg_id(); auto width_arg = ctx.parse_arg_id();
if (*s++ != '}') if (*s++ != '}')
FMT_THROW(format_error("invalid format string")); 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<int>::max)()) if (width > (std::numeric_limits<int>::max)())
FMT_THROW(format_error("number is too big")); FMT_THROW(format_error("number is too big"));
spec.width_ = static_cast<int>(width); spec.width_ = static_cast<int>(width);
@ -3441,7 +3428,7 @@ void do_format_arg(basic_buffer<Char> &buffer, basic_arg<Context> arg,
auto precision_arg = ctx.parse_arg_id(); auto precision_arg = ctx.parse_arg_id();
if (*s++ != '}') if (*s++ != '}')
FMT_THROW(format_error("invalid format string")); FMT_THROW(format_error("invalid format string"));
ULongLong precision = ulong_long precision =
visit(internal::PrecisionHandler(), precision_arg); visit(internal::PrecisionHandler(), precision_arg);
if (precision > (std::numeric_limits<int>::max)()) if (precision > (std::numeric_limits<int>::max)())
FMT_THROW(format_error("number is too big")); FMT_THROW(format_error("number is too big"));

View File

@ -85,7 +85,7 @@ void format_value(basic_buffer<Char> &buf, const T &value,
basic_context<Char> &ctx) { basic_context<Char> &ctx) {
internal::MemoryBuffer<Char, internal::INLINE_BUFFER_SIZE> buffer; internal::MemoryBuffer<Char, internal::INLINE_BUFFER_SIZE> buffer;
internal::format_value(buffer, value); internal::format_value(buffer, value);
BasicStringRef<Char> str(buffer.data(), buffer.size()); basic_string_view<Char> str(buffer.data(), buffer.size());
do_format_arg< ArgFormatter<Char> >( do_format_arg< ArgFormatter<Char> >(
buf, internal::make_arg< basic_context<Char> >(str), ctx); buf, internal::make_arg< basic_context<Char> >(str), ctx);
} }

View File

@ -124,7 +124,7 @@ void fmt::File::close() {
throw SystemError(errno, "cannot close file"); throw SystemError(errno, "cannot close file");
} }
fmt::LongLong fmt::File::size() const { fmt::long_long fmt::File::size() const {
#ifdef _WIN32 #ifdef _WIN32
// Use GetFileSize instead of GetFileSizeEx for the case when _WIN32_WINNT // Use GetFileSize instead of GetFileSizeEx for the case when _WIN32_WINNT
// is less than 0x0500 as is the case with some default MinGW builds. // 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) if (error != NO_ERROR)
throw WindowsError(GetLastError(), "cannot get file size"); 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; return (long_size << sizeof(DWORD) * CHAR_BIT) | size_lower;
#else #else
typedef struct stat Stat; typedef struct stat Stat;
Stat file_stat = Stat(); Stat file_stat = Stat();
if (FMT_POSIX_CALL(fstat(fd_, &file_stat)) == -1) if (FMT_POSIX_CALL(fstat(fd_, &file_stat)) == -1)
throw SystemError(errno, "cannot get file attributes"); 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 type of File::size is not large enough");
return file_stat.st_size; return file_stat.st_size;
#endif #endif

View File

@ -274,7 +274,7 @@ class File {
// Returns the file size. The size has signed type for consistency with // Returns the file size. The size has signed type for consistency with
// stat::st_size. // stat::st_size.
LongLong size() const; long_long size() const;
// Attempts to read count bytes from the file into the specified buffer. // Attempts to read count bytes from the file into the specified buffer.
std::size_t read(void *buffer, std::size_t count); std::size_t read(void *buffer, std::size_t count);

View File

@ -119,7 +119,7 @@ class ArgConverter {
// glibc's printf doesn't sign extend arguments of smaller types: // glibc's printf doesn't sign extend arguments of smaller types:
// std::printf("%lld", -42); // prints "4294967254" // std::printf("%lld", -42); // prints "4294967254"
// but we don't have to do the same because it's a UB. // but we don't have to do the same because it's a UB.
arg_ = internal::make_arg<Context>(static_cast<LongLong>(value)); arg_ = internal::make_arg<Context>(static_cast<long_long>(value));
} else { } else {
arg_ = internal::make_arg<Context>( arg_ = internal::make_arg<Context>(
static_cast<typename internal::MakeUnsigned<U>::Type>(value)); static_cast<typename internal::MakeUnsigned<U>::Type>(value));
@ -459,7 +459,7 @@ void printf_context<Char, AF>::format(basic_buffer<Char> &buffer) {
break; break;
case 'l': case 'l':
if (*s == 'l') if (*s == 'l')
convert_arg<fmt::LongLong>(arg, *++s); convert_arg<fmt::long_long>(arg, *++s);
else else
convert_arg<long>(arg, *s); convert_arg<long>(arg, *s);
break; break;

View File

@ -55,11 +55,11 @@ struct ValueExtractor {
}; };
TEST(FormatTest, ArgConverter) { TEST(FormatTest, ArgConverter) {
fmt::LongLong value = std::numeric_limits<fmt::LongLong>::max(); fmt::long_long value = std::numeric_limits<fmt::long_long>::max();
auto arg = fmt::internal::make_arg<fmt::context>(value); auto arg = fmt::internal::make_arg<fmt::context>(value);
visit(fmt::internal::ArgConverter< visit(fmt::internal::ArgConverter<
fmt::LongLong, fmt::context>(arg, 'd'), arg); fmt::long_long, fmt::context>(arg, 'd'), arg);
EXPECT_EQ(value, visit(ValueExtractor<fmt::LongLong>(), arg)); EXPECT_EQ(value, visit(ValueExtractor<fmt::long_long>(), arg));
} }
TEST(FormatTest, FormatNegativeNaN) { TEST(FormatTest, FormatNegativeNaN) {

View File

@ -73,7 +73,7 @@ using std::size_t;
using fmt::basic_writer; using fmt::basic_writer;
using fmt::format; using fmt::format;
using fmt::format_error; using fmt::format_error;
using fmt::StringRef; using fmt::string_view;
using fmt::CStringRef; using fmt::CStringRef;
using fmt::MemoryWriter; using fmt::MemoryWriter;
using fmt::WMemoryWriter; using fmt::WMemoryWriter;
@ -148,16 +148,16 @@ struct WriteChecker {
EXPECT_PRED_FORMAT1(WriteChecker<wchar_t>(), value) EXPECT_PRED_FORMAT1(WriteChecker<wchar_t>(), value)
} // namespace } // namespace
TEST(StringRefTest, Ctor) { TEST(StringViewTest, Ctor) {
EXPECT_STREQ("abc", StringRef("abc").data()); EXPECT_STREQ("abc", string_view("abc").data());
EXPECT_EQ(3u, StringRef("abc").size()); EXPECT_EQ(3u, string_view("abc").size());
EXPECT_STREQ("defg", StringRef(std::string("defg")).data()); EXPECT_STREQ("defg", string_view(std::string("defg")).data());
EXPECT_EQ(4u, StringRef(std::string("defg")).size()); EXPECT_EQ(4u, string_view(std::string("defg")).size());
} }
TEST(StringRefTest, ConvertToString) { TEST(StringViewTest, ConvertToString) {
std::string s = StringRef("abc").to_string(); std::string s = string_view("abc").to_string();
EXPECT_EQ("abc", s); EXPECT_EQ("abc", s);
} }
@ -1320,7 +1320,7 @@ TEST(FormatterTest, FormatString) {
} }
TEST(FormatterTest, FormatStringRef) { TEST(FormatterTest, FormatStringRef) {
EXPECT_EQ("test", format("{0}", StringRef("test"))); EXPECT_EQ("test", format("{0}", string_view("test")));
} }
TEST(FormatterTest, FormatCStringRef) { TEST(FormatterTest, FormatCStringRef) {

View File

@ -103,7 +103,7 @@ std::string read(File &f, std::size_t count) {
#endif // FMT_USE_FILE_DESCRIPTORS #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<char> out; fmt::internal::MemoryBuffer<char> out;
fmt::format_system_error(out, error_code, message); fmt::format_system_error(out, error_code, message);
return to_string(out); return to_string(out);

View File

@ -81,7 +81,7 @@
FMT_TEST_THROW_(statement, expected_exception, \ FMT_TEST_THROW_(statement, expected_exception, \
expected_message, GTEST_NONFATAL_FAILURE_) 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) \ #define EXPECT_SYSTEM_ERROR(statement, error_code, message) \
EXPECT_THROW_MSG(statement, fmt::SystemError, \ EXPECT_THROW_MSG(statement, fmt::SystemError, \

View File

@ -213,7 +213,7 @@ int (test::fileno)(FILE *stream) {
# define EXPECT_EQ_POSIX(expected, actual) # define EXPECT_EQ_POSIX(expected, actual)
#endif #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"); fmt::BufferedFile f(filename, "w");
f.print("{}", content); f.print("{}", content);
} }
@ -277,7 +277,7 @@ TEST(FileTest, Size) {
write_file("test", content); write_file("test", content);
File f("test", File::RDONLY); File f("test", File::RDONLY);
EXPECT_GE(f.size(), 0); EXPECT_GE(f.size(), 0);
EXPECT_EQ(content.size(), static_cast<fmt::ULongLong>(f.size())); EXPECT_EQ(content.size(), static_cast<fmt::ulong_long>(f.size()));
#ifdef _WIN32 #ifdef _WIN32
fmt::internal::MemoryBuffer<char> message; fmt::internal::MemoryBuffer<char> message;
fmt::internal::format_windows_error( fmt::internal::format_windows_error(

View File

@ -65,7 +65,7 @@ File open_file() {
} }
// Attempts to write a string to a 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(); std::size_t num_chars_left = s.size();
const char *ptr = s.data(); const char *ptr = s.data();
do { do {

View File

@ -40,7 +40,7 @@ using fmt::format_error;
const unsigned BIG_NUM = INT_MAX + 1u; const unsigned BIG_NUM = INT_MAX + 1u;
// Makes format string argument positional. // 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()); std::string s(format.to_string());
s.replace(s.find('%'), 1, "%1$"); s.replace(s.find('%'), 1, "%1$");
return s; return s;
@ -269,8 +269,8 @@ TEST(PrintfTest, DynamicPrecision) {
"argument index out of range"); "argument index out of range");
EXPECT_THROW_MSG(fmt::sprintf("%.*d", BIG_NUM, 42), format_error, EXPECT_THROW_MSG(fmt::sprintf("%.*d", BIG_NUM, 42), format_error,
"number is too big"); "number is too big");
if (sizeof(fmt::LongLong) != sizeof(int)) { if (sizeof(fmt::long_long) != sizeof(int)) {
fmt::LongLong prec = static_cast<fmt::LongLong>(INT_MIN) - 1; fmt::long_long prec = static_cast<fmt::long_long>(INT_MIN) - 1;
EXPECT_THROW_MSG(fmt::sprintf("%.*d", prec, 42), format_error, EXPECT_THROW_MSG(fmt::sprintf("%.*d", prec, 42), format_error,
"number is too big"); "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 short, short);
SPECIALIZE_MAKE_SIGNED(unsigned, int); SPECIALIZE_MAKE_SIGNED(unsigned, int);
SPECIALIZE_MAKE_SIGNED(unsigned long, long); 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``. // Test length format specifier ``length_spec``.
template <typename T, typename U> template <typename T, typename U>
void TestLength(const char *length_spec, U value) { void TestLength(const char *length_spec, U value) {
fmt::LongLong signed_value = 0; fmt::long_long signed_value = 0;
fmt::ULongLong unsigned_value = 0; fmt::ulong_long unsigned_value = 0;
// Apply integer promotion to the argument. // Apply integer promotion to the argument.
using std::numeric_limits; using std::numeric_limits;
fmt::ULongLong max = numeric_limits<U>::max(); fmt::ulong_long max = numeric_limits<U>::max();
using fmt::internal::const_check; using fmt::internal::const_check;
if (const_check(max <= static_cast<unsigned>(numeric_limits<int>::max()))) { if (const_check(max <= static_cast<unsigned>(numeric_limits<int>::max()))) {
signed_value = static_cast<int>(value); signed_value = static_cast<int>(value);
@ -308,7 +308,7 @@ void TestLength(const char *length_spec, U value) {
} }
using fmt::internal::MakeUnsigned; using fmt::internal::MakeUnsigned;
if (sizeof(U) <= sizeof(int) && sizeof(int) < sizeof(T)) { if (sizeof(U) <= sizeof(int) && sizeof(int) < sizeof(T)) {
signed_value = static_cast<fmt::LongLong>(value); signed_value = static_cast<fmt::long_long>(value);
unsigned_value = static_cast<typename MakeUnsigned<unsigned>::Type>(value); unsigned_value = static_cast<typename MakeUnsigned<unsigned>::Type>(value);
} else { } else {
signed_value = static_cast<typename MakeSigned<T>::Type>(value); signed_value = static_cast<typename MakeSigned<T>::Type>(value);
@ -339,20 +339,20 @@ void TestLength(const char *length_spec) {
TestLength<T>(length_spec, -42); TestLength<T>(length_spec, -42);
TestLength<T>(length_spec, min); TestLength<T>(length_spec, min);
TestLength<T>(length_spec, max); TestLength<T>(length_spec, max);
TestLength<T>(length_spec, fmt::LongLong(min) - 1); TestLength<T>(length_spec, fmt::long_long(min) - 1);
fmt::ULongLong long_long_max = std::numeric_limits<fmt::LongLong>::max(); fmt::ulong_long long_long_max = std::numeric_limits<fmt::long_long>::max();
if (static_cast<fmt::ULongLong>(max) < long_long_max) if (static_cast<fmt::ulong_long>(max) < long_long_max)
TestLength<T>(length_spec, fmt::LongLong(max) + 1); TestLength<T>(length_spec, fmt::long_long(max) + 1);
TestLength<T>(length_spec, std::numeric_limits<short>::min()); TestLength<T>(length_spec, std::numeric_limits<short>::min());
TestLength<T>(length_spec, std::numeric_limits<unsigned short>::max()); TestLength<T>(length_spec, std::numeric_limits<unsigned short>::max());
TestLength<T>(length_spec, std::numeric_limits<int>::min()); TestLength<T>(length_spec, std::numeric_limits<int>::min());
TestLength<T>(length_spec, std::numeric_limits<int>::max()); TestLength<T>(length_spec, std::numeric_limits<int>::max());
TestLength<T>(length_spec, std::numeric_limits<unsigned>::min()); TestLength<T>(length_spec, std::numeric_limits<unsigned>::min());
TestLength<T>(length_spec, std::numeric_limits<unsigned>::max()); TestLength<T>(length_spec, std::numeric_limits<unsigned>::max());
TestLength<T>(length_spec, std::numeric_limits<fmt::LongLong>::min()); TestLength<T>(length_spec, std::numeric_limits<fmt::long_long>::min());
TestLength<T>(length_spec, std::numeric_limits<fmt::LongLong>::max()); TestLength<T>(length_spec, std::numeric_limits<fmt::long_long>::max());
TestLength<T>(length_spec, std::numeric_limits<fmt::ULongLong>::min()); TestLength<T>(length_spec, std::numeric_limits<fmt::ulong_long>::min());
TestLength<T>(length_spec, std::numeric_limits<fmt::ULongLong>::max()); TestLength<T>(length_spec, std::numeric_limits<fmt::ulong_long>::max());
} }
TEST(PrintfTest, Length) { TEST(PrintfTest, Length) {
@ -363,8 +363,8 @@ TEST(PrintfTest, Length) {
TestLength<unsigned short>("h"); TestLength<unsigned short>("h");
TestLength<long>("l"); TestLength<long>("l");
TestLength<unsigned long>("l"); TestLength<unsigned long>("l");
TestLength<fmt::LongLong>("ll"); TestLength<fmt::long_long>("ll");
TestLength<fmt::ULongLong>("ll"); TestLength<fmt::ulong_long>("ll");
TestLength<intmax_t>("j"); TestLength<intmax_t>("j");
TestLength<std::size_t>("z"); TestLength<std::size_t>("z");
TestLength<std::ptrdiff_t>("t"); TestLength<std::ptrdiff_t>("t");
@ -388,10 +388,10 @@ TEST(PrintfTest, Int) {
EXPECT_PRINTF(fmt::format("{:X}", u), "%X", -42); 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 // fmt::printf allows passing long long arguments to %d without length
// specifiers. // specifiers.
fmt::LongLong max = std::numeric_limits<fmt::LongLong>::max(); fmt::long_long max = std::numeric_limits<fmt::long_long>::max();
EXPECT_PRINTF(fmt::format("{}", max), "%d", max); EXPECT_PRINTF(fmt::format("{}", max), "%d", max);
} }

View File

@ -54,7 +54,7 @@
using fmt::basic_arg; using fmt::basic_arg;
using fmt::basic_buffer; using fmt::basic_buffer;
using fmt::StringRef; using fmt::string_view;
using fmt::internal::MemoryBuffer; using fmt::internal::MemoryBuffer;
using fmt::internal::value; using fmt::internal::value;
@ -488,8 +488,8 @@ VISIT_TYPE(unsigned short, unsigned);
VISIT_TYPE(long, int); VISIT_TYPE(long, int);
VISIT_TYPE(unsigned long, unsigned); VISIT_TYPE(unsigned long, unsigned);
#else #else
VISIT_TYPE(long, fmt::LongLong); VISIT_TYPE(long, fmt::long_long);
VISIT_TYPE(unsigned long, fmt::ULongLong); VISIT_TYPE(unsigned long, fmt::ulong_long);
#endif #endif
VISIT_TYPE(float, double); VISIT_TYPE(float, double);
@ -511,7 +511,7 @@ class NumericArgTest : public testing::Test {};
typedef ::testing::Types< typedef ::testing::Types<
bool, signed char, unsigned char, signed, unsigned short, 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; float, double, long double> Types;
TYPED_TEST_CASE(NumericArgTest, Types); TYPED_TEST_CASE(NumericArgTest, Types);
@ -546,7 +546,7 @@ TEST(UtilTest, StringArg) {
CHECK_ARG_(wchar_t, cstr, str); CHECK_ARG_(wchar_t, cstr, str);
CHECK_ARG(cstr); CHECK_ARG(cstr);
StringRef sref(str); string_view sref(str);
CHECK_ARG_(char, sref, std::string(str)); CHECK_ARG_(char, sref, std::string(str));
CHECK_ARG_(wchar_t, sref, std::string(str)); CHECK_ARG_(wchar_t, sref, std::string(str));
CHECK_ARG(sref); CHECK_ARG(sref);
@ -557,11 +557,11 @@ TEST(UtilTest, WStringArg) {
wchar_t *str = str_data; wchar_t *str = str_data;
const wchar_t *cstr = str; 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, str);
CHECK_ARG_(wchar_t, sref, cstr); CHECK_ARG_(wchar_t, sref, cstr);
CHECK_ARG_(wchar_t, sref, std::wstring(str)); 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) { TEST(UtilTest, PointerArg) {
@ -612,7 +612,7 @@ void test_count_digits() {
TEST(UtilTest, StringRef) { TEST(UtilTest, StringRef) {
// Test that StringRef::size() returns string length, not buffer size. // Test that StringRef::size() returns string length, not buffer size.
char str[100] = "some string"; 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)); EXPECT_LT(std::strlen(str), sizeof(str));
} }
@ -623,18 +623,18 @@ void CheckOp() {
std::size_t num_inputs = sizeof(inputs) / sizeof(*inputs); std::size_t num_inputs = sizeof(inputs) / sizeof(*inputs);
for (std::size_t i = 0; i < num_inputs; ++i) { for (std::size_t i = 0; i < num_inputs; ++i) {
for (std::size_t j = 0; j < num_inputs; ++j) { for (std::size_t j = 0; j < num_inputs; ++j) {
StringRef lhs(inputs[i]), rhs(inputs[j]); string_view lhs(inputs[i]), rhs(inputs[j]);
EXPECT_EQ(Op<int>()(lhs.compare(rhs), 0), Op<StringRef>()(lhs, rhs)); EXPECT_EQ(Op<int>()(lhs.compare(rhs), 0), Op<string_view>()(lhs, rhs));
} }
} }
} }
TEST(UtilTest, StringRefCompare) { TEST(UtilTest, StringRefCompare) {
EXPECT_EQ(0, StringRef("foo").compare(StringRef("foo"))); EXPECT_EQ(0, string_view("foo").compare(string_view("foo")));
EXPECT_GT(StringRef("fop").compare(StringRef("foo")), 0); EXPECT_GT(string_view("fop").compare(string_view("foo")), 0);
EXPECT_LT(StringRef("foo").compare(StringRef("fop")), 0); EXPECT_LT(string_view("foo").compare(string_view("fop")), 0);
EXPECT_GT(StringRef("foo").compare(StringRef("fo")), 0); EXPECT_GT(string_view("foo").compare(string_view("fo")), 0);
EXPECT_LT(StringRef("fo").compare(StringRef("foo")), 0); EXPECT_LT(string_view("fo").compare(string_view("foo")), 0);
CheckOp<std::equal_to>(); CheckOp<std::equal_to>();
CheckOp<std::not_equal_to>(); CheckOp<std::not_equal_to>();
CheckOp<std::less>(); CheckOp<std::less>();
@ -666,7 +666,7 @@ TEST(UtilTest, UTF8ToUTF16) {
template <typename Converter, typename Char> template <typename Converter, typename Char>
void check_utf_conversion_error( void check_utf_conversion_error(
const char *message, const char *message,
fmt::BasicStringRef<Char> str = fmt::BasicStringRef<Char>(0, 0)) { fmt::basic_string_view<Char> str = fmt::basic_string_view<Char>(0, 0)) {
fmt::internal::MemoryBuffer<char> out; fmt::internal::MemoryBuffer<char> out;
fmt::internal::format_windows_error(out, ERROR_INVALID_PARAMETER, message); fmt::internal::format_windows_error(out, ERROR_INVALID_PARAMETER, message);
fmt::SystemError error(0, ""); fmt::SystemError error(0, "");
@ -688,19 +688,19 @@ TEST(UtilTest, UTF8ToUTF16Error) {
const char *message = "cannot convert string from UTF-8 to UTF-16"; const char *message = "cannot convert string from UTF-8 to UTF-16";
check_utf_conversion_error<fmt::internal::UTF8ToUTF16, char>(message); check_utf_conversion_error<fmt::internal::UTF8ToUTF16, char>(message);
check_utf_conversion_error<fmt::internal::UTF8ToUTF16, char>( check_utf_conversion_error<fmt::internal::UTF8ToUTF16, char>(
message, fmt::StringRef("foo", INT_MAX + 1u)); message, fmt::string_view("foo", INT_MAX + 1u));
} }
TEST(UtilTest, UTF16ToUTF8Convert) { TEST(UtilTest, UTF16ToUTF8Convert) {
fmt::internal::UTF16ToUTF8 u; 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, 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 #endif // _WIN32
typedef void (*FormatErrorMessage)( typedef void (*FormatErrorMessage)(
fmt::buffer &out, int error_code, StringRef message); fmt::buffer &out, int error_code, string_view message);
template <typename Error> template <typename Error>
void check_throw_error(int error_code, FormatErrorMessage format) { void check_throw_error(int error_code, FormatErrorMessage format) {
@ -723,7 +723,7 @@ TEST(UtilTest, FormatSystemError) {
to_string(message)); to_string(message));
message.clear(); message.clear();
fmt::format_system_error( fmt::format_system_error(
message, EDOM, fmt::StringRef(0, std::numeric_limits<size_t>::max())); message, EDOM, fmt::string_view(0, std::numeric_limits<size_t>::max()));
EXPECT_EQ(fmt::format("error {}", EDOM), to_string(message)); EXPECT_EQ(fmt::format("error {}", EDOM), to_string(message));
} }
@ -760,7 +760,7 @@ TEST(UtilTest, FormatWindowsError) {
actual_message.clear(); actual_message.clear();
fmt::internal::format_windows_error( fmt::internal::format_windows_error(
actual_message, ERROR_FILE_EXISTS, actual_message, ERROR_FILE_EXISTS,
fmt::StringRef(0, std::numeric_limits<size_t>::max())); fmt::string_view(0, std::numeric_limits<size_t>::max()));
EXPECT_EQ(fmt::format("error {}", ERROR_FILE_EXISTS), EXPECT_EQ(fmt::format("error {}", ERROR_FILE_EXISTS),
fmt::to_string(actual_message)); fmt::to_string(actual_message));
} }