CStringRef -> cstring_view

This commit is contained in:
Victor Zverovich 2017-03-26 15:13:10 -07:00
parent 5aa8d6ea21
commit 12252152ac
10 changed files with 78 additions and 79 deletions

View File

@ -214,7 +214,7 @@ void report_error(FormatFunc func, int error_code,
} // namespace
FMT_FUNC void system_error::init(
int err_code, CStringRef format_str, args args) {
int err_code, cstring_view format_str, args args) {
error_code_ = err_code;
memory_buffer buffer;
format_system_error(buffer, err_code, vformat(format_str, args));
@ -338,7 +338,7 @@ FMT_FUNC int internal::utf16_to_utf8::convert(wstring_view s) {
}
FMT_FUNC void windows_error::init(
int err_code, CStringRef format_str, args args) {
int err_code, cstring_view format_str, args args) {
error_code_ = err_code;
memory_buffer buffer;
internal::format_windows_error(buffer, err_code, vformat(format_str, args));
@ -420,17 +420,17 @@ FMT_FUNC void report_windows_error(
}
#endif
FMT_FUNC void vprint(std::FILE *f, CStringRef format_str, args args) {
FMT_FUNC void vprint(std::FILE *f, cstring_view format_str, args args) {
memory_buffer buffer;
vformat_to(buffer, format_str, args);
std::fwrite(buffer.data(), 1, buffer.size(), f);
}
FMT_FUNC void vprint(CStringRef format_str, args args) {
FMT_FUNC void vprint(cstring_view format_str, args args) {
vprint(stdout, format_str, args);
}
FMT_FUNC void vprint_colored(Color c, CStringRef format, args args) {
FMT_FUNC void vprint_colored(Color c, cstring_view format, args args) {
char escape[] = "\x1b[30m";
escape[3] = static_cast<char>('0' + c);
std::fputs(escape, stdout);
@ -439,10 +439,9 @@ FMT_FUNC void vprint_colored(Color c, CStringRef format, args args) {
}
template <typename Char>
void printf(basic_writer<Char> &w, BasicCStringRef<Char> format,
args args);
void printf(basic_writer<Char> &w, basic_cstring_view<Char> format, args args);
FMT_FUNC int vfprintf(std::FILE *f, CStringRef format, printf_args args) {
FMT_FUNC int vfprintf(std::FILE *f, cstring_view format, printf_args args) {
memory_buffer buffer;
printf(buffer, format, args);
std::size_t size = buffer.size();

View File

@ -476,56 +476,56 @@ typedef basic_string_view<wchar_t> wstring_view;
/**
\rst
A reference to a null terminated string. It can be constructed from a C
A reference to a null-terminated string. 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 |
+=============+==========================+
| CStringRef | BasicCStringRef<char> |
+-------------+--------------------------+
| WCStringRef | BasicCStringRef<wchar_t> |
+-------------+--------------------------+
+---------------+-----------------------------+
| Type | Definition |
+===============+=============================+
| cstring_view | basic_cstring_view<char> |
+---------------+-----------------------------+
| wcstring_view | basic_cstring_view<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(CStringRef format_str, const Args & ... args);
std::string format(cstring_view format_str, const Args & ... args);
format("{}", 42);
format(std::string("{}"), 42);
\endrst
*/
template <typename Char>
class BasicCStringRef {
class basic_cstring_view {
private:
const Char *data_;
public:
/** Constructs a string reference object from a C string. */
BasicCStringRef(const Char *s) : data_(s) {}
basic_cstring_view(const Char *s) : data_(s) {}
/**
\rst
Constructs a string reference from an ``std::string`` object.
\endrst
*/
BasicCStringRef(const std::basic_string<Char> &s) : data_(s.c_str()) {}
basic_cstring_view(const std::basic_string<Char> &s) : data_(s.c_str()) {}
/** Returns the pointer to a C string. */
const Char *c_str() const { return data_; }
};
typedef BasicCStringRef<char> CStringRef;
typedef BasicCStringRef<wchar_t> WCStringRef;
typedef basic_cstring_view<char> cstring_view;
typedef basic_cstring_view<wchar_t> wcstring_view;
/** A formatting error such as invalid format string. */
class format_error : public std::runtime_error {
public:
explicit format_error(CStringRef message)
explicit format_error(cstring_view message)
: std::runtime_error(message.c_str()) {}
~format_error() throw();
};
@ -1197,7 +1197,7 @@ template <> constexpr Type gettype<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<string_view>() { return STRING; }
template <> constexpr Type gettype<CStringRef>() { return CSTRING; }
template <> constexpr Type gettype<cstring_view>() { return CSTRING; }
template <> constexpr Type gettype<wchar_t *>() { return TSTRING; }
template <> constexpr Type gettype<const wchar_t *>() { return TSTRING; }
template <> constexpr Type gettype<std::wstring>() { return TSTRING; }
@ -1335,7 +1335,7 @@ class value {
FMT_MAKE_VALUE(const unsigned char *, ustring.value, CSTRING)
FMT_MAKE_STR_VALUE(const std::string &, STRING)
FMT_MAKE_STR_VALUE(string_view, STRING)
FMT_MAKE_VALUE_(CStringRef, string.value, CSTRING, value.c_str())
FMT_MAKE_VALUE_(cstring_view, string.value, CSTRING, value.c_str())
#define FMT_MAKE_WSTR_VALUE(Type, TYPE) \
value(typename wchar_helper<Type, Char>::supported value) { \
@ -2111,7 +2111,7 @@ class basic_context :
*/
class system_error : public std::runtime_error {
private:
void init(int err_code, CStringRef format_str, args args);
void init(int err_code, cstring_view format_str, args args);
protected:
int error_code_;
@ -2138,7 +2138,7 @@ class system_error : public std::runtime_error {
\endrst
*/
template <typename... Args>
system_error(int error_code, CStringRef message, const Args & ... args)
system_error(int error_code, cstring_view message, const Args & ... args)
: std::runtime_error("") {
init(error_code, message, make_args(args...));
}
@ -2799,7 +2799,7 @@ FMT_API void report_system_error(int error_code,
/** A Windows error. */
class windows_error : public system_error {
private:
FMT_API void init(int error_code, CStringRef format_str, args args);
FMT_API void init(int error_code, cstring_view format_str, args args);
public:
/**
@ -2831,7 +2831,7 @@ class windows_error : public system_error {
\endrst
*/
template <typename... Args>
windows_error(int error_code, CStringRef message, const Args & ... args) {
windows_error(int error_code, cstring_view message, const Args & ... args) {
init(error_code, message, make_args(args...));
}
};
@ -2845,7 +2845,7 @@ FMT_API void report_windows_error(int error_code,
enum Color { BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE };
FMT_API void vprint_colored(Color c, CStringRef format, args args);
FMT_API void vprint_colored(Color c, cstring_view format, args args);
/**
Formats a string and prints it to stdout using ANSI escape sequences
@ -2854,36 +2854,36 @@ FMT_API void vprint_colored(Color c, CStringRef format, args args);
print_colored(fmt::RED, "Elapsed time: {0:.2f} seconds", 1.23);
*/
template <typename... Args>
inline void print_colored(Color c, CStringRef format_str,
inline void print_colored(Color c, cstring_view format_str,
const Args & ... args) {
vprint_colored(c, format_str, make_args(args...));
}
template <typename ArgFormatter, typename Char, typename Context>
void vformat_to(basic_buffer<Char> &buffer, BasicCStringRef<Char> format_str,
void vformat_to(basic_buffer<Char> &buffer, basic_cstring_view<Char> format_str,
basic_args<Context> args);
inline void vformat_to(buffer &buf, CStringRef format_str, args args) {
inline void vformat_to(buffer &buf, cstring_view format_str, args args) {
vformat_to<arg_formatter<char>>(buf, format_str, args);
}
inline void vformat_to(wbuffer &buf, WCStringRef format_str, wargs args) {
inline void vformat_to(wbuffer &buf, wcstring_view format_str, wargs args) {
vformat_to<arg_formatter<wchar_t>>(buf, format_str, args);
}
template <typename... Args>
inline void format_to(buffer &buf, CStringRef format_str,
inline void format_to(buffer &buf, cstring_view format_str,
const Args & ... args) {
vformat_to(buf, format_str, make_args(args...));
}
template <typename... Args>
inline void format_to(wbuffer &buf, WCStringRef format_str,
inline void format_to(wbuffer &buf, wcstring_view format_str,
const Args & ... args) {
vformat_to(buf, format_str, make_args<wcontext>(args...));
}
inline std::string vformat(CStringRef format_str, args args) {
inline std::string vformat(cstring_view format_str, args args) {
memory_buffer buffer;
vformat_to(buffer, format_str, args);
return to_string(buffer);
@ -2899,22 +2899,22 @@ inline std::string vformat(CStringRef format_str, args args) {
\endrst
*/
template <typename... Args>
inline std::string format(CStringRef format_str, const Args & ... args) {
inline std::string format(cstring_view format_str, const Args & ... args) {
return vformat(format_str, make_args(args...));
}
inline std::wstring vformat(WCStringRef format_str, wargs args) {
inline std::wstring vformat(wcstring_view format_str, wargs args) {
wmemory_buffer buffer;
vformat_to(buffer, format_str, args);
return to_string(buffer);
}
template <typename... Args>
inline std::wstring format(WCStringRef format_str, const Args & ... args) {
inline std::wstring format(wcstring_view format_str, const Args & ... args) {
return vformat(format_str, make_args<wcontext>(args...));
}
FMT_API void vprint(std::FILE *f, CStringRef format_str, args args);
FMT_API void vprint(std::FILE *f, cstring_view format_str, args args);
/**
\rst
@ -2926,11 +2926,12 @@ FMT_API void vprint(std::FILE *f, CStringRef format_str, args args);
\endrst
*/
template <typename... Args>
inline void print(std::FILE *f, CStringRef format_str, const Args & ... args) {
inline void print(std::FILE *f, cstring_view format_str,
const Args & ... args) {
vprint(f, format_str, make_args(args...));
}
FMT_API void vprint(CStringRef format_str, args args);
FMT_API void vprint(cstring_view format_str, args args);
/**
\rst
@ -2942,7 +2943,7 @@ FMT_API void vprint(CStringRef format_str, args args);
\endrst
*/
template <typename... Args>
inline void print(CStringRef format_str, const Args & ... args) {
inline void print(cstring_view format_str, const Args & ... args) {
vprint(format_str, make_args(args...));
}
@ -3383,7 +3384,7 @@ void do_format_arg(basic_buffer<Char> &buffer, basic_arg<Context> arg,
/** Formats arguments and writes the output to the buffer. */
template <typename ArgFormatter, typename Char, typename Context>
void vformat_to(basic_buffer<Char> &buffer, BasicCStringRef<Char> format_str,
void vformat_to(basic_buffer<Char> &buffer, basic_cstring_view<Char> format_str,
basic_args<Context> args) {
basic_context<Char> ctx(format_str.c_str(), args);
const Char *&s = ctx.ptr();

View File

@ -27,7 +27,7 @@ FMT_FUNC void write(std::ostream &os, buffer &buf) {
}
}
FMT_FUNC void vprint(std::ostream &os, CStringRef format_str, args args) {
FMT_FUNC void vprint(std::ostream &os, cstring_view format_str, args args) {
memory_buffer buffer;
vformat_to(buffer, format_str, args);
internal::write(os, buffer);

View File

@ -90,7 +90,7 @@ void format_value(basic_buffer<Char> &buf, const T &value,
buf, internal::make_arg< basic_context<Char> >(str), ctx);
}
FMT_API void vprint(std::ostream &os, CStringRef format_str, args args);
FMT_API void vprint(std::ostream &os, cstring_view format_str, args args);
/**
\rst
@ -102,7 +102,7 @@ FMT_API void vprint(std::ostream &os, CStringRef format_str, args args);
\endrst
*/
template <typename... Args>
inline void print(std::ostream &os, CStringRef format_str,
inline void print(std::ostream &os, cstring_view format_str,
const Args & ... args) {
vprint(os, format_str, make_args(args...));
}

View File

@ -69,7 +69,7 @@ fmt::BufferedFile::~BufferedFile() FMT_NOEXCEPT {
}
fmt::BufferedFile::BufferedFile(
fmt::CStringRef filename, fmt::CStringRef mode) {
fmt::cstring_view filename, fmt::cstring_view mode) {
FMT_RETRY_VAL(file_, FMT_SYSTEM(fopen(filename.c_str(), mode.c_str())), 0);
if (!file_)
throw system_error(errno, "cannot open file {}", filename);
@ -94,7 +94,7 @@ int fmt::BufferedFile::fileno() const {
return fd;
}
fmt::File::File(fmt::CStringRef path, int oflag) {
fmt::File::File(fmt::cstring_view path, int oflag) {
int mode = S_IRUSR | S_IWUSR;
#if defined(_WIN32) && !defined(__MINGW32__)
fd_ = -1;

View File

@ -154,7 +154,7 @@ public:
#endif
// Opens a file.
BufferedFile(CStringRef filename, CStringRef mode);
BufferedFile(cstring_view filename, cstring_view mode);
// Closes the file.
void close();
@ -166,12 +166,12 @@ public:
// of MinGW that define fileno as a macro.
int (fileno)() const;
void vprint(CStringRef format_str, const args &args) {
void vprint(cstring_view format_str, const args &args) {
fmt::vprint(file_, format_str, args);
}
template <typename... Args>
inline void print(CStringRef format_str, const Args & ... args) {
inline void print(cstring_view format_str, const Args & ... args) {
vprint(format_str, make_args(args...));
}
};
@ -201,7 +201,7 @@ class File {
File() FMT_NOEXCEPT : fd_(-1) {}
// Opens a file and constructs a File object representing this file.
File(CStringRef path, int oflag);
File(cstring_view path, int oflag);
#if !FMT_USE_RVALUE_REFERENCES
// Emulate a move constructor and a move assignment operator if rvalue

View File

@ -327,7 +327,7 @@ class printf_context :
appropriate lifetimes.
\endrst
*/
explicit printf_context(BasicCStringRef<Char> format_str,
explicit printf_context(basic_cstring_view<Char> format_str,
basic_args<printf_context> args)
: Base(format_str.c_str(), args) {}
@ -514,14 +514,14 @@ void format_value(basic_buffer<Char> &buf, const T &value,
}
template <typename Char>
void printf(basic_buffer<Char> &buf, BasicCStringRef<Char> format,
void printf(basic_buffer<Char> &buf, basic_cstring_view<Char> format,
basic_args<printf_context<Char>> args) {
printf_context<Char>(format, args).format(buf);
}
typedef basic_args<printf_context<char>> printf_args;
inline std::string vsprintf(CStringRef format, printf_args args) {
inline std::string vsprintf(cstring_view format, printf_args args) {
memory_buffer buffer;
printf(buffer, format, args);
return to_string(buffer);
@ -537,24 +537,24 @@ inline std::string vsprintf(CStringRef format, printf_args args) {
\endrst
*/
template <typename... Args>
inline std::string sprintf(CStringRef format_str, const Args & ... args) {
inline std::string sprintf(cstring_view format_str, const Args & ... args) {
return vsprintf(format_str, make_args<printf_context<char>>(args...));
}
inline std::wstring vsprintf(
WCStringRef format, basic_args<printf_context<wchar_t>> args) {
wcstring_view format, basic_args<printf_context<wchar_t>> args) {
wmemory_buffer buffer;
printf(buffer, format, args);
return to_string(buffer);
}
template <typename... Args>
inline std::wstring sprintf(WCStringRef format_str, const Args & ... args) {
inline std::wstring sprintf(wcstring_view format_str, const Args & ... args) {
auto vargs = make_args<printf_context<wchar_t>>(args...);
return vsprintf(format_str, vargs);
}
FMT_API int vfprintf(std::FILE *f, CStringRef format, printf_args args);
FMT_API int vfprintf(std::FILE *f, cstring_view format, printf_args args);
/**
\rst
@ -566,12 +566,12 @@ FMT_API int vfprintf(std::FILE *f, CStringRef format, printf_args args);
\endrst
*/
template <typename... Args>
inline int fprintf(std::FILE *f, CStringRef format_str, const Args & ... args) {
inline int fprintf(std::FILE *f, cstring_view format_str, const Args & ... args) {
auto vargs = make_args<printf_context<char>>(args...);
return vfprintf(f, format_str, vargs);
}
inline int vprintf(CStringRef format, printf_args args) {
inline int vprintf(cstring_view format, printf_args args) {
return vfprintf(stdout, format, args);
}
@ -585,11 +585,11 @@ inline int vprintf(CStringRef format, printf_args args) {
\endrst
*/
template <typename... Args>
inline int printf(CStringRef format_str, const Args & ... args) {
inline int printf(cstring_view format_str, const Args & ... args) {
return vprintf(format_str, make_args<printf_context<char>>(args...));
}
inline int vfprintf(std::ostream &os, CStringRef format_str, printf_args args) {
inline int vfprintf(std::ostream &os, cstring_view format_str, printf_args args) {
memory_buffer buffer;
printf(buffer, format_str, args);
internal::write(os, buffer);
@ -606,7 +606,7 @@ inline int vfprintf(std::ostream &os, CStringRef format_str, printf_args args) {
\endrst
*/
template <typename... Args>
inline int fprintf(std::ostream &os, CStringRef format_str,
inline int fprintf(std::ostream &os, cstring_view format_str,
const Args & ... args) {
auto vargs = make_args<printf_context<char>>(args...);
return vfprintf(os, format_str, vargs);

View File

@ -45,7 +45,7 @@ class CustomPrintfArgFormatter : public printf_arg_formatter<char> {
}
};
std::string custom_vformat(fmt::CStringRef format_str, fmt::args args) {
std::string custom_vformat(fmt::cstring_view format_str, fmt::args args) {
fmt::memory_buffer buffer;
// Pass custom argument formatter as a template arg to vwrite.
fmt::vformat_to<CustomArgFormatter>(buffer, format_str, args);

View File

@ -28,7 +28,6 @@
#include <cctype>
#include <cfloat>
#include <climits>
#include <clocale>
#include <cmath>
#include <cstring>
#include <memory>
@ -58,7 +57,7 @@ using fmt::basic_writer;
using fmt::format;
using fmt::format_error;
using fmt::string_view;
using fmt::CStringRef;
using fmt::cstring_view;
using fmt::memory_buffer;
using fmt::wmemory_buffer;
using fmt::fill;
@ -146,9 +145,9 @@ TEST(StringViewTest, ConvertToString) {
EXPECT_EQ("abc", s);
}
TEST(CStringRefTest, Ctor) {
EXPECT_STREQ("abc", CStringRef("abc").c_str());
EXPECT_STREQ("defg", CStringRef(std::string("defg")).c_str());
TEST(CStringViewTest, Ctor) {
EXPECT_STREQ("abc", cstring_view("abc").c_str());
EXPECT_STREQ("defg", cstring_view(std::string("defg")).c_str());
}
#if FMT_USE_TYPE_TRAITS
@ -465,7 +464,7 @@ TEST(FormatterTest, ArgErrors) {
template <int N>
struct TestFormat {
template <typename... Args>
static std::string format(fmt::CStringRef format_str, const Args & ... args) {
static std::string format(fmt::cstring_view format_str, const Args & ... args) {
return TestFormat<N - 1>::format(format_str, N - 1, args...);
}
};
@ -473,7 +472,7 @@ struct TestFormat {
template <>
struct TestFormat<0> {
template <typename... Args>
static std::string format(fmt::CStringRef format_str, const Args & ... args) {
static std::string format(fmt::cstring_view format_str, const Args & ... args) {
return fmt::format(format_str, args...);
}
};
@ -1230,12 +1229,12 @@ TEST(FormatterTest, FormatString) {
EXPECT_EQ("test", format("{0}", std::string("test")));
}
TEST(FormatterTest, FormatStringRef) {
TEST(FormatterTest, FormatStringView) {
EXPECT_EQ("test", format("{0}", string_view("test")));
}
TEST(FormatterTest, FormatCStringRef) {
EXPECT_EQ("test", format("{0}", CStringRef("test")));
TEST(FormatterTest, FormatCStringView) {
EXPECT_EQ("test", format("{0}", cstring_view("test")));
}
void format_value(fmt::buffer &buf, const Date &d, fmt::context &) {
@ -1513,7 +1512,7 @@ class MockArgFormatter : public fmt::internal::arg_formatter_base<char> {
void operator()(fmt::internal::custom_value<char>) {}
};
void custom_vformat(fmt::CStringRef format_str, fmt::args args) {
void custom_vformat(fmt::cstring_view format_str, fmt::args args) {
fmt::memory_buffer buffer;
fmt::vformat_to<MockArgFormatter>(buffer, format_str, args);
}

View File

@ -213,7 +213,7 @@ int (test::fileno)(FILE *stream) {
# define EXPECT_EQ_POSIX(expected, actual)
#endif
void write_file(fmt::CStringRef filename, fmt::string_view content) {
void write_file(fmt::cstring_view filename, fmt::string_view content) {
fmt::BufferedFile f(filename, "w");
f.print("{}", content);
}