From 62ea6e891c9d57d60afaa25dbca807a7d169a926 Mon Sep 17 00:00:00 2001 From: Nick Gerleman Date: Tue, 4 Feb 2025 21:51:21 -0800 Subject: [PATCH] `react/utils/toLower` (#49187) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/49187 `tolower` is not `constexpr`. Share some quick utilities for char to lowercase, and case insensitive comparision that does not create new string. Changelog: [Internal] Reviewed By: lenaic Differential Revision: D69134770 fbshipit-source-id: 57a84f2d1a441e5a4c07c0db96cb6c133770fb51 --- .../react/renderer/css/CSSHexColor.h | 9 ++--- .../ReactCommon/react/utils/fnv1a.h | 7 ++-- .../ReactCommon/react/utils/iequals.h | 34 +++++++++++++++++++ .../ReactCommon/react/utils/toLower.h | 22 ++++++++++++ 4 files changed, 61 insertions(+), 11 deletions(-) create mode 100644 packages/react-native/ReactCommon/react/utils/iequals.h create mode 100644 packages/react-native/ReactCommon/react/utils/toLower.h diff --git a/packages/react-native/ReactCommon/react/renderer/css/CSSHexColor.h b/packages/react-native/ReactCommon/react/renderer/css/CSSHexColor.h index 58607f67d4a..2a1268c4fe5 100644 --- a/packages/react-native/ReactCommon/react/renderer/css/CSSHexColor.h +++ b/packages/react-native/ReactCommon/react/renderer/css/CSSHexColor.h @@ -10,6 +10,8 @@ #include #include +#include + namespace facebook::react { namespace detail { @@ -18,13 +20,6 @@ enum class HexColorType { Short, }; -constexpr char toLower(char c) { - if (c >= 'A' && c <= 'Z') { - return static_cast(c + 32); - } - return c; -} - constexpr uint8_t hexToNumeric(std::string_view hex, HexColorType hexType) { int result = 0; for (char c : hex) { diff --git a/packages/react-native/ReactCommon/react/utils/fnv1a.h b/packages/react-native/ReactCommon/react/utils/fnv1a.h index 2acdeb3113b..4a945d968f7 100644 --- a/packages/react-native/ReactCommon/react/utils/fnv1a.h +++ b/packages/react-native/ReactCommon/react/utils/fnv1a.h @@ -11,6 +11,8 @@ #include #include +#include + namespace facebook::react { /** @@ -41,10 +43,7 @@ constexpr uint32_t fnv1a(std::string_view string) noexcept { constexpr uint32_t fnv1aLowercase(std::string_view string) { struct LowerCaseTransform { constexpr char operator()(char c) const { - if (c >= 'A' && c <= 'Z') { - return c + static_cast('a' - 'A'); - } - return c; + return toLower(c); } }; diff --git a/packages/react-native/ReactCommon/react/utils/iequals.h b/packages/react-native/ReactCommon/react/utils/iequals.h new file mode 100644 index 00000000000..b30bd5b9e9d --- /dev/null +++ b/packages/react-native/ReactCommon/react/utils/iequals.h @@ -0,0 +1,34 @@ +/* + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +#pragma once + +#include +#include + +#include + +namespace facebook::react { + +/** + * constexpr check for case insensitive equality of two strings. + */ +constexpr bool iequals(std::string_view a, std::string_view b) { + if (a.size() != b.size()) { + return false; + } + + for (size_t i = 0; i < a.size(); i++) { + if (toLower(a[i]) != toLower(b[i])) { + return false; + } + } + + return true; +} + +} // namespace facebook::react diff --git a/packages/react-native/ReactCommon/react/utils/toLower.h b/packages/react-native/ReactCommon/react/utils/toLower.h new file mode 100644 index 00000000000..27212d006dc --- /dev/null +++ b/packages/react-native/ReactCommon/react/utils/toLower.h @@ -0,0 +1,22 @@ +/* + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +#pragma once + +namespace facebook::react { + +/** + * constexpr version of tolower + */ +constexpr char toLower(char c) { + if (c >= 'A' && c <= 'Z') { + return static_cast(c + 32); + } + return c; +} + +} // namespace facebook::react