forked from fmtlib/fmt
Replace StringValue with StringRef
This commit is contained in:
33
fmt/format.h
33
fmt/format.h
@ -1016,7 +1016,7 @@ struct Value {
|
|||||||
StringValue<char> string;
|
StringValue<char> string;
|
||||||
StringValue<signed char> sstring;
|
StringValue<signed char> sstring;
|
||||||
StringValue<unsigned char> ustring;
|
StringValue<unsigned char> ustring;
|
||||||
StringValue<wchar_t> wstring;
|
StringValue<wchar_t> wstring; // TODO: Char
|
||||||
CustomValue custom;
|
CustomValue custom;
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -1114,9 +1114,9 @@ typename std::result_of<Visitor(int)>::type
|
|||||||
case format_arg::CSTRING:
|
case format_arg::CSTRING:
|
||||||
return vis(arg.string.value);
|
return vis(arg.string.value);
|
||||||
case format_arg::STRING:
|
case format_arg::STRING:
|
||||||
return vis(arg.string);
|
return vis(StringRef(arg.string.value, arg.string.size));
|
||||||
case format_arg::WSTRING:
|
case format_arg::WSTRING:
|
||||||
return vis(arg.wstring);
|
return vis(WStringRef(arg.wstring.value, arg.wstring.size));
|
||||||
case format_arg::POINTER:
|
case format_arg::POINTER:
|
||||||
return vis(arg.pointer);
|
return vis(arg.pointer);
|
||||||
case format_arg::CUSTOM:
|
case format_arg::CUSTOM:
|
||||||
@ -1954,7 +1954,7 @@ class ArgFormatterBase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
template <typename StrChar>
|
template <typename StrChar>
|
||||||
void write_str(format_arg::StringValue<StrChar> value,
|
void write_str(BasicStringRef<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) {
|
||||||
@ -1962,7 +1962,7 @@ class ArgFormatterBase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
template <typename StrChar>
|
template <typename StrChar>
|
||||||
void write_str(format_arg::StringValue<StrChar> value,
|
void write_str(BasicStringRef<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) {
|
||||||
@ -1974,16 +1974,12 @@ class ArgFormatterBase {
|
|||||||
FormatSpec &spec() { return spec_; }
|
FormatSpec &spec() { return spec_; }
|
||||||
|
|
||||||
void write(bool value) {
|
void write(bool value) {
|
||||||
const char *str_value = value ? "true" : "false";
|
writer_.write_str(StringRef(value ? "true" : "false"), spec_);
|
||||||
format_arg::StringValue<char> str = { str_value, std::strlen(str_value) };
|
|
||||||
writer_.write_str(str, spec_);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void write(const char *value) {
|
void write(const char *value) {
|
||||||
format_arg::StringValue<char> str = {
|
writer_.write_str(
|
||||||
value, value != 0 ? std::strlen(value) : 0
|
StringRef(value, value != 0 ? std::strlen(value) : 0), spec_);
|
||||||
};
|
|
||||||
writer_.write_str(str, spec_);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public:
|
public:
|
||||||
@ -2042,11 +2038,11 @@ class ArgFormatterBase {
|
|||||||
write(value);
|
write(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
void operator()(format_arg::StringValue<char> value) {
|
void operator()(StringRef value) {
|
||||||
writer_.write_str(value, spec_);
|
writer_.write_str(value, spec_);
|
||||||
}
|
}
|
||||||
|
|
||||||
void operator()(format_arg::StringValue<wchar_t> value) {
|
void operator()(BasicStringRef<wchar_t> value) {
|
||||||
write_str(value);
|
write_str(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -2339,8 +2335,7 @@ class BasicWriter {
|
|||||||
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(const format_arg::StringValue<StrChar> &str,
|
void write_str(BasicStringRef<StrChar> str, const FormatSpec &spec);
|
||||||
const FormatSpec &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 stream. If you want to print a wide string as a
|
// and strings to a char stream. If you want to print a wide string as a
|
||||||
@ -2563,13 +2558,13 @@ typename BasicWriter<Char>::CharPtr BasicWriter<Char>::write_str(
|
|||||||
template <typename Char>
|
template <typename Char>
|
||||||
template <typename StrChar>
|
template <typename StrChar>
|
||||||
void BasicWriter<Char>::write_str(
|
void BasicWriter<Char>::write_str(
|
||||||
const format_arg::StringValue<StrChar> &s, const FormatSpec &spec) {
|
BasicStringRef<StrChar> s, const FormatSpec &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')
|
||||||
internal::report_unknown_type(spec.type_, "string");
|
internal::report_unknown_type(spec.type_, "string");
|
||||||
const StrChar *str_value = s.value;
|
const StrChar *str_value = s.data();
|
||||||
std::size_t str_size = s.size;
|
std::size_t str_size = s.size();
|
||||||
if (str_size == 0) {
|
if (str_size == 0) {
|
||||||
if (!str_value) {
|
if (!str_value) {
|
||||||
FMT_THROW(format_error("string pointer is null"));
|
FMT_THROW(format_error("string pointer is null"));
|
||||||
|
@ -437,12 +437,6 @@ namespace internal {
|
|||||||
bool operator==(Value::CustomValue lhs, Value::CustomValue rhs) {
|
bool operator==(Value::CustomValue lhs, Value::CustomValue rhs) {
|
||||||
return lhs.value == rhs.value;
|
return lhs.value == rhs.value;
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename T>
|
|
||||||
bool operator==(Value::StringValue<T> lhs, Value::StringValue<T> rhs) {
|
|
||||||
return std::basic_string<T>(lhs.value, lhs.size) ==
|
|
||||||
std::basic_string<T>(rhs.value, rhs.size);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -542,11 +536,10 @@ TEST(UtilTest, StringArg) {
|
|||||||
CHECK_ARG_(wchar_t, cstr, str);
|
CHECK_ARG_(wchar_t, cstr, str);
|
||||||
CHECK_ARG(cstr);
|
CHECK_ARG(cstr);
|
||||||
|
|
||||||
Value::StringValue<char> strval = {str, 4};
|
StringRef sref(str);
|
||||||
CHECK_ARG_(char, strval, std::string(str));
|
CHECK_ARG_(char, sref, std::string(str));
|
||||||
CHECK_ARG_(wchar_t, strval, std::string(str));
|
CHECK_ARG_(wchar_t, sref, std::string(str));
|
||||||
CHECK_ARG_(char, strval, fmt::StringRef(str));
|
CHECK_ARG(sref);
|
||||||
CHECK_ARG_(wchar_t, strval, fmt::StringRef(str));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(UtilTest, WStringArg) {
|
TEST(UtilTest, WStringArg) {
|
||||||
@ -554,11 +547,11 @@ TEST(UtilTest, WStringArg) {
|
|||||||
wchar_t *str = str_data;
|
wchar_t *str = str_data;
|
||||||
const wchar_t *cstr = str;
|
const wchar_t *cstr = str;
|
||||||
|
|
||||||
Value::StringValue<wchar_t> strval = {str, 4};
|
fmt::WStringRef sref(str);
|
||||||
CHECK_ARG_(wchar_t, strval, str);
|
CHECK_ARG_(wchar_t, sref, str);
|
||||||
CHECK_ARG_(wchar_t, strval, cstr);
|
CHECK_ARG_(wchar_t, sref, cstr);
|
||||||
CHECK_ARG_(wchar_t, strval, std::wstring(str));
|
CHECK_ARG_(wchar_t, sref, std::wstring(str));
|
||||||
CHECK_ARG_(wchar_t, strval, fmt::WStringRef(str));
|
CHECK_ARG_(wchar_t, sref, fmt::WStringRef(str));
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(UtilTest, PointerArg) {
|
TEST(UtilTest, PointerArg) {
|
||||||
|
Reference in New Issue
Block a user