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:
vitaut
2015-06-26 07:43:54 -07:00
parent 88c7c20102
commit 438bd9b0e6
7 changed files with 141 additions and 83 deletions

View File

@@ -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"));

View File

@@ -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);
}

View File

@@ -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;