Bug 1591417: part 3) Add `nsTStringRepr<T>::EqualsLatin1`. r=froydnj
authorMirko Brodesser <mbrodesser@mozilla.com>
Tue, 12 Nov 2019 15:20:25 +0000
changeset 501718 0dd20dae7caac58c51562d1ab9fd0b51792a8d2d
parent 501717 34804d1ae0c049bef2003f93e9ffb966a228db3c
child 501719 e711f304a2cd05c11e7c8e0231b75be4a70e7493
push id114172
push userdluca@mozilla.com
push dateTue, 19 Nov 2019 11:31:10 +0000
treeherdermozilla-inbound@b5c5ba07d3db [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersfroydnj
bugs1591417
milestone72.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1591417: part 3) Add `nsTStringRepr<T>::EqualsLatin1`. r=froydnj Needed to compare with `nsTextFragment::Get1b()` which returns latin1-encoded characters. Used in a subsequent review. Differential Revision: https://phabricator.services.mozilla.com/D52343
xpcom/string/nsCharTraits.h
xpcom/string/nsTStringRepr.h
xpcom/string/nsTSubstring.cpp
--- a/xpcom/string/nsCharTraits.h
+++ b/xpcom/string/nsCharTraits.h
@@ -159,16 +159,27 @@ struct nsCharTraits<char16_t> {
         return to_int_type(*aStr1) -
                to_int_type(static_cast<char_type>(*aStr2));
       }
     }
 
     return 0;
   }
 
+  static bool equalsLatin1(const char_type* aStr1, const char* aStr2,
+                           const size_t aN) {
+    for (size_t i = aN; i > 0; --i, ++aStr1, ++aStr2) {
+      if (*aStr1 != static_cast<char_type>(*aStr2)) {
+        return false;
+      }
+    }
+
+    return true;
+  }
+
   // this version assumes that s2 is null-terminated and s1 has length n.
   // if s1 is shorter than s2 then we return -1; if s1 is longer than s2,
   // we return 1.
   static int compareASCIINullTerminated(const char_type* aStr1, size_t aN,
                                         const char* aStr2) {
     for (; aN--; ++aStr1, ++aStr2) {
       if (!*aStr2) {
         return 1;
@@ -323,16 +334,21 @@ struct nsCharTraits<char> {
 #ifdef DEBUG
     for (size_t i = 0; i < aN; ++i) {
       NS_ASSERTION(!(aStr2[i] & ~0x7F), "Unexpected non-ASCII character");
     }
 #endif
     return compare(aStr1, aStr2, aN);
   }
 
+  static bool equalsLatin1(const char_type* aStr1, const char* aStr2,
+                           size_t aN) {
+    return memcmp(aStr1, aStr2, aN) == 0;
+  }
+
   // this version assumes that s2 is null-terminated and s1 has length n.
   // if s1 is shorter than s2 then we return -1; if s1 is longer than s2,
   // we return 1.
   static int compareASCIINullTerminated(const char_type* aStr1, size_t aN,
                                         const char* aStr2) {
     // can't use strcmp here because we don't want to stop when aStr1
     // contains a null
     for (; aN--; ++aStr1, ++aStr2) {
--- a/xpcom/string/nsTStringRepr.h
+++ b/xpcom/string/nsTStringRepr.h
@@ -252,16 +252,20 @@ class nsTStringRepr {
   // for wide strings. Call this version when you know the
   // length of 'data'.
   bool NS_FASTCALL EqualsASCII(const char* aData, size_type aLen) const;
   // An efficient comparison with ASCII that can be used even
   // for wide strings. Call this version when 'data' is
   // null-terminated.
   bool NS_FASTCALL EqualsASCII(const char* aData) const;
 
+  // An efficient comparison with Latin1 characters that can be used even for
+  // wide strings.
+  bool EqualsLatin1(const char* aData, size_type aLength) const;
+
   // EqualsLiteral must ONLY be called with an actual literal string, or
   // a char array *constant* declared without an explicit size and with an
   // initializer that is a string literal or is otherwise null-terminated.
   // Use EqualsASCII for other char array variables.
   // (Although this method may happen to produce expected results for other
   // char arrays that have bound one greater than the sequence of interest,
   // such use is discouraged for reasons of readability and maintainability.)
   // The template trick to acquire the array bound at compile time without
--- a/xpcom/string/nsTSubstring.cpp
+++ b/xpcom/string/nsTSubstring.cpp
@@ -1049,16 +1049,23 @@ bool nsTStringRepr<T>::EqualsASCII(const
 
 template <typename T>
 bool nsTStringRepr<T>::EqualsASCII(const char* aData) const {
   return char_traits::compareASCIINullTerminated(this->mData, this->mLength,
                                                  aData) == 0;
 }
 
 template <typename T>
+bool nsTStringRepr<T>::EqualsLatin1(const char* aData,
+                                    const size_type aLength) const {
+  return (this->mLength == aLength) &&
+         char_traits::equalsLatin1(this->mData, aData, aLength);
+}
+
+template <typename T>
 bool nsTStringRepr<T>::LowerCaseEqualsASCII(const char* aData,
                                             size_type aLen) const {
   return this->mLength == aLen &&
          char_traits::compareLowerCaseToASCII(this->mData, aData, aLen) == 0;
 }
 
 template <typename T>
 bool nsTStringRepr<T>::LowerCaseEqualsASCII(const char* aData) const {