mirror of
https://github.com/fmtlib/fmt.git
synced 2025-11-15 15:00:01 +01:00
Add BasicCStringRef to represent a null-termated string (#100)
and use it instead of BasicStringRef in cases that assume that the string is null-terminated such as POSIX function and format string parser.
This commit is contained in:
@@ -66,6 +66,7 @@ using fmt::BasicWriter;
|
||||
using fmt::format;
|
||||
using fmt::FormatError;
|
||||
using fmt::StringRef;
|
||||
using fmt::CStringRef;
|
||||
using fmt::MemoryWriter;
|
||||
using fmt::WMemoryWriter;
|
||||
using fmt::pad;
|
||||
@@ -136,6 +137,24 @@ struct WriteChecker {
|
||||
EXPECT_PRED_FORMAT1(WriteChecker<wchar_t>(), value)
|
||||
} // namespace
|
||||
|
||||
TEST(StringRefTest, Ctor) {
|
||||
EXPECT_STREQ("abc", StringRef("abc").data());
|
||||
EXPECT_EQ(3u, StringRef("abc").size());
|
||||
|
||||
EXPECT_STREQ("defg", StringRef(std::string("defg")).data());
|
||||
EXPECT_EQ(4u, StringRef(std::string("defg")).size());
|
||||
}
|
||||
|
||||
TEST(StringRefTest, ConvertToString) {
|
||||
std::string s = StringRef("abc").to_string();
|
||||
EXPECT_EQ("abc", s);
|
||||
}
|
||||
|
||||
TEST(CStringRefTest, Ctor) {
|
||||
EXPECT_STREQ("abc", CStringRef("abc").c_str());
|
||||
EXPECT_STREQ("defg", CStringRef(std::string("defg")).c_str());
|
||||
}
|
||||
|
||||
class TestString {
|
||||
private:
|
||||
std::string value_;
|
||||
@@ -583,7 +602,7 @@ TEST(FormatterTest, ArgErrors) {
|
||||
template <int N>
|
||||
struct TestFormat {
|
||||
template <typename... Args>
|
||||
static std::string format(fmt::StringRef format_str, const Args & ... args) {
|
||||
static std::string format(fmt::CStringRef format_str, const Args & ... args) {
|
||||
return TestFormat<N - 1>::format(format_str, N - 1, args...);
|
||||
}
|
||||
};
|
||||
@@ -591,7 +610,7 @@ struct TestFormat {
|
||||
template <>
|
||||
struct TestFormat<0> {
|
||||
template <typename... Args>
|
||||
static std::string format(fmt::StringRef format_str, const Args & ... args) {
|
||||
static std::string format(fmt::CStringRef format_str, const Args & ... args) {
|
||||
return fmt::format(format_str, args...);
|
||||
}
|
||||
};
|
||||
@@ -1372,6 +1391,10 @@ TEST(FormatterTest, FormatStringRef) {
|
||||
EXPECT_EQ("test", format("{0}", StringRef("test")));
|
||||
}
|
||||
|
||||
TEST(FormatterTest, FormatCStringRef) {
|
||||
EXPECT_EQ("test", format("{0}", CStringRef("test")));
|
||||
}
|
||||
|
||||
TEST(FormatterTest, FormatUsingIOStreams) {
|
||||
EXPECT_EQ("a string", format("{0}", TestString("a string")));
|
||||
std::string s = format("The date is {0}", Date(2012, 12, 9));
|
||||
@@ -1440,19 +1463,6 @@ TEST(FormatterTest, FormatExamples) {
|
||||
}, error_code, "Cannot open file 'nonexistent'");
|
||||
}
|
||||
|
||||
TEST(StringRefTest, Ctor) {
|
||||
EXPECT_STREQ("abc", StringRef("abc").c_str());
|
||||
EXPECT_EQ(3u, StringRef("abc").size());
|
||||
|
||||
EXPECT_STREQ("defg", StringRef(std::string("defg")).c_str());
|
||||
EXPECT_EQ(4u, StringRef(std::string("defg")).size());
|
||||
}
|
||||
|
||||
TEST(StringRefTest, ConvertToString) {
|
||||
std::string s = StringRef("abc").to_string();
|
||||
EXPECT_EQ("abc", s);
|
||||
}
|
||||
|
||||
TEST(FormatterTest, Examples) {
|
||||
EXPECT_EQ("First, thou shalt count to three",
|
||||
format("First, thou shalt count to {0}", "three"));
|
||||
|
||||
@@ -207,7 +207,7 @@ int (test::fileno)(FILE *stream) {
|
||||
# define EXPECT_EQ_POSIX(expected, actual)
|
||||
#endif
|
||||
|
||||
void write_file(fmt::StringRef filename, fmt::StringRef content) {
|
||||
void write_file(fmt::CStringRef filename, fmt::StringRef content) {
|
||||
fmt::BufferedFile f(filename, "w");
|
||||
f.print("{}", content);
|
||||
}
|
||||
|
||||
@@ -64,7 +64,7 @@ File open_file() {
|
||||
// Attempts to write a string to a file.
|
||||
void write(File &f, fmt::StringRef s) {
|
||||
std::size_t num_chars_left = s.size();
|
||||
const char *ptr = s.c_str();
|
||||
const char *ptr = s.data();
|
||||
do {
|
||||
std::streamsize count = f.write(ptr, num_chars_left);
|
||||
ptr += count;
|
||||
|
||||
Reference in New Issue
Block a user