// Copyright 2020 The Abseil Authors. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // https://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. #ifndef ABSL_STRINGS_INTERNAL_STR_FORMAT_PARSER_H_ #define ABSL_STRINGS_INTERNAL_STR_FORMAT_PARSER_H_ #include <stddef.h> #include <stdlib.h> #include <cassert> #include <cstring> #include <initializer_list> #include <memory> #include <string> #include <utility> #include <vector> #include "absl/base/config.h" #include "absl/base/optimization.h" #include "absl/strings/internal/str_format/checker.h" #include "absl/strings/internal/str_format/constexpr_parser.h" #include "absl/strings/internal/str_format/extension.h" #include "absl/strings/string_view.h" namespace absl { ABSL_NAMESPACE_BEGIN namespace str_format_internal { std::string LengthModToString(LengthMod v); const char* ConsumeUnboundConversionNoInline(const char* p, const char* end, UnboundConversion* conv, int* next_arg); // Parse the format string provided in 'src' and pass the identified items into // 'consumer'. // Text runs will be passed by calling // Consumer::Append(string_view); // ConversionItems will be passed by calling // Consumer::ConvertOne(UnboundConversion, string_view); // In the case of ConvertOne, the string_view that is passed is the // portion of the format string corresponding to the conversion, not including // the leading %. On success, it returns true. On failure, it stops and returns // false. template <typename Consumer> bool ParseFormatString(string_view src, Consumer consumer) { … } // Always returns true, or fails to compile in a constexpr context if s does not // point to a constexpr char array. constexpr bool EnsureConstexpr(string_view s) { … } class ParsedFormatBase { … }; // A value type representing a preparsed format. These can be created, copied // around, and reused to speed up formatting loops. // The user must specify through the template arguments the conversion // characters used in the format. This will be checked at compile time. // // This class uses Conv enum values to specify each argument. // This allows for more flexibility as you can specify multiple possible // conversion characters for each argument. // ParsedFormat<char...> is a simplified alias for when the user only // needs to specify a single conversion character for each argument. // // Example: // // Extended format supports multiple characters per argument: // using MyFormat = ExtendedParsedFormat<Conv::d | Conv::x>; // MyFormat GetFormat(bool use_hex) { // if (use_hex) return MyFormat("foo %x bar"); // return MyFormat("foo %d bar"); // } // // 'format' can be used with any value that supports 'd' and 'x', // // like `int`. // auto format = GetFormat(use_hex); // value = StringF(format, i); // // This class also supports runtime format checking with the ::New() and // ::NewAllowIgnored() factory functions. // This is the only API that allows the user to pass a runtime specified format // string. These factory functions will return NULL if the format does not match // the conversions requested by the user. template <FormatConversionCharSet... C> class ExtendedParsedFormat : public str_format_internal::ParsedFormatBase { … }; } // namespace str_format_internal ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_STRINGS_INTERNAL_STR_FORMAT_PARSER_H_