Summary: | Add helper function to match identifiers and return a corresponding enum mapped enum value for CSS property parser | ||
---|---|---|---|
Product: | WebKit | Reporter: | Sam Weinig <sam> |
Component: | CSS | Assignee: | Sam Weinig <sam> |
Status: | RESOLVED FIXED | ||
Severity: | Normal | CC: | webkit-bug-importer |
Priority: | P2 | Keywords: | InRadar |
Version: | WebKit Local Build | ||
Hardware: | Unspecified | ||
OS: | Unspecified |
Description
Sam Weinig
2022-11-01 11:07:09 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); } ``` 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 }, }); } ``` 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); } ``` Pull request: https://github.com/WebKit/WebKit/pull/6050 Committed 256290@main (dcf98ac5a51f): <https://commits.webkit.org/256290@main> Reviewed commits have been landed. Closing PR #6050 and removing active labels. |