[libcxx-commits] [libcxx] f2b40ba - [libc++][format] Fix precision parser conformance.

Mark de Wever via libcxx-commits libcxx-commits at lists.llvm.org
Wed Jan 5 08:41:41 PST 2022


Author: Mark de Wever
Date: 2022-01-05T17:41:36+01:00
New Revision: f2b40ba40004bfb70d90e9787ad8c9ab6d85646b

URL: https://github.com/llvm/llvm-project/commit/f2b40ba40004bfb70d90e9787ad8c9ab6d85646b
DIFF: https://github.com/llvm/llvm-project/commit/f2b40ba40004bfb70d90e9787ad8c9ab6d85646b.diff

LOG: [libc++][format] Fix precision parser conformance.

@CaseyCarter reported that the tests for the std-format-spec rejects leading
zeroes for precision, which the Standard does not require. The Standard allows
them. Only for precision, not for the width or an arg-id.

Fixes the precision parser and adds some test for the arg-id since they
were missing.

Reviewed By: ldionne, #libc

Differential Revision: https://reviews.llvm.org/D116381

Added: 
    

Modified: 
    libcxx/include/__format/parser_std_format_spec.h
    libcxx/test/libcxx/utilities/format/format.string/format.string.std/std_format_spec_string.pass.cpp
    libcxx/test/std/utilities/format/format.functions/format_tests.h

Removed: 
    


################################################################################
diff  --git a/libcxx/include/__format/parser_std_format_spec.h b/libcxx/include/__format/parser_std_format_spec.h
index 9b713b8114846..5ab6b423fce50 100644
--- a/libcxx/include/__format/parser_std_format_spec.h
+++ b/libcxx/include/__format/parser_std_format_spec.h
@@ -363,17 +363,6 @@ class _LIBCPP_TYPE_VIS __parser_precision {
     if (__begin == __end)
       __throw_format_error("End of input while parsing format-spec precision");
 
-    if (*__begin == _CharT('0')) {
-      ++__begin;
-      if (__begin != __end && *__begin >= '0' && *__begin <= '9')
-        __throw_format_error(
-            "A format-spec precision field shouldn't have a leading zero");
-
-      __precision = 0;
-      __precision_as_arg = 0;
-      return __begin;
-    }
-
     if (*__begin == _CharT('{')) {
       __format::__parse_number_result __arg_id =
           __parse_arg_id(++__begin, __end, __parse_ctx);

diff  --git a/libcxx/test/libcxx/utilities/format/format.string/format.string.std/std_format_spec_string.pass.cpp b/libcxx/test/libcxx/utilities/format/format.string/format.string.std/std_format_spec_string.pass.cpp
index b3bcecffb3f36..e7bccbb27cce9 100644
--- a/libcxx/test/libcxx/utilities/format/format.string/format.string.std/std_format_spec_string.pass.cpp
+++ b/libcxx/test/libcxx/utilities/format/format.string/format.string.std/std_format_spec_string.pass.cpp
@@ -205,12 +205,6 @@ constexpr void test() {
   test({.precision = 0, .precision_as_arg = true}, 4, CSTR(".{0}}"));
   test({.precision = 1, .precision_as_arg = true}, 4, CSTR(".{1}}"));
 
-  test_exception<Parser<CharT>>(
-      "A format-spec precision field shouldn't have a leading zero",
-      CSTR(".00"));
-  test_exception<Parser<CharT>>(
-      "A format-spec precision field shouldn't have a leading zero",
-      CSTR(".01"));
   test_exception<Parser<CharT>>(
       "The format-spec precision field doesn't contain a value or arg-id",
       CSTR(".a"));

diff  --git a/libcxx/test/std/utilities/format/format.functions/format_tests.h b/libcxx/test/std/utilities/format/format.functions/format_tests.h
index ca73dbb71dc8a..1d19719fa1ff1 100644
--- a/libcxx/test/std/utilities/format/format.functions/format_tests.h
+++ b/libcxx/test/std/utilities/format/format.functions/format_tests.h
@@ -175,11 +175,12 @@ void format_test_string(T world, T universe, TestFunction check,
   check_exception(
       "Using automatic argument numbering in manual argument numbering mode",
       STR("hello {0:{}}"), world, 1);
+  // Arg-id may not have leading zeros.
+  check_exception(
+      "A format-spec arg-id should terminate at a '}'",
+      STR("hello {0:{01}}"), world, 1);
 
   // *** precision ***
-  check_exception("A format-spec precision field shouldn't have a leading zero",
-                  STR("hello {:.01}"), world);
-
 #if _LIBCPP_VERSION
   // This limit isn't specified in the Standard.
   static_assert(std::__format::__number_max == 2'147'483'647,
@@ -194,6 +195,8 @@ void format_test_string(T world, T universe, TestFunction check,
 
   // Precision 0 allowed, but not useful for string arguments.
   check(STR("hello "), STR("hello {:.{}}"), world, 0);
+  // Precision may have leading zeros. Secondly tests the value is still base 10.
+  check(STR("hello 0123456789"), STR("hello {:.000010}"), STR("0123456789abcdef"));
   check_exception(
       "A format-spec arg-id replacement shouldn't have a negative value",
       STR("hello {:.{}}"), world, -1);
@@ -210,6 +213,10 @@ void format_test_string(T world, T universe, TestFunction check,
   check_exception(
       "Using automatic argument numbering in manual argument numbering mode",
       STR("hello {0:.{}}"), world, 1);
+  // Arg-id may not have leading zeros.
+  check_exception(
+      "A format-spec arg-id should terminate at a '}'",
+      STR("hello {0:.{01}}"), world, 1);
 
   // *** locale-specific form ***
   check_exception("The format-spec should consume the input or end with a '}'",


        


More information about the libcxx-commits mailing list