aboutsummaryrefslogtreecommitdiff
path: root/libcxx/include/__format/parser_std_format_spec.h
diff options
context:
space:
mode:
authorDimitry Andric <dim@FreeBSD.org>2022-01-27 22:06:42 +0000
committerDimitry Andric <dim@FreeBSD.org>2022-01-27 22:06:42 +0000
commit6f8fc217eaa12bf657be1c6468ed9938d10168b3 (patch)
treea1fd89b864d9b93e2ad68fe1dcf7afee2e3c8d76 /libcxx/include/__format/parser_std_format_spec.h
parent77fc4c146f0870ffb09c1afb823ccbe742c5e6ff (diff)
downloadsrc-6f8fc217eaa12bf657be1c6468ed9938d10168b3.tar.gz
src-6f8fc217eaa12bf657be1c6468ed9938d10168b3.zip
Vendor import of llvm-project main llvmorg-14-init-17616-g024a1fab5c35.vendor/llvm-project/llvmorg-14-init-17616-g024a1fab5c35
Diffstat (limited to 'libcxx/include/__format/parser_std_format_spec.h')
-rw-r--r--libcxx/include/__format/parser_std_format_spec.h274
1 files changed, 244 insertions, 30 deletions
diff --git a/libcxx/include/__format/parser_std_format_spec.h b/libcxx/include/__format/parser_std_format_spec.h
index 9b713b811484..9d893e9ced27 100644
--- a/libcxx/include/__format/parser_std_format_spec.h
+++ b/libcxx/include/__format/parser_std_format_spec.h
@@ -214,7 +214,7 @@ __parse_arg_id(const _CharT* __begin, const _CharT* __end, auto& __parse_ctx) {
__format::__parse_arg_id(__begin, __end, __parse_ctx);
if (__r.__ptr == __end || *__r.__ptr != _CharT('}'))
- __throw_format_error("A format-spec arg-id should terminate at a '}'");
+ __throw_format_error("Invalid arg-id");
++__r.__ptr;
return __r;
@@ -363,17 +363,6 @@ protected:
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);
@@ -487,6 +476,21 @@ __parse_type(const _CharT* __begin, _Flags& __flags) {
}
/**
+ * Process the parsed alignment and zero-padding state of arithmetic types.
+ *
+ * [format.string.std]/13
+ * If the 0 character and an align option both appear, the 0 character is
+ * ignored.
+ *
+ * For the formatter a @ref __default alignment means zero-padding.
+ */
+_LIBCPP_HIDE_FROM_ABI constexpr void __process_arithmetic_alignment(_Flags& __flags) {
+ __flags.__zero_padding &= __flags.__alignment == _Flags::_Alignment::__default;
+ if (!__flags.__zero_padding && __flags.__alignment == _Flags::_Alignment::__default)
+ __flags.__alignment = _Flags::_Alignment::__right;
+}
+
+/**
* The parser for the std-format-spec.
*
* [format.string.std]/1 specifies the std-format-spec:
@@ -659,23 +663,9 @@ protected:
return __begin;
}
- /**
- * Handles the post-parsing updates for the integer types.
- *
- * Updates the zero-padding and alignment for integer types.
- *
- * [format.string.std]/13
- * If the 0 character and an align option both appear, the 0 character is
- * ignored.
- *
- * For the formatter a @ref __default alignment means zero-padding. Update
- * the alignment based on parsed format string.
- */
+ /** Handles the post-parsing updates for the integer types. */
_LIBCPP_HIDE_FROM_ABI constexpr void __handle_integer() noexcept {
- this->__zero_padding &= this->__alignment == _Flags::_Alignment::__default;
- if (!this->__zero_padding &&
- this->__alignment == _Flags::_Alignment::__default)
- this->__alignment = _Flags::_Alignment::__right;
+ __process_arithmetic_alignment(static_cast<_Flags&>(*this));
}
/**
@@ -712,8 +702,232 @@ protected:
}
};
-// TODO FMT Add a parser for floating-point values.
-// TODO FMT Add a parser for pointer values.
+/**
+ * The parser for the std-format-spec.
+ *
+ * This implements the parser for the floating-point types.
+ *
+ * See @ref __parser_string.
+ */
+template <class _CharT>
+class _LIBCPP_TEMPLATE_VIS __parser_floating_point
+ : public __parser_width, // provides __width(|as_arg)
+ public __parser_precision, // provides __precision(|as_arg)
+ public __parser_fill_align<_CharT>, // provides __fill and uses __flags
+ public _Flags // provides __flags
+{
+public:
+ using char_type = _CharT;
+
+ /**
+ * The low-level std-format-spec parse function.
+ *
+ * @pre __begin points at the beginning of the std-format-spec. This means
+ * directly after the ':'.
+ * @pre The std-format-spec parses the entire input, or the first unmatched
+ * character is a '}'.
+ *
+ * @returns The iterator pointing at the last parsed character.
+ */
+ _LIBCPP_HIDE_FROM_ABI constexpr auto parse(auto& __parse_ctx)
+ -> decltype(__parse_ctx.begin()) {
+ auto __it = __parse(__parse_ctx);
+ __process_arithmetic_alignment(static_cast<_Flags&>(*this));
+ __process_display_type();
+ return __it;
+ }
+protected:
+ /**
+ * The low-level std-format-spec parse function.
+ *
+ * @pre __begin points at the beginning of the std-format-spec. This means
+ * directly after the ':'.
+ * @pre The std-format-spec parses the entire input, or the first unmatched
+ * character is a '}'.
+ *
+ * @returns The iterator pointing at the last parsed character.
+ */
+ _LIBCPP_HIDE_FROM_ABI constexpr auto __parse(auto& __parse_ctx)
+ -> decltype(__parse_ctx.begin()) {
+ auto __begin = __parse_ctx.begin();
+ auto __end = __parse_ctx.end();
+ if (__begin == __end)
+ return __begin;
+
+ __begin = __parser_fill_align<_CharT>::__parse(__begin, __end,
+ static_cast<_Flags&>(*this));
+ if (__begin == __end)
+ return __begin;
+
+ __begin = __parse_sign(__begin, static_cast<_Flags&>(*this));
+ if (__begin == __end)
+ return __begin;
+
+ __begin = __parse_alternate_form(__begin, static_cast<_Flags&>(*this));
+ if (__begin == __end)
+ return __begin;
+
+ __begin = __parse_zero_padding(__begin, static_cast<_Flags&>(*this));
+ if (__begin == __end)
+ return __begin;
+
+ __begin = __parser_width::__parse(__begin, __end, __parse_ctx);
+ if (__begin == __end)
+ return __begin;
+
+ __begin = __parser_precision::__parse(__begin, __end, __parse_ctx);
+ if (__begin == __end)
+ return __begin;
+
+ __begin =
+ __parse_locale_specific_form(__begin, static_cast<_Flags&>(*this));
+ if (__begin == __end)
+ return __begin;
+
+ __begin = __parse_type(__begin, static_cast<_Flags&>(*this));
+
+ if (__begin != __end && *__begin != _CharT('}'))
+ __throw_format_error(
+ "The format-spec should consume the input or end with a '}'");
+
+ return __begin;
+ }
+
+ /** Processes the parsed std-format-spec based on the parsed display type. */
+ _LIBCPP_HIDE_FROM_ABI constexpr void __process_display_type() {
+ switch (this->__type) {
+ case _Flags::_Type::__default:
+ // When no precision specified then it keeps default since that
+ // formatting differs from the other types.
+ if (this->__has_precision_field())
+ this->__type = _Flags::_Type::__general_lower_case;
+ break;
+ case _Flags::_Type::__float_hexadecimal_lower_case:
+ case _Flags::_Type::__float_hexadecimal_upper_case:
+ // Precision specific behavior will be handled later.
+ break;
+ case _Flags::_Type::__scientific_lower_case:
+ case _Flags::_Type::__scientific_upper_case:
+ case _Flags::_Type::__fixed_lower_case:
+ case _Flags::_Type::__fixed_upper_case:
+ case _Flags::_Type::__general_lower_case:
+ case _Flags::_Type::__general_upper_case:
+ if (!this->__has_precision_field()) {
+ // Set the default precision for the call to to_chars.
+ this->__precision = 6;
+ this->__precision_as_arg = false;
+ }
+ break;
+
+ default:
+ __throw_format_error("The format-spec type has a type not supported for "
+ "a floating-point argument");
+ }
+ }
+};
+
+/**
+ * The parser for the std-format-spec.
+ *
+ * This implements the parser for the pointer types.
+ *
+ * See @ref __parser_string.
+ */
+template <class _CharT>
+class _LIBCPP_TEMPLATE_VIS __parser_pointer : public __parser_width, // provides __width(|as_arg)
+ public __parser_fill_align<_CharT>, // provides __fill and uses __flags
+ public _Flags // provides __flags
+{
+public:
+ using char_type = _CharT;
+
+ _LIBCPP_HIDE_FROM_ABI constexpr __parser_pointer() {
+ // Implements LWG3612 Inconsistent pointer alignment in std::format.
+ // The issue's current status is "Tentatively Ready" and libc++ status is
+ // still experimental.
+ //
+ // TODO FMT Validate this with the final resolution of LWG3612.
+ this->__alignment = _Flags::_Alignment::__right;
+ }
+
+ /**
+ * The low-level std-format-spec parse function.
+ *
+ * @pre __begin points at the beginning of the std-format-spec. This means
+ * directly after the ':'.
+ * @pre The std-format-spec parses the entire input, or the first unmatched
+ * character is a '}'.
+ *
+ * @returns The iterator pointing at the last parsed character.
+ */
+ _LIBCPP_HIDE_FROM_ABI constexpr auto parse(auto& __parse_ctx) -> decltype(__parse_ctx.begin()) {
+ auto __it = __parse(__parse_ctx);
+ __process_display_type();
+ return __it;
+ }
+
+protected:
+ /**
+ * The low-level std-format-spec parse function.
+ *
+ * @pre __begin points at the beginning of the std-format-spec. This means
+ * directly after the ':'.
+ * @pre The std-format-spec parses the entire input, or the first unmatched
+ * character is a '}'.
+ *
+ * @returns The iterator pointing at the last parsed character.
+ */
+ _LIBCPP_HIDE_FROM_ABI constexpr auto __parse(auto& __parse_ctx) -> decltype(__parse_ctx.begin()) {
+ auto __begin = __parse_ctx.begin();
+ auto __end = __parse_ctx.end();
+ if (__begin == __end)
+ return __begin;
+
+ __begin = __parser_fill_align<_CharT>::__parse(__begin, __end, static_cast<_Flags&>(*this));
+ if (__begin == __end)
+ return __begin;
+
+ // An integer presentation type isn't defined in the Standard.
+ // Since a pointer is formatted as an integer it can be argued it's an
+ // integer presentation type. However there are two LWG-issues asserting it
+ // isn't an integer presentation type:
+ // - LWG3612 Inconsistent pointer alignment in std::format
+ // - LWG3644 std::format does not define "integer presentation type"
+ //
+ // There's a paper to make additional clarifications on the status of
+ // formatting pointers and proposes additional fields to be valid. That
+ // paper hasn't been reviewed by the Committee yet.
+ // - P2510 Formatting pointers
+ //
+ // The current implementation assumes formatting pointers isn't covered by
+ // "integer presentation type".
+ // TODO FMT Apply the LWG-issues/papers after approval/rejection by the Committee.
+
+ __begin = __parser_width::__parse(__begin, __end, __parse_ctx);
+ if (__begin == __end)
+ return __begin;
+
+ __begin = __parse_type(__begin, static_cast<_Flags&>(*this));
+
+ if (__begin != __end && *__begin != _CharT('}'))
+ __throw_format_error("The format-spec should consume the input or end with a '}'");
+
+ return __begin;
+ }
+
+ /** Processes the parsed std-format-spec based on the parsed display type. */
+ _LIBCPP_HIDE_FROM_ABI constexpr void __process_display_type() {
+ switch (this->__type) {
+ case _Flags::_Type::__default:
+ this->__type = _Flags::_Type::__pointer;
+ break;
+ case _Flags::_Type::__pointer:
+ break;
+ default:
+ __throw_format_error("The format-spec type has a type not supported for a pointer argument");
+ }
+ }
+};
/** Helper struct returned from @ref __get_string_alignment. */
template <class _CharT>