From 77eba872fa49dfc80d7c239b25d7f6e489df9429 Mon Sep 17 00:00:00 2001 From: James Ide Date: Fri, 4 Dec 2026 01:47:58 -0800 Subject: [PATCH] Decode CSS hex colors in one pass MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `parseCSSHexColor` currently reads each digit twice, once to validate it and once to assemble the color. Use a compile-time lookup table to decode each byte once and reuse the decoded value for both steps. The parser accepts the same case-insensitive 3-, 4-, 6- and 8-digit forms. [CSS Color 4 ยง5.2](https://www.w3.org/TR/css-color-4/#hex-notation) The C++ `CSSColor` suite passes. The new cases check mixed-case shorthand and full colors at compile time and reject invalid lengths, digits and non-ASCII bytes at runtime. `yarn cxx-api-validate` passes with Doxygen 1.16.1. An optimized C++ benchmark on Apple M6 alternates valid colors of all four lengths. The median across five interleaved runs, measured with `proc_pid_rusage`, falls from 155.84 to 61.01 retired instructions per valid parse and from 54.88 to 42.38 per parse for inputs with invalid lengths or digits. The result checksums match. Signed-off-by: James Ide --- .../react/renderer/css/CSSHexColor.h | 110 +++++++----------- .../react/renderer/css/tests/CSSColorTest.cpp | 19 +++ .../ReactAndroidFrameworksCxx.api | 11 +- .../api-snapshots/ReactAppleFrameworksCxx.api | 11 +- .../ReactCommonFrameworksCxx.api | 11 +- 5 files changed, 70 insertions(+), 92 deletions(-) diff --git a/packages/react-native/ReactCommon/react/renderer/css/CSSHexColor.h b/packages/react-native/ReactCommon/react/renderer/css/CSSHexColor.h index a630c7542560..2ce80c7191cf 100644 --- a/packages/react-native/ReactCommon/react/renderer/css/CSSHexColor.h +++ b/packages/react-native/ReactCommon/react/renderer/css/CSSHexColor.h @@ -9,60 +9,50 @@ #include +#include +#include #include #include -#include - namespace facebook::react { namespace detail { -enum class HexColorType { - Long, - Short, -}; - -constexpr uint8_t hexToNumeric(std::string_view hex, HexColorType hexType) -{ - int result = 0; - for (char c : hex) { - int value = 0; - if (c >= '0' && c <= '9') { - value = c - '0'; - } else { - value = toLower(c) - 'a' + 10; - } - result *= 16; - result += value; +inline constexpr auto hexDigits = [] { + std::array digits; + digits.fill(0xff); + for (uint8_t digit = 0; digit < 10; ++digit) { + digits['0' + digit] = digit; } - - if (hexType == HexColorType::Short) { - return static_cast(result * 16 + result); - } else { - return static_cast(result); + for (uint8_t digit = 0; digit < 6; ++digit) { + digits['A' + digit] = 10 + digit; + digits['a' + digit] = 10 + digit; } -} + return digits; +}(); -constexpr bool isHexDigit(char c) +template +constexpr std::optional parseHexDigits(const char *value) { - return (c >= '0' && c <= '9') || (toLower(c) >= 'a' && toLower(c) <= 'f'); -} - -constexpr bool isValidHexColor(std::string_view hex) -{ - // The syntax of a is a token whose value consists - // of 3, 4, 6, or 8 hexadecimal digits. - if (hex.size() != 3 && hex.size() != 4 && hex.size() != 6 && hex.size() != 8) { - return false; + std::array digits; + uint8_t combined = 0; + for (size_t index = 0; index < DigitCount; ++index) { + digits[index] = hexDigits[static_cast(value[index])]; + combined |= digits[index]; } - - for (auto c : hex) { - if (!isHexDigit(c)) { - return false; - } + if (combined > 0xf) { + return {}; } - return true; + constexpr size_t digitsPerChannel = DigitCount < 6 ? 1 : 2; + constexpr size_t channelCount = DigitCount / digitsPerChannel; + + auto decodeChannel = [&](size_t channel) -> uint8_t { + auto offset = channel * digitsPerChannel; + return static_cast((digits[offset] << 4) | digits[offset + digitsPerChannel - 1]); + }; + + return CSSColor{ + decodeChannel(0), decodeChannel(1), decodeChannel(2), channelCount == 4 ? decodeChannel(3) : uint8_t{255}}; } } // namespace detail @@ -74,34 +64,18 @@ constexpr bool isValidHexColor(std::string_view hex) template constexpr std::optional parseCSSHexColor(std::string_view hexColorValue) { - if (detail::isValidHexColor(hexColorValue)) { - if (hexColorValue.length() == 3) { - return CSSColor{ - hexToNumeric(hexColorValue.substr(0, 1), detail::HexColorType::Short), - hexToNumeric(hexColorValue.substr(1, 1), detail::HexColorType::Short), - hexToNumeric(hexColorValue.substr(2, 1), detail::HexColorType::Short), - 255u}; - } else if (hexColorValue.length() == 4) { - return CSSColor{ - hexToNumeric(hexColorValue.substr(0, 1), detail::HexColorType::Short), - hexToNumeric(hexColorValue.substr(1, 1), detail::HexColorType::Short), - hexToNumeric(hexColorValue.substr(2, 1), detail::HexColorType::Short), - hexToNumeric(hexColorValue.substr(3, 1), detail::HexColorType::Short)}; - } else if (hexColorValue.length() == 6) { - return CSSColor{ - hexToNumeric(hexColorValue.substr(0, 2), detail::HexColorType::Long), - hexToNumeric(hexColorValue.substr(2, 2), detail::HexColorType::Long), - hexToNumeric(hexColorValue.substr(4, 2), detail::HexColorType::Long), - 255u}; - } else if (hexColorValue.length() == 8) { - return CSSColor{ - hexToNumeric(hexColorValue.substr(0, 2), detail::HexColorType::Long), - hexToNumeric(hexColorValue.substr(2, 2), detail::HexColorType::Long), - hexToNumeric(hexColorValue.substr(4, 2), detail::HexColorType::Long), - hexToNumeric(hexColorValue.substr(6, 2), detail::HexColorType::Long)}; - } + switch (hexColorValue.size()) { + case 3: + return detail::parseHexDigits(hexColorValue.data()); + case 4: + return detail::parseHexDigits(hexColorValue.data()); + case 6: + return detail::parseHexDigits(hexColorValue.data()); + case 8: + return detail::parseHexDigits(hexColorValue.data()); + default: + return {}; } - return {}; } } // namespace facebook::react diff --git a/packages/react-native/ReactCommon/react/renderer/css/tests/CSSColorTest.cpp b/packages/react-native/ReactCommon/react/renderer/css/tests/CSSColorTest.cpp index 748dd6e1d298..f76e56d9b33b 100644 --- a/packages/react-native/ReactCommon/react/renderer/css/tests/CSSColorTest.cpp +++ b/packages/react-native/ReactCommon/react/renderer/css/tests/CSSColorTest.cpp @@ -66,6 +66,25 @@ TEST(CSSColor, hex_color_values) { EXPECT_EQ(std::get(hexAlphaTestValue).a, 204); } +TEST(CSSColor, hex_color_digit_validation) { + constexpr auto shorthand = parseCSSHexColor("1aB7"); + static_assert(shorthand == CSSColor{0x11, 0xaa, 0xbb, 0x77}); + constexpr auto full = parseCSSHexColor("01aB2398"); + static_assert(full == CSSColor{0x01, 0xab, 0x23, 0x98}); + + for (const auto value : + {"12", + "12345", + "1234567", + "g12", + "123g", + "12345g", + "1234567g", + "1234567\xff"}) { + EXPECT_FALSE(parseCSSHexColor(value)) << value; + } +} + TEST(CSSColor, named_colors) { auto invalidNamedColorTestValue = parseCSSProperty("redd"); EXPECT_TRUE( diff --git a/scripts/cxx-api/api-snapshots/ReactAndroidFrameworksCxx.api b/scripts/cxx-api/api-snapshots/ReactAndroidFrameworksCxx.api index b520c48af496..95ddb9e5922b 100644 --- a/scripts/cxx-api/api-snapshots/ReactAndroidFrameworksCxx.api +++ b/scripts/cxx-api/api-snapshots/ReactAndroidFrameworksCxx.api @@ -8467,19 +8467,19 @@ template void facebook::react::exceptions::throwIfObjectAlreadyConsumed(const T& t, const char* msg); -constexpr bool facebook::react::detail::isHexDigit(char c); -constexpr bool facebook::react::detail::isValidHexColor(std::string_view hex); +constexpr auto facebook::react::detail::hexDigits; constexpr float facebook::react::detail::hueToRgb(float p, float q, float t); constexpr std::optional facebook::react::detail::normalizeNumberComponent(const std::variant& component); constexpr uint8_t facebook::react::detail::clamp255Component(float f); constexpr uint8_t facebook::react::detail::clampAlpha(std::optional alpha); -constexpr uint8_t facebook::react::detail::hexToNumeric(std::string_view hex, facebook::react::detail::HexColorType hexType); float facebook::react::detail::normalizeHue(float hue); std::optional facebook::react::detail::normalizeHueComponent(const std::variant& component); std::tuple facebook::react::detail::hslToRgb(float h, float s, float l); std::tuple facebook::react::detail::hwbToRgb(float h, float w, float b); template constexpr bool facebook::react::detail::isLegacyColorFunction(facebook::react::CSSValueParser& parser); +template +constexpr std::optional facebook::react::detail::parseHexDigits(const char* value); template constexpr std::optional facebook::react::detail::parseLegacyRgbFunction(facebook::react::CSSValueParser& parser); template @@ -8508,11 +8508,6 @@ class facebook::react::detail::OwnedBytesBuffer : public facebook::jsi::MutableB public virtual uint8_t* data() override; } -enum facebook::react::detail::HexColorType { - Long, - Short, -} - template struct facebook::react::detail::merge_data_types { public using type = typename facebook::react::detail::merge_data_types, RestT...>::type; diff --git a/scripts/cxx-api/api-snapshots/ReactAppleFrameworksCxx.api b/scripts/cxx-api/api-snapshots/ReactAppleFrameworksCxx.api index 85668b7d3ccc..976b487bced2 100644 --- a/scripts/cxx-api/api-snapshots/ReactAppleFrameworksCxx.api +++ b/scripts/cxx-api/api-snapshots/ReactAppleFrameworksCxx.api @@ -10322,19 +10322,19 @@ bool facebook::react::HostPlatformViewTraitsInitializer::formsView(const faceboo bool facebook::react::HostPlatformViewTraitsInitializer::isKeyboardFocusable(const facebook::react::ViewProps&); -constexpr bool facebook::react::detail::isHexDigit(char c); -constexpr bool facebook::react::detail::isValidHexColor(std::string_view hex); +constexpr auto facebook::react::detail::hexDigits; constexpr float facebook::react::detail::hueToRgb(float p, float q, float t); constexpr std::optional facebook::react::detail::normalizeNumberComponent(const std::variant& component); constexpr uint8_t facebook::react::detail::clamp255Component(float f); constexpr uint8_t facebook::react::detail::clampAlpha(std::optional alpha); -constexpr uint8_t facebook::react::detail::hexToNumeric(std::string_view hex, facebook::react::detail::HexColorType hexType); float facebook::react::detail::normalizeHue(float hue); std::optional facebook::react::detail::normalizeHueComponent(const std::variant& component); std::tuple facebook::react::detail::hslToRgb(float h, float s, float l); std::tuple facebook::react::detail::hwbToRgb(float h, float w, float b); template constexpr bool facebook::react::detail::isLegacyColorFunction(facebook::react::CSSValueParser& parser); +template +constexpr std::optional facebook::react::detail::parseHexDigits(const char* value); template constexpr std::optional facebook::react::detail::parseLegacyRgbFunction(facebook::react::CSSValueParser& parser); template @@ -10363,11 +10363,6 @@ class facebook::react::detail::OwnedBytesBuffer : public facebook::jsi::MutableB public virtual uint8_t* data() override; } -enum facebook::react::detail::HexColorType { - Long, - Short, -} - template struct facebook::react::detail::merge_data_types { public using type = typename facebook::react::detail::merge_data_types, RestT...>::type; diff --git a/scripts/cxx-api/api-snapshots/ReactCommonFrameworksCxx.api b/scripts/cxx-api/api-snapshots/ReactCommonFrameworksCxx.api index f68615e9118b..c681c1bb80b8 100644 --- a/scripts/cxx-api/api-snapshots/ReactCommonFrameworksCxx.api +++ b/scripts/cxx-api/api-snapshots/ReactCommonFrameworksCxx.api @@ -5656,19 +5656,19 @@ struct facebook::react::Bridging, std::enable_if_t facebook::react::detail::normalizeNumberComponent(const std::variant& component); constexpr uint8_t facebook::react::detail::clamp255Component(float f); constexpr uint8_t facebook::react::detail::clampAlpha(std::optional alpha); -constexpr uint8_t facebook::react::detail::hexToNumeric(std::string_view hex, facebook::react::detail::HexColorType hexType); float facebook::react::detail::normalizeHue(float hue); std::optional facebook::react::detail::normalizeHueComponent(const std::variant& component); std::tuple facebook::react::detail::hslToRgb(float h, float s, float l); std::tuple facebook::react::detail::hwbToRgb(float h, float w, float b); template constexpr bool facebook::react::detail::isLegacyColorFunction(facebook::react::CSSValueParser& parser); +template +constexpr std::optional facebook::react::detail::parseHexDigits(const char* value); template constexpr std::optional facebook::react::detail::parseLegacyRgbFunction(facebook::react::CSSValueParser& parser); template @@ -5697,11 +5697,6 @@ class facebook::react::detail::OwnedBytesBuffer : public facebook::jsi::MutableB public virtual uint8_t* data() override; } -enum facebook::react::detail::HexColorType { - Long, - Short, -} - template struct facebook::react::detail::merge_data_types { public using type = typename facebook::react::detail::merge_data_types, RestT...>::type;