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
This commit is contained in:
Nick Gerleman
2025-02-04 21:51:21 -08:00
committed by Facebook GitHub Bot
parent 75097b2599
commit 62ea6e891c
4 changed files with 61 additions and 11 deletions
@@ -10,6 +10,8 @@
#include <optional>
#include <string_view>
#include <react/utils/toLower.h>
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<char>(c + 32);
}
return c;
}
constexpr uint8_t hexToNumeric(std::string_view hex, HexColorType hexType) {
int result = 0;
for (char c : hex) {
@@ -11,6 +11,8 @@
#include <functional>
#include <string_view>
#include <react/utils/toLower.h>
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<char>('a' - 'A');
}
return c;
return toLower(c);
}
};
@@ -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 <cstdint>
#include <string_view>
#include <react/utils/toLower.h>
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
@@ -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<char>(c + 32);
}
return c;
}
} // namespace facebook::react