From b615b2d77bdaefa0165412f597351d0e00eaeb42 Mon Sep 17 00:00:00 2001 From: Charles Date: Thu, 18 Feb 2021 17:54:30 -0800 Subject: [PATCH 1/4] add tests for various escaped curlies and simple text. --- stl/inc/format | 3 +- .../test.cpp | 68 ++++++++++++++++++- 2 files changed, 67 insertions(+), 4 deletions(-) diff --git a/stl/inc/format b/stl/inc/format index 65b9145847a..a40e2b6a3d9 100644 --- a/stl/inc/format +++ b/stl/inc/format @@ -586,7 +586,8 @@ constexpr void _Parse_format_string(basic_string_view<_CharT> _Format_str, _Hand // In this case we didn't find either a closing curl or opening curl. // Write the whole thing out. if (_ClosingCurl == _OpeningCurl) { - return _Handler._On_text(_Begin, _OpeningCurl); + _Handler._On_text(_Begin, _OpeningCurl); + break; } // We know _ClosingCurl isn't past the end because // the above condition was not met. diff --git a/tests/std/tests/P0645R10_text_formatting_formatting/test.cpp b/tests/std/tests/P0645R10_text_formatting_formatting/test.cpp index 3c0f0d9173d..b8eddf3d42b 100644 --- a/tests/std/tests/P0645R10_text_formatting_formatting/test.cpp +++ b/tests/std/tests/P0645R10_text_formatting_formatting/test.cpp @@ -8,9 +8,71 @@ #include #include +using namespace std; + // TODO: fill in tests -template std::back_insert_iterator std::vformat_to(std::back_insert_iterator, - const std::locale&, std::string_view, std::format_args_t, char>); +template back_insert_iterator std::vformat_to( + back_insert_iterator, const locale&, string_view, format_args_t, char>); + + +int main() { + string output_string = ""; + + vformat_to(back_insert_iterator(output_string), locale::classic(), "f", make_format_args()); + assert(output_string == "f"); + + output_string.clear(); + vformat_to(back_insert_iterator(output_string), locale::classic(), "format", make_format_args()); + assert(output_string == "format"); + + // test escaped opening curlies + output_string.clear(); + vformat_to(back_insert_iterator(output_string), locale::classic(), "{{", make_format_args()); + assert(output_string == "{"); + + output_string.clear(); + vformat_to(back_insert_iterator(output_string), locale::classic(), "{{{{", make_format_args()); + assert(output_string == "{{"); + + output_string.clear(); + vformat_to(back_insert_iterator(output_string), locale::classic(), "x{{", make_format_args()); + assert(output_string == "x{"); + + output_string.clear(); + vformat_to(back_insert_iterator(output_string), locale::classic(), "{{ {{", make_format_args()); + assert(output_string == "{ {"); + + output_string.clear(); + vformat_to(back_insert_iterator(output_string), locale::classic(), "x{{x", make_format_args()); + assert(output_string == "x{x"); + + output_string.clear(); + vformat_to(back_insert_iterator(output_string), locale::classic(), "{{x", make_format_args()); + assert(output_string == "{x"); + + // tests escaped closing curlies + output_string.clear(); + vformat_to(back_insert_iterator(output_string), locale::classic(), "}}", make_format_args()); + assert(output_string == "}"); + + output_string.clear(); + vformat_to(back_insert_iterator(output_string), locale::classic(), "}}}}", make_format_args()); + assert(output_string == "}}"); + + output_string.clear(); + vformat_to(back_insert_iterator(output_string), locale::classic(), "x}}", make_format_args()); + assert(output_string == "x}"); + + output_string.clear(); + vformat_to(back_insert_iterator(output_string), locale::classic(), "}} }}", make_format_args()); + assert(output_string == "} }"); + output_string.clear(); + vformat_to(back_insert_iterator(output_string), locale::classic(), "x}}x", make_format_args()); + assert(output_string == "x}x"); -int main() {} + output_string.clear(); + vformat_to(back_insert_iterator(output_string), locale::classic(), "}}x", make_format_args()); + assert(output_string == "}x"); + return 0; +} From c525eb522d53b93083052595467d05b5782c62ee Mon Sep 17 00:00:00 2001 From: Charles Date: Thu, 18 Feb 2021 17:56:01 -0800 Subject: [PATCH 2/4] whitespace --- tests/std/tests/P0645R10_text_formatting_formatting/test.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/std/tests/P0645R10_text_formatting_formatting/test.cpp b/tests/std/tests/P0645R10_text_formatting_formatting/test.cpp index b8eddf3d42b..b87fab4a2db 100644 --- a/tests/std/tests/P0645R10_text_formatting_formatting/test.cpp +++ b/tests/std/tests/P0645R10_text_formatting_formatting/test.cpp @@ -74,5 +74,6 @@ int main() { output_string.clear(); vformat_to(back_insert_iterator(output_string), locale::classic(), "}}x", make_format_args()); assert(output_string == "}x"); + return 0; } From 57f5538d844a1ba5ed96bbb7a1591ac695b0dd3a Mon Sep 17 00:00:00 2001 From: Charles Date: Fri, 19 Feb 2021 12:25:04 -0800 Subject: [PATCH 3/4] mega nitpicks --- stl/inc/format | 2 +- tests/std/tests/P0645R10_text_formatting_formatting/test.cpp | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/stl/inc/format b/stl/inc/format index a40e2b6a3d9..fb0a76b7290 100644 --- a/stl/inc/format +++ b/stl/inc/format @@ -579,7 +579,7 @@ constexpr void _Parse_format_string(basic_string_view<_CharT> _Format_str, _Hand const _CharT* _OpeningCurl = _Begin; if (*_Begin != '{') { // we didn't start at an opening curl, find the next one - _OpeningCurl = _STD find(_Begin + 1, _End, '{'); + _OpeningCurl = _Find_unchecked(_Begin + 1, _End, '{'); for (;;) { const _CharT* _ClosingCurl = _Find_unchecked(_Begin, _OpeningCurl, '}'); diff --git a/tests/std/tests/P0645R10_text_formatting_formatting/test.cpp b/tests/std/tests/P0645R10_text_formatting_formatting/test.cpp index b87fab4a2db..1c97b8de8ae 100644 --- a/tests/std/tests/P0645R10_text_formatting_formatting/test.cpp +++ b/tests/std/tests/P0645R10_text_formatting_formatting/test.cpp @@ -25,7 +25,7 @@ int main() { vformat_to(back_insert_iterator(output_string), locale::classic(), "format", make_format_args()); assert(output_string == "format"); - // test escaped opening curlies + // test escaped opening curls output_string.clear(); vformat_to(back_insert_iterator(output_string), locale::classic(), "{{", make_format_args()); assert(output_string == "{"); @@ -50,7 +50,7 @@ int main() { vformat_to(back_insert_iterator(output_string), locale::classic(), "{{x", make_format_args()); assert(output_string == "{x"); - // tests escaped closing curlies + // tests escaped closing curls output_string.clear(); vformat_to(back_insert_iterator(output_string), locale::classic(), "}}", make_format_args()); assert(output_string == "}"); From 6dd2e9573ed25a784558d6ce56d080b4d8dbd769 Mon Sep 17 00:00:00 2001 From: Charlie Barto Date: Fri, 19 Feb 2021 12:25:35 -0800 Subject: [PATCH 4/4] better comment in _Parse_format_string Co-authored-by: Casey Carter --- stl/inc/format | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stl/inc/format b/stl/inc/format index fb0a76b7290..bf09d0572f2 100644 --- a/stl/inc/format +++ b/stl/inc/format @@ -583,7 +583,7 @@ constexpr void _Parse_format_string(basic_string_view<_CharT> _Format_str, _Hand for (;;) { const _CharT* _ClosingCurl = _Find_unchecked(_Begin, _OpeningCurl, '}'); - // In this case we didn't find either a closing curl or opening curl. + // In this case there are neither closing nor opening curls in [_Begin, _OpenCurl) // Write the whole thing out. if (_ClosingCurl == _OpeningCurl) { _Handler._On_text(_Begin, _OpeningCurl);