From 12252152acfdd4f35c6c63f414c25815c9d2c1f2 Mon Sep 17 00:00:00 2001 From: Victor Zverovich Date: Sun, 26 Mar 2017 15:13:10 -0700 Subject: [PATCH] CStringRef -> cstring_view --- fmt/format.cc | 15 ++++--- fmt/format.h | 75 ++++++++++++++++++----------------- fmt/ostream.cc | 2 +- fmt/ostream.h | 4 +- fmt/posix.cc | 4 +- fmt/posix.h | 8 ++-- fmt/printf.h | 24 +++++------ test/custom-formatter-test.cc | 2 +- test/format-test.cc | 21 +++++----- test/posix-mock-test.cc | 2 +- 10 files changed, 78 insertions(+), 79 deletions(-) diff --git a/fmt/format.cc b/fmt/format.cc index 2b6afc2f..f8cd4c54 100644 --- a/fmt/format.cc +++ b/fmt/format.cc @@ -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('0' + c); std::fputs(escape, stdout); @@ -439,10 +439,9 @@ FMT_FUNC void vprint_colored(Color c, CStringRef format, args args) { } template -void printf(basic_writer &w, BasicCStringRef format, - args args); +void printf(basic_writer &w, basic_cstring_view 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(); diff --git a/fmt/format.h b/fmt/format.h index 13b260ea..4ee87c8b 100644 --- a/fmt/format.h +++ b/fmt/format.h @@ -476,56 +476,56 @@ typedef basic_string_view 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 | - +-------------+--------------------------+ - | WCStringRef | BasicCStringRef | - +-------------+--------------------------+ + +---------------+-----------------------------+ + | Type | Definition | + +===============+=============================+ + | cstring_view | basic_cstring_view | + +---------------+-----------------------------+ + | wcstring_view | basic_cstring_view | + +---------------+-----------------------------+ This class is most useful as a parameter type to allow passing different types of strings to a function, for example:: template - std::string format(CStringRef format_str, const Args & ... args); + std::string format(cstring_view format_str, const Args & ... args); format("{}", 42); format(std::string("{}"), 42); \endrst */ template -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 &s) : data_(s.c_str()) {} + basic_cstring_view(const std::basic_string &s) : data_(s.c_str()) {} /** Returns the pointer to a C string. */ const Char *c_str() const { return data_; } }; -typedef BasicCStringRef CStringRef; -typedef BasicCStringRef WCStringRef; +typedef basic_cstring_view cstring_view; +typedef basic_cstring_view 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() { return CSTRING; } template <> constexpr Type gettype() { return CSTRING; } template <> constexpr Type gettype() { return STRING; } template <> constexpr Type gettype() { return STRING; } -template <> constexpr Type gettype() { return CSTRING; } +template <> constexpr Type gettype() { return CSTRING; } template <> constexpr Type gettype() { return TSTRING; } template <> constexpr Type gettype() { return TSTRING; } template <> constexpr Type gettype() { 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::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 - 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 - 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 -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 -void vformat_to(basic_buffer &buffer, BasicCStringRef format_str, +void vformat_to(basic_buffer &buffer, basic_cstring_view format_str, basic_args 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>(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>(buf, format_str, args); } template -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 -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(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 -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 -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(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 -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 -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 &buffer, basic_arg arg, /** Formats arguments and writes the output to the buffer. */ template -void vformat_to(basic_buffer &buffer, BasicCStringRef format_str, +void vformat_to(basic_buffer &buffer, basic_cstring_view format_str, basic_args args) { basic_context ctx(format_str.c_str(), args); const Char *&s = ctx.ptr(); diff --git a/fmt/ostream.cc b/fmt/ostream.cc index 34c0bc99..b6696b16 100644 --- a/fmt/ostream.cc +++ b/fmt/ostream.cc @@ -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); diff --git a/fmt/ostream.h b/fmt/ostream.h index 59ccc83e..39606c7e 100644 --- a/fmt/ostream.h +++ b/fmt/ostream.h @@ -90,7 +90,7 @@ void format_value(basic_buffer &buf, const T &value, buf, internal::make_arg< basic_context >(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 -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...)); } diff --git a/fmt/posix.cc b/fmt/posix.cc index f8ce6b9e..1e5aca9d 100644 --- a/fmt/posix.cc +++ b/fmt/posix.cc @@ -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; diff --git a/fmt/posix.h b/fmt/posix.h index 03cbc664..39fa8d26 100644 --- a/fmt/posix.h +++ b/fmt/posix.h @@ -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 - 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 diff --git a/fmt/printf.h b/fmt/printf.h index 88a5450c..0c37ea00 100644 --- a/fmt/printf.h +++ b/fmt/printf.h @@ -327,7 +327,7 @@ class printf_context : appropriate lifetimes. \endrst */ - explicit printf_context(BasicCStringRef format_str, + explicit printf_context(basic_cstring_view format_str, basic_args args) : Base(format_str.c_str(), args) {} @@ -514,14 +514,14 @@ void format_value(basic_buffer &buf, const T &value, } template -void printf(basic_buffer &buf, BasicCStringRef format, +void printf(basic_buffer &buf, basic_cstring_view format, basic_args> args) { printf_context(format, args).format(buf); } typedef basic_args> 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 -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>(args...)); } inline std::wstring vsprintf( - WCStringRef format, basic_args> args) { + wcstring_view format, basic_args> args) { wmemory_buffer buffer; printf(buffer, format, args); return to_string(buffer); } template -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>(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 -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>(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 -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>(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 -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>(args...); return vfprintf(os, format_str, vargs); diff --git a/test/custom-formatter-test.cc b/test/custom-formatter-test.cc index 3e226d71..0e976791 100644 --- a/test/custom-formatter-test.cc +++ b/test/custom-formatter-test.cc @@ -45,7 +45,7 @@ class CustomPrintfArgFormatter : public printf_arg_formatter { } }; -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(buffer, format_str, args); diff --git a/test/format-test.cc b/test/format-test.cc index b5414d81..7906d96a 100644 --- a/test/format-test.cc +++ b/test/format-test.cc @@ -28,7 +28,6 @@ #include #include #include -#include #include #include #include @@ -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 struct TestFormat { template - 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::format(format_str, N - 1, args...); } }; @@ -473,7 +472,7 @@ struct TestFormat { template <> struct TestFormat<0> { template - 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 { void operator()(fmt::internal::custom_value) {} }; -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(buffer, format_str, args); } diff --git a/test/posix-mock-test.cc b/test/posix-mock-test.cc index f4722b79..f960b60f 100644 --- a/test/posix-mock-test.cc +++ b/test/posix-mock-test.cc @@ -213,7 +213,7 @@ int (test::fileno)(FILE *stream) { # define EXPECT_EQ_POSIX(expected, actual) #endif -void write_file(fmt::CStringRef filename, fmt::string_view content) { +void write_file(fmt::cstring_view filename, fmt::string_view content) { fmt::BufferedFile f(filename, "w"); f.print("{}", content); }