diff options
author | Wyatt Hepler <hepler@google.com> | 2020-01-23 17:40:10 -0800 |
---|---|---|
committer | CQ Bot Account <commit-bot@chromium.org> | 2020-01-24 22:53:22 +0000 |
commit | 2596fe5418844863402b8d94e0c52394d608bfaa (patch) | |
tree | abcd19ad00acc28f77f5882654a5781945d149a9 /pw_string | |
parent | bd4f215f87c523b20d8d16ff8da754904504db8a (diff) | |
download | pigweed-2596fe5418844863402b8d94e0c52394d608bfaa.tar.gz |
pw_string: Rename va_list Format to FormatVaList
On Windows, sometimes the va_list overload would be used when formatting
with a single argument. To avoid potential conflicts like these, the
va_list versions of string::Format and StringBuilder::Format are renamed
to FormatVaList.
Change-Id: Id29fae21b13ed420b13841a479e3748a1d3b91ce
Diffstat (limited to 'pw_string')
-rw-r--r-- | pw_string/docs.rst | 7 | ||||
-rw-r--r-- | pw_string/format.cc | 8 | ||||
-rw-r--r-- | pw_string/format_test.cc | 4 | ||||
-rw-r--r-- | pw_string/public/pw_string/format.h | 6 | ||||
-rw-r--r-- | pw_string/public/pw_string/string_builder.h | 2 | ||||
-rw-r--r-- | pw_string/size_report/format_single.cc | 3 | ||||
-rw-r--r-- | pw_string/string_builder.cc | 7 |
7 files changed, 19 insertions, 18 deletions
diff --git a/pw_string/docs.rst b/pw_string/docs.rst index a47b08fe9..67ee9a64d 100644 --- a/pw_string/docs.rst +++ b/pw_string/docs.rst @@ -26,9 +26,10 @@ Features pw::string::Format ------------------ -The ``pw::string::Format`` functions provides are safer alternatives to -``std::snprintf`` and ``std::vsnprintf``. The snprintf return value is awkward -to interpret, and misinterpreting it can lead to serious bugs. +The ``pw::string::Format`` and ``pw::string::FormatVaList`` functions provide +safer alternatives to ``std::snprintf`` and ``std::vsnprintf``. The snprintf +return value is awkward to interpret, and misinterpreting it can lead to serious +bugs. Size report: replacing snprintf with pw::string::Format ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/pw_string/format.cc b/pw_string/format.cc index a0f2b17d2..dc83f1d45 100644 --- a/pw_string/format.cc +++ b/pw_string/format.cc @@ -21,15 +21,15 @@ namespace pw::string { StatusWithSize Format(const span<char>& buffer, const char* format, ...) { va_list args; va_start(args, format); - const StatusWithSize result = Format(buffer, format, args); + const StatusWithSize result = FormatVaList(buffer, format, args); va_end(args); return result; } -StatusWithSize Format(const span<char>& buffer, - const char* format, - va_list args) { +StatusWithSize FormatVaList(const span<char>& buffer, + const char* format, + va_list args) { if (buffer.empty()) { return StatusWithSize(Status::RESOURCE_EXHAUSTED, 0); } diff --git a/pw_string/format_test.cc b/pw_string/format_test.cc index 6344ad50b..1c44c4eaa 100644 --- a/pw_string/format_test.cc +++ b/pw_string/format_test.cc @@ -76,7 +76,7 @@ TEST(Format, FormatLargerThanBuffer_ReturnsResourceExhausted) { TEST(Format, ArgumentLargerThanBuffer_ReturnsResourceExhausted) { char buffer[5]; - auto result = Format(buffer, "%s%c", "2big", '!'); + auto result = Format(buffer, "%s", "2big!"); EXPECT_EQ(Status::RESOURCE_EXHAUSTED, result.status()); EXPECT_EQ(4u, result.size()); @@ -87,7 +87,7 @@ StatusWithSize CallFormatWithVaList(span<char> buffer, const char* fmt, ...) { va_list args; va_start(args, fmt); - StatusWithSize result = Format(buffer, fmt, args); + StatusWithSize result = FormatVaList(buffer, fmt, args); va_end(args); return result; diff --git a/pw_string/public/pw_string/format.h b/pw_string/public/pw_string/format.h index efa71f153..72198bf3f 100644 --- a/pw_string/public/pw_string/format.h +++ b/pw_string/public/pw_string/format.h @@ -45,8 +45,8 @@ StatusWithSize Format(const span<char>& buffer, const char* format, ...); // Writes a printf-style formatted string with va_list-packed arguments to the // provided buffer, similarly to std::vsnprintf. The return value is the same as // above. -StatusWithSize Format(const span<char>& buffer, - const char* format, - va_list args); +StatusWithSize FormatVaList(const span<char>& buffer, + const char* format, + va_list args); } // namespace pw::string diff --git a/pw_string/public/pw_string/string_builder.h b/pw_string/public/pw_string/string_builder.h index e3a1db905..d2397e48d 100644 --- a/pw_string/public/pw_string/string_builder.h +++ b/pw_string/public/pw_string/string_builder.h @@ -232,7 +232,7 @@ class StringBuilder { // truncated and the status is set to RESOURCE_EXHAUSTED. // // Internally, calls string::Format, which calls std::vsnprintf. - StringBuilder& Format(const char* format, va_list args); + StringBuilder& FormatVaList(const char* format, va_list args); // Sets the StringBuilder's size. This function only truncates; if // new_size > size(), it sets status to OUT_OF_RANGE and does nothing. diff --git a/pw_string/size_report/format_single.cc b/pw_string/size_report/format_single.cc index 761029770..3982a1c09 100644 --- a/pw_string/size_report/format_single.cc +++ b/pw_string/size_report/format_single.cc @@ -40,8 +40,7 @@ unsigned OutputStringsToBuffer() { #if USE_FORMAT // The code for using pw::string::Format is much simpler and safer. - return pw::string::Format( - span(buffer, buffer_size), "hello %s %d", get_buffer, get_size) + return Format(span(buffer, buffer_size), "hello %s %d", get_buffer, get_size) .size(); #else // std::snprintf if (buffer_size == 0u) { diff --git a/pw_string/string_builder.cc b/pw_string/string_builder.cc index 2c0be3736..b433e7f31 100644 --- a/pw_string/string_builder.cc +++ b/pw_string/string_builder.cc @@ -88,14 +88,15 @@ void StringBuilder::resize(size_t new_size) { StringBuilder& StringBuilder::Format(const char* format, ...) { va_list args; va_start(args, format); - Format(format, args); + FormatVaList(format, args); va_end(args); return *this; } -StringBuilder& StringBuilder::Format(const char* format, va_list args) { - HandleStatusWithSize(string::Format(buffer_.subspan(size_), format, args)); +StringBuilder& StringBuilder::FormatVaList(const char* format, va_list args) { + HandleStatusWithSize( + string::FormatVaList(buffer_.subspan(size_), format, args)); return *this; } |