Add WideString::CompareASCIINoCase().
Another useful helper function for a subsequent cl.
Change-Id: I8284e06695393ab9b6a5c2230f339c327361fbf7
Reviewed-on: https://pdfium-review.googlesource.com/c/46190
Reviewed-by: Lei Zhang <thestig@chromium.org>
Commit-Queue: Tom Sepez <tsepez@chromium.org>
diff --git a/core/fxcrt/widestring.cpp b/core/fxcrt/widestring.cpp
index afd37ef..bda34e1 100644
--- a/core/fxcrt/widestring.cpp
+++ b/core/fxcrt/widestring.cpp
@@ -650,7 +650,20 @@
for (size_t i = 0; i < length; ++i) {
wchar_t wc = (*this)[i];
- if (wc <= 0 || wc > 127 || wc != that[i]) // Questionable signedness.
+ if (wc <= 0 || wc > 127 || wc != that[i])
+ return false;
+ }
+ return true;
+}
+
+bool WideString::EqualsASCIINoCase(const ByteStringView& that) const {
+ size_t length = GetLength();
+ if (length != that.GetLength())
+ return false;
+
+ for (size_t i = 0; i < length; ++i) {
+ wchar_t wc = (*this)[i];
+ if (wc <= 0 || wc > 127 || tolower(wc) != tolower(that[i]))
return false;
}
return true;
diff --git a/core/fxcrt/widestring.h b/core/fxcrt/widestring.h
index de7db38..be03731 100644
--- a/core/fxcrt/widestring.h
+++ b/core/fxcrt/widestring.h
@@ -194,6 +194,8 @@
bool IsASCII() const;
bool EqualsASCII(const ByteStringView& that) const;
+ bool EqualsASCIINoCase(const ByteStringView& that) const;
+
ByteString ToASCII() const;
ByteString ToDefANSI() const;
ByteString ToUTF8() const;
diff --git a/core/fxcrt/widestring_unittest.cpp b/core/fxcrt/widestring_unittest.cpp
index 4354dbe..41465c0 100644
--- a/core/fxcrt/widestring_unittest.cpp
+++ b/core/fxcrt/widestring_unittest.cpp
@@ -1018,6 +1018,15 @@
EXPECT_FALSE(WideString(L"\u0141").EqualsASCII("\x41"));
}
+TEST(WideString, EqualsASCIINoCase) {
+ EXPECT_TRUE(WideString(L"").EqualsASCIINoCase(""));
+ EXPECT_FALSE(WideString(L"A").EqualsASCIINoCase("b"));
+ EXPECT_TRUE(WideString(L"AbC").EqualsASCIINoCase("aBc"));
+ EXPECT_FALSE(WideString(L"ABc").EqualsASCIINoCase("AeC"));
+ EXPECT_FALSE(WideString(L"\u00c1").EqualsASCIINoCase("\x41"));
+ EXPECT_FALSE(WideString(L"\u0141").EqualsASCIINoCase("\x41"));
+}
+
TEST(WideString, ToASCII) {
const char* kResult =
"x"