Bug 1623278 - Use mozilla::Span instead of pointer/length pair in Key. r=dom-workers-and-storage-reviewers,janv
authorSimon Giesecke <sgiesecke@mozilla.com>
Tue, 16 Jun 2020 14:25:04 +0000
changeset 599968 c86f2337fbc49455261dfafedcd050f11ef1250a
parent 599967 9055fd940de5dfeac1762b2779f5297482f1e114
child 599969 c124c1c7b2a4df82ff393c15fcb436f1a234d479
push id13310
push userffxbld-merge
push dateMon, 29 Jun 2020 14:50:06 +0000
treeherdermozilla-beta@15a59a0afa5c [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersdom-workers-and-storage-reviewers, janv
bugs1623278
milestone79.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 1623278 - Use mozilla::Span instead of pointer/length pair in Key. r=dom-workers-and-storage-reviewers,janv Differential Revision: https://phabricator.services.mozilla.com/D72632
dom/indexedDB/Key.cpp
dom/indexedDB/Key.h
--- a/dom/indexedDB/Key.cpp
+++ b/dom/indexedDB/Key.cpp
@@ -474,52 +474,45 @@ nsresult Key::DecodeJSValInternal(const 
 IDBResult<void, IDBSpecialValue::Invalid> Key::EncodeJSVal(
     JSContext* aCx, JS::Handle<JS::Value> aVal, uint8_t aTypeOffset,
     ErrorResult& aRv) {
   return EncodeJSValInternal(aCx, aVal, aTypeOffset, 0, aRv);
 }
 
 IDBResult<void, IDBSpecialValue::Invalid> Key::EncodeString(
     const nsAString& aString, uint8_t aTypeOffset, ErrorResult& aRv) {
-  const char16_t* start = aString.BeginReading();
-  const char16_t* end = aString.EndReading();
-  return EncodeString(start, end, aTypeOffset, aRv);
+  return EncodeString(Span{aString}, aTypeOffset, aRv);
 }
 
 template <typename T>
-IDBResult<void, IDBSpecialValue::Invalid> Key::EncodeString(const T* aStart,
-                                                            const T* aEnd,
-                                                            uint8_t aTypeOffset,
-                                                            ErrorResult& aRv) {
-  return EncodeAsString(aStart, aEnd, eString + aTypeOffset, aRv);
+IDBResult<void, IDBSpecialValue::Invalid> Key::EncodeString(
+    const Span<const T> aInput, const uint8_t aTypeOffset, ErrorResult& aRv) {
+  return EncodeAsString(aInput, eString + aTypeOffset, aRv);
 }
 
 template <typename T>
 IDBResult<void, IDBSpecialValue::Invalid> Key::EncodeAsString(
-    const T* aStart, const T* aEnd, uint8_t aType, ErrorResult& aRv) {
+    const Span<const T> aInput, const uint8_t aType, ErrorResult& aRv) {
   // First measure how long the encoded string will be.
-  if (NS_WARN_IF(aStart > aEnd || UINT32_MAX - 2 < uintptr_t(aEnd - aStart))) {
+  if (NS_WARN_IF(UINT32_MAX - 2 < uintptr_t(aInput.Length()))) {
     IDB_REPORT_INTERNAL_ERR();
     aRv.Throw(NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
     return Exception;
   }
 
   // The +2 is for initial aType and trailing 0. We'll compensate for multi-byte
   // chars below.
-  uint32_t checkedSize = aEnd - aStart;
-  CheckedUint32 size = checkedSize;
+  CheckedUint32 size = aInput.Length();
   size += 2;
 
   MOZ_ASSERT(size.isValid());
 
-  const T* start = aStart;
-  const T* end = aEnd;
-  for (const T* iter = start; iter < end; ++iter) {
-    if (*iter > ONE_BYTE_LIMIT) {
-      size += char16_t(*iter) > TWO_BYTE_LIMIT ? 2 : 1;
+  for (const auto val : aInput) {
+    if (val > ONE_BYTE_LIMIT) {
+      size += char16_t(val) > TWO_BYTE_LIMIT ? 2 : 1;
       if (!size.isValid()) {
         IDB_REPORT_INTERNAL_ERR();
         aRv.Throw(NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
         return Exception;
       }
     }
   }
 
@@ -540,25 +533,25 @@ IDBResult<void, IDBSpecialValue::Invalid
     return Exception;
   }
   buffer += oldLen;
 
   // Write type marker
   *(buffer++) = aType;
 
   // Encode string
-  for (const T* iter = start; iter < end; ++iter) {
-    if (*iter <= ONE_BYTE_LIMIT) {
-      *(buffer++) = *iter + ONE_BYTE_ADJUST;
-    } else if (char16_t(*iter) <= TWO_BYTE_LIMIT) {
-      char16_t c = char16_t(*iter) + TWO_BYTE_ADJUST + 0x8000;
+  for (const auto val : aInput) {
+    if (val <= ONE_BYTE_LIMIT) {
+      *(buffer++) = val + ONE_BYTE_ADJUST;
+    } else if (char16_t(val) <= TWO_BYTE_LIMIT) {
+      char16_t c = char16_t(val) + TWO_BYTE_ADJUST + 0x8000;
       *(buffer++) = (char)(c >> 8);
       *(buffer++) = (char)(c & 0xFF);
     } else {
-      uint32_t c = (uint32_t(*iter) << THREE_BYTE_SHIFT) | 0x00C00000;
+      uint32_t c = (uint32_t(val) << THREE_BYTE_SHIFT) | 0x00C00000;
       *(buffer++) = (char)(c >> 16);
       *(buffer++) = (char)(c >> 8);
       *(buffer++) = (char)c;
     }
   }
 
   // Write end marker
   *(buffer++) = eTerminator;
@@ -598,18 +591,18 @@ IDBResult<void, IDBSpecialValue::Invalid
   }
 
   ucol_close(collator);
   if (NS_WARN_IF(sortKeyLength == 0)) {
     aRv.Throw(NS_ERROR_FAILURE);
     return Exception;
   }
 
-  return EncodeString(keyBuffer.Elements(),
-                      keyBuffer.Elements() + sortKeyLength, aTypeOffset, aRv);
+  return EncodeString(Span{keyBuffer}.AsConst().First(sortKeyLength),
+                      aTypeOffset, aRv);
 }
 
 // static
 nsresult Key::DecodeJSVal(const EncodedDataType*& aPos,
                           const EncodedDataType* aEnd, JSContext* aCx,
                           JS::MutableHandle<JS::Value> aVal) {
   return DecodeJSValInternal(aPos, aEnd, aCx, 0, aVal, 0);
 }
@@ -765,17 +758,17 @@ IDBResult<void, IDBSpecialValue::Invalid
   // the former will handle the wrapped case, the later won't.
   if (aIsViewObject) {
     bool unused;
     JS_GetObjectAsArrayBufferView(aObject, &bufferLength, &unused, &bufferData);
   } else {
     JS::GetObjectAsArrayBuffer(aObject, &bufferLength, &bufferData);
   }
 
-  return EncodeAsString(bufferData, bufferData + bufferLength,
+  return EncodeAsString(Span{bufferData, bufferLength}.AsConst(),
                         eBinary + aTypeOffset, aRv);
 }
 
 // static
 JSObject* Key::DecodeBinary(const EncodedDataType*& aPos,
                             const EncodedDataType* aEnd, JSContext* aCx) {
   JS::RootedObject rv(aCx);
   DecodeStringy<eBinary, uint8_t>(
--- a/dom/indexedDB/Key.h
+++ b/dom/indexedDB/Key.h
@@ -285,24 +285,22 @@ class Key {
   IDBResult<void, IDBSpecialValue::Invalid> EncodeJSVal(
       JSContext* aCx, JS::Handle<JS::Value> aVal, uint8_t aTypeOffset,
       ErrorResult& aRv);
 
   IDBResult<void, IDBSpecialValue::Invalid> EncodeString(
       const nsAString& aString, uint8_t aTypeOffset, ErrorResult& aRv);
 
   template <typename T>
-  IDBResult<void, IDBSpecialValue::Invalid> EncodeString(const T* aStart,
-                                                         const T* aEnd,
+  IDBResult<void, IDBSpecialValue::Invalid> EncodeString(Span<const T> aInput,
                                                          uint8_t aTypeOffset,
                                                          ErrorResult& aRv);
 
   template <typename T>
-  IDBResult<void, IDBSpecialValue::Invalid> EncodeAsString(const T* aStart,
-                                                           const T* aEnd,
+  IDBResult<void, IDBSpecialValue::Invalid> EncodeAsString(Span<const T> aInput,
                                                            uint8_t aType,
                                                            ErrorResult& aRv);
 
   IDBResult<void, IDBSpecialValue::Invalid> EncodeLocaleString(
       const nsDependentString& aString, uint8_t aTypeOffset,
       const nsCString& aLocale, ErrorResult& aRv);
 
   void EncodeNumber(double aFloat, uint8_t aType);