Move ASCII string operations to string_view_template.h
Allows interoperability with both Strings and StringViews of
either kind.
Test: tested by widestring wrapper.
Change-Id: Ifd11f1985ec758143ed4a6f5e9e813262684513f
Reviewed-on: https://pdfium-review.googlesource.com/c/46210
Commit-Queue: Tom Sepez <tsepez@chromium.org>
Reviewed-by: Lei Zhang <thestig@chromium.org>
diff --git a/core/fxcrt/string_view_template.h b/core/fxcrt/string_view_template.h
index e1924fe..adedbdc 100644
--- a/core/fxcrt/string_view_template.h
+++ b/core/fxcrt/string_view_template.h
@@ -109,6 +109,40 @@
return !(*this == other);
}
+ bool IsASCII() const {
+ for (auto c : *this) {
+ if (c <= 0 || c > 127) // Questionable signedness of |c|.
+ return false;
+ }
+ return true;
+ }
+
+ bool EqualsASCII(const StringViewTemplate<char>& that) const {
+ size_t length = GetLength();
+ if (length != that.GetLength())
+ return false;
+
+ for (size_t i = 0; i < length; ++i) {
+ auto c = (*this)[i];
+ if (c <= 0 || c > 127 || c != that[i]) // Questionable signedness of |c|.
+ return false;
+ }
+ return true;
+ }
+
+ bool EqualsASCIINoCase(const StringViewTemplate<char>& that) const {
+ size_t length = GetLength();
+ if (length != that.GetLength())
+ return false;
+
+ for (size_t i = 0; i < length; ++i) {
+ auto c = (*this)[i];
+ if (c <= 0 || c > 127 || tolower(c) != tolower(that[i]))
+ return false;
+ }
+ return true;
+ }
+
uint32_t GetID() const {
if (m_Span.size() == 0)
return 0;
diff --git a/core/fxcrt/widestring.cpp b/core/fxcrt/widestring.cpp
index bda34e1..0ddbcd9 100644
--- a/core/fxcrt/widestring.cpp
+++ b/core/fxcrt/widestring.cpp
@@ -635,40 +635,6 @@
return m_pData ? m_pData->m_nRefs : 0;
}
-bool WideString::IsASCII() const {
- for (wchar_t wc : *this) {
- if (wc <= 0 || wc > 127) // Questionable signedness of wchar_t.
- return false;
- }
- return true;
-}
-
-bool WideString::EqualsASCII(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 || 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;
-}
-
ByteString WideString::ToASCII() const {
ByteString result;
result.Reserve(GetLength());
diff --git a/core/fxcrt/widestring.h b/core/fxcrt/widestring.h
index be03731..f918bca 100644
--- a/core/fxcrt/widestring.h
+++ b/core/fxcrt/widestring.h
@@ -192,9 +192,13 @@
size_t Replace(const WideStringView& pOld, const WideStringView& pNew);
size_t Remove(wchar_t ch);
- bool IsASCII() const;
- bool EqualsASCII(const ByteStringView& that) const;
- bool EqualsASCIINoCase(const ByteStringView& that) const;
+ bool IsASCII() const { return AsStringView().IsASCII(); }
+ bool EqualsASCII(const ByteStringView& that) const {
+ return AsStringView().EqualsASCII(that);
+ }
+ bool EqualsASCIINoCase(const ByteStringView& that) const {
+ return AsStringView().EqualsASCIINoCase(that);
+ }
ByteString ToASCII() const;
ByteString ToDefANSI() const;