RESOLVED FIXED 247326
Add helper function to match identifiers and return a corresponding enum mapped enum value for CSS property parser
https://bugs.webkit.org/show_bug.cgi?id=247326
Summary Add helper function to match identifiers and return a corresponding enum mapp...
Sam Weinig
Reported 2022-11-01 11:07:09 PDT
Add helper function to match identifiers and return a corresponding enum mapped enum value for CSS property parser
Attachments
Sam Weinig
Comment 1 2022-11-01 20:37:43 PDT
One idea is to just use SortedArrayMap + a helper. For example, the helper "consumeIdentUsingMapping" and and example use: ``` template<typename Map> static std::optional<typename Map::ValueType> consumeIdentUsingMapping(CSSParserTokenRange& range, Map& map) { if (auto value = map.tryGet(range.peek().id())) { range.consumeIncludingWhitespace(); return std::make_optional(*value); } return std::nullopt; } static std::optional<HueInterpolationMethod> consumeHueInterpolationMethod(CSSParserTokenRange& range) { static constexpr std::pair<CSSValueID, HueInterpolationMethod> hueInterpolationMethodMappings[] { { CSSValueShorter, HueInterpolationMethod::Shorter }, { CSSValueLonger, HueInterpolationMethod::Longer }, { CSSValueIncreasing, HueInterpolationMethod::Increasing }, { CSSValueDecreasing, HueInterpolationMethod::Decreasing }, { CSSValueSpecified, HueInterpolationMethod::Specified }, }; static constexpr SortedArrayMap hueInterpolationMethodMap { hueInterpolationMethodMappings }; return consumeIdentUsingMapping(range, hueInterpolationMethodMap); } ```
Sam Weinig
Comment 2 2022-11-01 20:38:59 PDT
An alternative that doesn't require declaring the static constexprs would be to use initializer_list and a linear search. For example: ``` template<typename MappedType> static std::optional<typename Map::ValueType> consumeIdentUsingMappingTo(CSSParserTokenRange& range, std::initializer_list<std::pair<CSSValueID, MappedType> list) { auto key = range.peek().id(); for (auto [valueID, mapping] : list) { if (key == valueID) return std::make_optional(mapping); } return std::nullopt; } static std::optional<HueInterpolationMethod> consumeHueInterpolationMethod(CSSParserTokenRange& range) { return consumeIdentifierMappingTo<HueInterpolationMethod>(range, { { CSSValueShorter, HueInterpolationMethod::Shorter }, { CSSValueLonger, HueInterpolationMethod::Longer }, { CSSValueIncreasing, HueInterpolationMethod::Increasing }, { CSSValueDecreasing, HueInterpolationMethod::Decreasing }, { CSSValueSpecified, HueInterpolationMethod::Specified }, }); } ```
Sam Weinig
Comment 3 2022-11-01 20:41:41 PDT
Yet another alternative would be to use the template arguments themselves (I haven't written out this version yet, but it would be a recursive walk through the argument pairs). The caller might look like this: ``` static std::optional<HueInterpolationMethod> consumeHueInterpolationMethod(CSSParserTokenRange& range) { return consumeIdentifierMappingTo<HueInterpolationMethod, CSSValueShorter, HueInterpolationMethod::Shorter, CSSValueLonger, HueInterpolationMethod::Longer, CSSValueIncreasing, HueInterpolationMethod::Increasing, CSSValueDecreasing, HueInterpolationMethod::Decreasing, CSSValueSpecified, HueInterpolationMethod::Specified, >(range); } ```
Radar WebKit Bug Importer
Comment 4 2022-11-02 14:11:57 PDT
Sam Weinig
Comment 5 2022-11-02 14:15:37 PDT
EWS
Comment 6 2022-11-03 13:24:59 PDT
Committed 256290@main (dcf98ac5a51f): <https://commits.webkit.org/256290@main> Reviewed commits have been landed. Closing PR #6050 and removing active labels.
Note You need to log in before you can comment on or make changes to this bug.