Provide more overloads for the wide string flavour

Signed-off-by: Daniela Engert <dani@ngrt.de>
This commit is contained in:
Daniela Engert
2018-04-26 20:32:14 +02:00
committed by Victor Zverovich
parent ca31ca13f1
commit 2570f1afdc
8 changed files with 182 additions and 23 deletions

View File

@@ -151,7 +151,8 @@ class char_converter: public function<void> {
template <typename T>
typename std::enable_if<std::is_integral<T>::value>::type
operator()(T value) {
arg_ = internal::make_arg<Context>(static_cast<char>(value));
typedef typename Context::char_type Char;
arg_ = internal::make_arg<Context>(static_cast<Char>(value));
}
template <typename T>
@@ -223,11 +224,16 @@ class printf_arg_formatter:
context_type &context_;
void write_null_pointer() {
void write_null_pointer(char) {
this->spec().type_ = 0;
this->write("(nil)");
}
void write_null_pointer(wchar_t) {
this->spec().type_ = 0;
this->write(L"(nil)");
}
public:
typedef typename base::format_specs format_specs;
@@ -270,18 +276,29 @@ class printf_arg_formatter:
if (value)
base::operator()(value);
else if (this->spec().type_ == 'p')
write_null_pointer();
write_null_pointer(char_type());
else
this->write("(null)");
return this->out();
}
/** Formats a null-terminated wide C string. */
iterator operator()(const wchar_t *value) {
if (value)
base::operator()(value);
else if (this->spec().type_ == 'p')
write_null_pointer(char_type());
else
this->write(L"(null)");
return this->out();
}
/** Formats a pointer. */
iterator operator()(const void *value) {
if (value)
return base::operator()(value);
this->spec().type_ = 0;
write_null_pointer();
write_null_pointer(char_type());
return this->out();
}
@@ -518,7 +535,7 @@ void basic_printf_context<OutputIt, Char, AF>::format() {
spec.type_ = 'd';
break;
case 'c':
// TODO: handle wchar_t
// TODO: handle wchar_t better?
visit(internal::char_converter<basic_printf_context>(arg), arg);
break;
}
@@ -551,6 +568,7 @@ inline format_arg_store<printf_context<internal::buffer>::type, Args...>
args...);
}
typedef basic_format_args<printf_context<internal::buffer>::type> printf_args;
typedef basic_format_args<printf_context<internal::wbuffer>::type> wprintf_args;
inline std::string vsprintf(string_view format, printf_args args) {
memory_buffer buffer;
@@ -573,9 +591,7 @@ inline std::string sprintf(string_view format_str, const Args & ... args) {
make_format_args<typename printf_context<internal::buffer>::type>(args...));
}
inline std::wstring vsprintf(
wstring_view format,
basic_format_args<printf_context<internal::wbuffer>::type> args) {
inline std::wstring vsprintf(wstring_view format, wprintf_args args) {
wmemory_buffer buffer;
printf(buffer, format, args);
return to_string(buffer);
@@ -583,17 +599,19 @@ inline std::wstring vsprintf(
template <typename... Args>
inline std::wstring sprintf(wstring_view format_str, const Args & ... args) {
auto vargs = make_format_args<
typename printf_context<internal::wbuffer>::type>(args...);
return vsprintf(format_str, vargs);
return vsprintf(format_str,
make_format_args<typename printf_context<internal::wbuffer>::type>(args...));
}
inline int vfprintf(std::FILE *f, string_view format, printf_args args) {
memory_buffer buffer;
template <typename Char>
inline int vfprintf(std::FILE *f, basic_string_view<Char> format,
basic_format_args<typename printf_context<
internal::basic_buffer<Char>>::type> args) {
basic_memory_buffer<Char> buffer;
printf(buffer, format, args);
std::size_t size = buffer.size();
return std::fwrite(
buffer.data(), 1, size, f) < size ? -1 : static_cast<int>(size);
buffer.data(), sizeof(Char), size, f) < size ? -1 : static_cast<int>(size);
}
/**
@@ -612,10 +630,21 @@ inline int fprintf(std::FILE *f, string_view format_str, const Args & ... args)
return vfprintf(f, format_str, vargs);
}
template <typename... Args>
inline int fprintf(std::FILE *f, wstring_view format_str,
const Args & ... args) {
return vfprintf(f, format_str,
make_format_args<typename printf_context<internal::wbuffer>::type>(args...));
}
inline int vprintf(string_view format, printf_args args) {
return vfprintf(stdout, format, args);
}
inline int vprintf(wstring_view format, wprintf_args args) {
return vfprintf(stdout, format, args);
}
/**
\rst
Prints formatted data to ``stdout``.
@@ -631,6 +660,12 @@ inline int printf(string_view format_str, const Args & ... args) {
make_format_args<typename printf_context<internal::buffer>::type>(args...));
}
template <typename... Args>
inline int printf(wstring_view format_str, const Args & ... args) {
return vprintf(format_str,
make_format_args<typename printf_context<internal::wbuffer>::type>(args...));
}
inline int vfprintf(std::ostream &os, string_view format_str,
printf_args args) {
memory_buffer buffer;
@@ -639,6 +674,14 @@ inline int vfprintf(std::ostream &os, string_view format_str,
return static_cast<int>(buffer.size());
}
inline int vfprintf(std::wostream &os, wstring_view format_str,
wprintf_args args) {
wmemory_buffer buffer;
printf(buffer, format_str, args);
internal::write(os, buffer);
return static_cast<int>(buffer.size());
}
/**
\rst
Prints formatted data to the stream *os*.
@@ -655,6 +698,14 @@ inline int fprintf(std::ostream &os, string_view format_str,
typename printf_context<internal::buffer>::type>(args...);
return vfprintf(os, format_str, vargs);
}
template <typename... Args>
inline int fprintf(std::wostream &os, wstring_view format_str,
const Args & ... args) {
auto vargs = make_format_args<
typename printf_context<internal::buffer>::type>(args...);
return vfprintf(os, format_str, vargs);
}
} // namespace fmt
#endif // FMT_PRINTF_H_