Deprecate c_str() and str().

This commit is contained in:
Victor Zverovich 2014-06-30 06:43:53 -07:00
parent 90e6faffa0
commit e63a0ff125
5 changed files with 79 additions and 59 deletions

View File

@ -178,12 +178,12 @@ const uint64_t fmt::internal::POWERS_OF_10_64[] = {
void fmt::internal::ReportUnknownType(char code, const char *type) { void fmt::internal::ReportUnknownType(char code, const char *type) {
if (std::isprint(static_cast<unsigned char>(code))) { if (std::isprint(static_cast<unsigned char>(code))) {
throw fmt::FormatError(fmt::str( throw fmt::FormatError(
fmt::format("unknown format code '{}' for {}", code, type))); fmt::format("unknown format code '{}' for {}", code, type));
} }
throw fmt::FormatError( throw fmt::FormatError(
fmt::str(fmt::format("unknown format code '\\x{:02x}' for {}", fmt::format("unknown format code '\\x{:02x}' for {}",
static_cast<unsigned>(code), type))); static_cast<unsigned>(code), type));
} }
#ifdef _WIN32 #ifdef _WIN32
@ -569,11 +569,11 @@ void fmt::BasicWriter<Char>::FormatParser::CheckSign(
char sign = static_cast<char>(*s); char sign = static_cast<char>(*s);
if (arg.type > Arg::LAST_NUMERIC_TYPE) { if (arg.type > Arg::LAST_NUMERIC_TYPE) {
report_error_(s, report_error_(s,
fmt::c_str(fmt::format("format specifier '{}' requires numeric argument", sign))); fmt::format("format specifier '{}' requires numeric argument", sign).c_str());
} }
if (arg.type == Arg::UINT || arg.type == Arg::ULONG_LONG) { if (arg.type == Arg::UINT || arg.type == Arg::ULONG_LONG) {
report_error_(s, report_error_(s,
fmt::c_str(fmt::format("format specifier '{}' requires signed argument", sign))); fmt::format("format specifier '{}' requires signed argument", sign).c_str());
} }
++s; ++s;
} }

View File

@ -1695,32 +1695,52 @@ class BasicFormatter {
} }
}; };
template <typename Char>
FMT_DEPRECATED(std::basic_string<Char> str(const BasicWriter<Char> &f));
// This function is deprecated. Use BasicWriter::str() instead.
template <typename Char> template <typename Char>
inline std::basic_string<Char> str(const BasicWriter<Char> &f) { inline std::basic_string<Char> str(const BasicWriter<Char> &f) {
return f.str(); return f.str();
} }
template <typename Char>
FMT_DEPRECATED(const Char *c_str(const BasicWriter<Char> &f));
// This function is deprecated. Use BasicWriter::c_str() instead.
template <typename Char> template <typename Char>
inline const Char *c_str(const BasicWriter<Char> &f) { return f.c_str(); } inline const Char *c_str(const BasicWriter<Char> &f) { return f.c_str(); }
FMT_DEPRECATED(std::string str(StringRef s));
/** /**
Converts a string reference to `std::string`. Converts a string reference to `std::string`.
*/ */
// This function is deprecated. Use StringRef::c_str() instead.
inline std::string str(StringRef s) { inline std::string str(StringRef s) {
return std::string(s.c_str(), s.size()); return std::string(s.c_str(), s.size());
} }
FMT_DEPRECATED(const char *c_str(StringRef s));
/** /**
Returns the pointer to a C string. Returns the pointer to a C string.
*/ */
// This function is deprecated. Use StringRef::c_str() instead.
inline const char *c_str(StringRef s) { inline const char *c_str(StringRef s) {
return s.c_str(); return s.c_str();
} }
FMT_DEPRECATED(std::wstring str(WStringRef s));
// This function is deprecated. Use WStringRef::c_str() instead.
inline std::wstring str(WStringRef s) { inline std::wstring str(WStringRef s) {
return std::wstring(s.c_str(), s.size()); return std::wstring(s.c_str(), s.size());
} }
FMT_DEPRECATED(const wchar_t *c_str(WStringRef s));
// This function is deprecated. Use WStringRef::c_str() instead.
inline const wchar_t *c_str(WStringRef s) { inline const wchar_t *c_str(WStringRef s) {
return s.c_str(); return s.c_str();
} }

View File

@ -74,11 +74,11 @@ static ::testing::AssertionResult CheckWrite(const T &value, const char *type) {
std::basic_ostringstream<Char> os; std::basic_ostringstream<Char> os;
os << value; os << value;
std::basic_string<Char> expected = os.str(); std::basic_string<Char> expected = os.str();
std::basic_string<Char> actual = str(BasicWriter<Char>() << value); std::basic_string<Char> actual = (BasicWriter<Char>() << value).str();
if (expected == actual) if (expected == actual)
return ::testing::AssertionSuccess(); return ::testing::AssertionSuccess();
return ::testing::AssertionFailure() return ::testing::AssertionFailure()
<< "Value of: str(Writer<" << type << ">() << value)\n" << "Value of: (Writer<" << type << ">() << value).str()\n"
<< " Actual: " << actual << "\n" << " Actual: " << actual << "\n"
<< "Expected: " << expected << "\n"; << "Expected: " << expected << "\n";
} }
@ -421,25 +421,25 @@ TEST(WriterTest, WriteWideString) {
TEST(WriterTest, bin) { TEST(WriterTest, bin) {
using fmt::bin; using fmt::bin;
EXPECT_EQ("1100101011111110", str(Writer() << bin(0xcafe))); EXPECT_EQ("1100101011111110", (Writer() << bin(0xcafe)).str());
EXPECT_EQ("1011101010111110", str(Writer() << bin(0xbabeu))); EXPECT_EQ("1011101010111110", (Writer() << bin(0xbabeu)).str());
EXPECT_EQ("1101111010101101", str(Writer() << bin(0xdeadl))); EXPECT_EQ("1101111010101101", (Writer() << bin(0xdeadl)).str());
EXPECT_EQ("1011111011101111", str(Writer() << bin(0xbeeful))); EXPECT_EQ("1011111011101111", (Writer() << bin(0xbeeful)).str());
EXPECT_EQ("11001010111111101011101010111110", EXPECT_EQ("11001010111111101011101010111110",
str(Writer() << bin(0xcafebabell))); (Writer() << bin(0xcafebabell)).str());
EXPECT_EQ("11011110101011011011111011101111", EXPECT_EQ("11011110101011011011111011101111",
str(Writer() << bin(0xdeadbeefull))); (Writer() << bin(0xdeadbeefull)).str());
} }
TEST(WriterTest, oct) { TEST(WriterTest, oct) {
using fmt::oct; using fmt::oct;
EXPECT_EQ("12", str(Writer() << oct(static_cast<short>(012)))); EXPECT_EQ("12", (Writer() << oct(static_cast<short>(012))).str());
EXPECT_EQ("12", str(Writer() << oct(012))); EXPECT_EQ("12", (Writer() << oct(012)).str());
EXPECT_EQ("34", str(Writer() << oct(034u))); EXPECT_EQ("34", (Writer() << oct(034u)).str());
EXPECT_EQ("56", str(Writer() << oct(056l))); EXPECT_EQ("56", (Writer() << oct(056l)).str());
EXPECT_EQ("70", str(Writer() << oct(070ul))); EXPECT_EQ("70", (Writer() << oct(070ul)).str());
EXPECT_EQ("1234", str(Writer() << oct(01234ll))); EXPECT_EQ("1234", (Writer() << oct(01234ll)).str());
EXPECT_EQ("5670", str(Writer() << oct(05670ull))); EXPECT_EQ("5670", (Writer() << oct(05670ull)).str());
} }
TEST(WriterTest, hex) { TEST(WriterTest, hex) {
@ -449,22 +449,22 @@ TEST(WriterTest, hex) {
// This shouldn't compile: // This shouldn't compile:
//fmt::IntFormatSpec<short, fmt::TypeSpec<'x'> > (*phex2)(short value) = hex; //fmt::IntFormatSpec<short, fmt::TypeSpec<'x'> > (*phex2)(short value) = hex;
EXPECT_EQ("cafe", str(Writer() << hex(0xcafe))); EXPECT_EQ("cafe", (Writer() << hex(0xcafe)).str());
EXPECT_EQ("babe", str(Writer() << hex(0xbabeu))); EXPECT_EQ("babe", (Writer() << hex(0xbabeu)).str());
EXPECT_EQ("dead", str(Writer() << hex(0xdeadl))); EXPECT_EQ("dead", (Writer() << hex(0xdeadl)).str());
EXPECT_EQ("beef", str(Writer() << hex(0xbeeful))); EXPECT_EQ("beef", (Writer() << hex(0xbeeful)).str());
EXPECT_EQ("cafebabe", str(Writer() << hex(0xcafebabell))); EXPECT_EQ("cafebabe", (Writer() << hex(0xcafebabell)).str());
EXPECT_EQ("deadbeef", str(Writer() << hex(0xdeadbeefull))); EXPECT_EQ("deadbeef", (Writer() << hex(0xdeadbeefull)).str());
} }
TEST(WriterTest, hexu) { TEST(WriterTest, hexu) {
using fmt::hexu; using fmt::hexu;
EXPECT_EQ("CAFE", str(Writer() << hexu(0xcafe))); EXPECT_EQ("CAFE", (Writer() << hexu(0xcafe)).str());
EXPECT_EQ("BABE", str(Writer() << hexu(0xbabeu))); EXPECT_EQ("BABE", (Writer() << hexu(0xbabeu)).str());
EXPECT_EQ("DEAD", str(Writer() << hexu(0xdeadl))); EXPECT_EQ("DEAD", (Writer() << hexu(0xdeadl)).str());
EXPECT_EQ("BEEF", str(Writer() << hexu(0xbeeful))); EXPECT_EQ("BEEF", (Writer() << hexu(0xbeeful)).str());
EXPECT_EQ("CAFEBABE", str(Writer() << hexu(0xcafebabell))); EXPECT_EQ("CAFEBABE", (Writer() << hexu(0xcafebabell)).str());
EXPECT_EQ("DEADBEEF", str(Writer() << hexu(0xdeadbeefull))); EXPECT_EQ("DEADBEEF", (Writer() << hexu(0xdeadbeefull)).str());
} }
class Date { class Date {
@ -505,19 +505,19 @@ ISO8601DateFormatter iso8601(const Date &d) { return ISO8601DateFormatter(d); }
TEST(WriterTest, pad) { TEST(WriterTest, pad) {
using fmt::hex; using fmt::hex;
EXPECT_EQ(" cafe", str(Writer() << pad(hex(0xcafe), 8))); EXPECT_EQ(" cafe", (Writer() << pad(hex(0xcafe), 8)).str());
EXPECT_EQ(" babe", str(Writer() << pad(hex(0xbabeu), 8))); EXPECT_EQ(" babe", (Writer() << pad(hex(0xbabeu), 8)).str());
EXPECT_EQ(" dead", str(Writer() << pad(hex(0xdeadl), 8))); EXPECT_EQ(" dead", (Writer() << pad(hex(0xdeadl), 8)).str());
EXPECT_EQ(" beef", str(Writer() << pad(hex(0xbeeful), 8))); EXPECT_EQ(" beef", (Writer() << pad(hex(0xbeeful), 8)).str());
EXPECT_EQ(" dead", str(Writer() << pad(hex(0xdeadll), 8))); EXPECT_EQ(" dead", (Writer() << pad(hex(0xdeadll), 8)).str());
EXPECT_EQ(" beef", str(Writer() << pad(hex(0xbeefull), 8))); EXPECT_EQ(" beef", (Writer() << pad(hex(0xbeefull), 8)).str());
EXPECT_EQ(" 11", str(Writer() << pad(11, 7))); EXPECT_EQ(" 11", (Writer() << pad(11, 7)).str());
EXPECT_EQ(" 22", str(Writer() << pad(22u, 7))); EXPECT_EQ(" 22", (Writer() << pad(22u, 7)).str());
EXPECT_EQ(" 33", str(Writer() << pad(33l, 7))); EXPECT_EQ(" 33", (Writer() << pad(33l, 7)).str());
EXPECT_EQ(" 44", str(Writer() << pad(44ul, 7))); EXPECT_EQ(" 44", (Writer() << pad(44ul, 7)).str());
EXPECT_EQ(" 33", str(Writer() << pad(33ll, 7))); EXPECT_EQ(" 33", (Writer() << pad(33ll, 7)).str());
EXPECT_EQ(" 44", str(Writer() << pad(44ull, 7))); EXPECT_EQ(" 44", (Writer() << pad(44ull, 7)).str());
BasicWriter<char> w; BasicWriter<char> w;
w.clear(); w.clear();
@ -532,21 +532,21 @@ TEST(WriterTest, pad) {
} }
TEST(WriterTest, PadString) { TEST(WriterTest, PadString) {
EXPECT_EQ("test ", str(Writer() << pad("test", 8))); EXPECT_EQ("test ", (Writer() << pad("test", 8)).str());
EXPECT_EQ("test******", str(Writer() << pad("test", 10, '*'))); EXPECT_EQ("test******", (Writer() << pad("test", 10, '*')).str());
} }
TEST(WriterTest, PadWString) { TEST(WriterTest, PadWString) {
EXPECT_EQ(L"test ", str(WWriter() << pad(L"test", 8))); EXPECT_EQ(L"test ", (WWriter() << pad(L"test", 8)).str());
EXPECT_EQ(L"test******", str(WWriter() << pad(L"test", 10, '*'))); EXPECT_EQ(L"test******", (WWriter() << pad(L"test", 10, '*')).str());
EXPECT_EQ(L"test******", str(WWriter() << pad(L"test", 10, L'*'))); EXPECT_EQ(L"test******", (WWriter() << pad(L"test", 10, L'*')).str());
} }
TEST(WriterTest, NoConflictWithIOManip) { TEST(WriterTest, NoConflictWithIOManip) {
using namespace std; using namespace std;
using namespace fmt; using namespace fmt;
EXPECT_EQ("cafe", str(Writer() << hex(0xcafe))); EXPECT_EQ("cafe", (Writer() << hex(0xcafe)).str());
EXPECT_EQ("12", str(Writer() << oct(012))); EXPECT_EQ("12", (Writer() << oct(012)).str());
} }
TEST(WriterTest, Format) { TEST(WriterTest, Format) {
@ -564,7 +564,7 @@ TEST(WriterTest, Format) {
} }
TEST(WriterTest, WWriter) { TEST(WriterTest, WWriter) {
EXPECT_EQ(L"cafe", str(fmt::WWriter() << fmt::hex(0xcafe))); EXPECT_EQ(L"cafe", (fmt::WWriter() << fmt::hex(0xcafe)).str());
} }
TEST(FormatterTest, Escape) { TEST(FormatterTest, Escape) {
@ -1341,7 +1341,7 @@ TEST(FormatterTest, FormatStringFromSpeedTest) {
TEST(FormatterTest, FormatExamples) { TEST(FormatterTest, FormatExamples) {
using fmt::hex; using fmt::hex;
EXPECT_EQ("0000cafe", str(BasicWriter<char>() << pad(hex(0xcafe), 8, '0'))); EXPECT_EQ("0000cafe", (Writer() << pad(hex(0xcafe), 8, '0')).str());
std::string message = format("The answer is {}", 42); std::string message = format("The answer is {}", 42);
EXPECT_EQ("The answer is 42", message); EXPECT_EQ("The answer is 42", message);
@ -1597,7 +1597,7 @@ TEST(FormatTest, Variadic) {
template <typename T> template <typename T>
std::string str(const T &value) { std::string str(const T &value) {
return fmt::str(fmt::format("{0}", value)); return fmt::format("{}", value);
} }
TEST(StrTest, Convert) { TEST(StrTest, Convert) {

View File

@ -94,5 +94,5 @@ std::string OutputRedirect::RestoreAndRead() {
std::string FormatSystemErrorMessage(int error_code, fmt::StringRef message) { std::string FormatSystemErrorMessage(int error_code, fmt::StringRef message) {
fmt::Writer out; fmt::Writer out;
fmt::internal::FormatSystemErrorMessage(out, error_code, message); fmt::internal::FormatSystemErrorMessage(out, error_code, message);
return str(out); return out.str();
} }

View File

@ -184,7 +184,7 @@ void CheckErrorSink(int error_code, FormatErrorMessage format) {
} }
fmt::Writer message; fmt::Writer message;
format(message, error_code, "test"); format(message, error_code, "test");
EXPECT_EQ(str(message), error.what()); EXPECT_EQ(message.str(), error.what());
EXPECT_EQ(error_code, error.error_code()); EXPECT_EQ(error_code, error.error_code());
} }
@ -199,7 +199,7 @@ void CheckThrowError(int error_code, FormatErrorMessage format,
} }
fmt::Writer message; fmt::Writer message;
format(message, error_code, "test error"); format(message, error_code, "test error");
EXPECT_EQ(str(message), error.what()); EXPECT_EQ(message.str(), error.what());
EXPECT_EQ(error_code, error.error_code()); EXPECT_EQ(error_code, error.error_code());
} }
@ -207,7 +207,7 @@ TEST(UtilTest, FormatSystemErrorMessage) {
fmt::Writer message; fmt::Writer message;
fmt::internal::FormatSystemErrorMessage(message, EDOM, "test"); fmt::internal::FormatSystemErrorMessage(message, EDOM, "test");
EXPECT_EQ(fmt::format("test: {}", EXPECT_EQ(fmt::format("test: {}",
GetSystemErrorMessage(EDOM)), fmt::str(message)); GetSystemErrorMessage(EDOM)), message.str());
} }
TEST(UtilTest, SystemErrorSink) { TEST(UtilTest, SystemErrorSink) {