[libcxx-commits] [PATCH] D116381: [libc++][format] Fix precision parser conformance.

Mark de Wever via Phabricator via libcxx-commits libcxx-commits at lists.llvm.org
Wed Dec 29 10:57:13 PST 2021


Mordante created this revision.
Mordante added reviewers: ldionne, vitaut.
Mordante requested review of this revision.
Herald added a project: libc++.
Herald added a subscriber: libcxx-commits.
Herald added a reviewer: libc++.

@CaseyCarter reported the std-format-spec parser prohibits leading
zeros. 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.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D116381

Files:
  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


Index: libcxx/test/std/utilities/format/format.functions/format_tests.h
===================================================================
--- libcxx/test/std/utilities/format/format.functions/format_tests.h
+++ libcxx/test/std/utilities/format/format.functions/format_tests.h
@@ -175,11 +175,12 @@
   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 @@
 
   // Precision 0 allowed, but not useful for string arguments.
   check(STR("hello "), STR("hello {:.{}}"), world, 0);
+  // Precision may have leading zeros
+  check(STR("hello w"), STR("hello {:.00001}"), world);
   check_exception(
       "A format-spec arg-id replacement shouldn't have a negative value",
       STR("hello {:.{}}"), world, -1);
@@ -210,6 +213,10 @@
   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 '}'",
Index: libcxx/test/libcxx/utilities/format/format.string/format.string.std/std_format_spec_string.pass.cpp
===================================================================
--- libcxx/test/libcxx/utilities/format/format.string/format.string.std/std_format_spec_string.pass.cpp
+++ libcxx/test/libcxx/utilities/format/format.string/format.string.std/std_format_spec_string.pass.cpp
@@ -205,12 +205,6 @@
   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"));
Index: libcxx/include/__format/parser_std_format_spec.h
===================================================================
--- libcxx/include/__format/parser_std_format_spec.h
+++ libcxx/include/__format/parser_std_format_spec.h
@@ -363,17 +363,6 @@
     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);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D116381.396553.patch
Type: text/x-patch
Size: 3451 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libcxx-commits/attachments/20211229/80bf7299/attachment.bin>


More information about the libcxx-commits mailing list