Bug 698256 Add and use Substring(data, length) convenience method r=dbaron
authorNeil Rashbrook <neil@parkwaycc.co.uk>
Fri, 09 Dec 2011 08:35:41 +0000
changeset 82270 141fe205fb73cbbf0263bdd44bc61a46ca51f032
parent 82269 9e7239c0f557ddafdca5bcf9598294c52f42d08d
child 82352 63bff373cb94613e93c2795eb3b46e9a23ab3ee5
push id21590
push userneil@parkwaycc.co.uk
push dateFri, 09 Dec 2011 08:36:59 +0000
treeherdermozilla-central@141fe205fb73 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersdbaron
bugs698256
milestone11.0a1
first release with
nightly win64
141fe205fb73 / 11.0a1 / 20111209031218 / files
nightly linux32
nightly linux64
nightly mac
nightly win32
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
releases
nightly win64
Bug 698256 Add and use Substring(data, length) convenience method r=dbaron
content/base/src/nsTextFragment.h
modules/libpref/src/Preferences.cpp
modules/libpref/src/nsPrefBranch.cpp
netwerk/base/src/nsSerializationHelper.cpp
netwerk/streamconv/converters/nsIndexedToHTML.cpp
xpcom/build/nsXPCOMStrings.cpp
xpcom/ds/nsVariant.cpp
xpcom/io/nsStringStream.cpp
xpcom/string/public/nsString.h
xpcom/string/public/nsTDependentSubstring.h
xpcom/string/src/nsTDependentSubstring.cpp
xpcom/string/src/nsTStringObsolete.cpp
--- a/content/base/src/nsTextFragment.h
+++ b/content/base/src/nsTextFragment.h
@@ -171,31 +171,30 @@ public:
 
   /**
    * Append the contents of this string fragment to aString
    */
   void AppendTo(nsAString& aString) const {
     if (mState.mIs2b) {
       aString.Append(m2b, mState.mLength);
     } else {
-      AppendASCIItoUTF16(Substring(m1b, m1b + mState.mLength),
-                         aString);
+      AppendASCIItoUTF16(Substring(m1b, mState.mLength), aString);
     }
   }
 
   /**
    * Append a substring of the contents of this string fragment to aString.
    * @param aOffset where to start the substring in this text fragment
    * @param aLength the length of the substring
    */
   void AppendTo(nsAString& aString, PRInt32 aOffset, PRInt32 aLength) const {
     if (mState.mIs2b) {
       aString.Append(m2b + aOffset, aLength);
     } else {
-      AppendASCIItoUTF16(Substring(m1b + aOffset, m1b + aOffset + aLength), aString);
+      AppendASCIItoUTF16(Substring(m1b + aOffset, aLength), aString);
     }
   }
 
   /**
    * Make a copy of the fragments contents starting at offset for
    * count characters. The offset and count will be adjusted to
    * lie within the fragments data. The fragments data is converted if
    * necessary.
--- a/modules/libpref/src/Preferences.cpp
+++ b/modules/libpref/src/Preferences.cpp
@@ -1014,17 +1014,17 @@ static nsresult pref_InitInitialObjects(
     NS_ENSURE_SUCCESS(rv, rv);
 
     // Load jar:$gre/omni.jar!/defaults/pref/*.js
     rv = jarReader->FindInit("defaults/pref/*.js$", &findPtr);
     NS_ENSURE_SUCCESS(rv, rv);
 
     find = findPtr;
     while (NS_SUCCEEDED(find->FindNext(&entryName, &entryNameLen))) {
-      prefEntries.AppendElement(Substring(entryName, entryName + entryNameLen));
+      prefEntries.AppendElement(Substring(entryName, entryNameLen));
     }
 
     prefEntries.Sort();
     for (PRUint32 i = prefEntries.Length(); i--; ) {
       rv = pref_ReadPrefFromJar(jarReader, prefEntries[i].get());
       if (NS_FAILED(rv))
         NS_WARNING("Error parsing preferences.");
     }
@@ -1075,17 +1075,17 @@ static nsresult pref_InitInitialObjects(
   // Load jar:$app/omni.jar!/defaults/preferences/*.js
   nsRefPtr<nsZipArchive> appJarReader = mozilla::Omnijar::GetReader(mozilla::Omnijar::APP);
   if (appJarReader) {
     rv = appJarReader->FindInit("defaults/preferences/*.js$", &findPtr);
     NS_ENSURE_SUCCESS(rv, rv);
     find = findPtr;
     prefEntries.Clear();
     while (NS_SUCCEEDED(find->FindNext(&entryName, &entryNameLen))) {
-      prefEntries.AppendElement(Substring(entryName, entryName + entryNameLen));
+      prefEntries.AppendElement(Substring(entryName, entryNameLen));
     }
     prefEntries.Sort();
     for (PRUint32 i = prefEntries.Length(); i--; ) {
       rv = pref_ReadPrefFromJar(appJarReader, prefEntries[i].get());
       if (NS_FAILED(rv))
         NS_WARNING("Error parsing preferences.");
     }
   }
--- a/modules/libpref/src/nsPrefBranch.cpp
+++ b/modules/libpref/src/nsPrefBranch.cpp
@@ -825,17 +825,17 @@ nsPrefLocalizedString::SetData(const PRU
 }
 
 NS_IMETHODIMP
 nsPrefLocalizedString::SetDataWithLength(PRUint32 aLength,
                                          const PRUnichar *aData)
 {
   if (!aData)
     return SetData(EmptyString());
-  return SetData(Substring(aData, aData + aLength));
+  return SetData(Substring(aData, aLength));
 }
 
 //----------------------------------------------------------------------------
 // nsRelativeFilePref
 //----------------------------------------------------------------------------
 
 NS_IMPL_THREADSAFE_ISUPPORTS1(nsRelativeFilePref, nsIRelativeFilePref)
 
--- a/netwerk/base/src/nsSerializationHelper.cpp
+++ b/netwerk/base/src/nsSerializationHelper.cpp
@@ -88,17 +88,17 @@ NS_DeserializeObject(const nsCSubstring&
     }
   }
   size = (size * 3) / 4;
   char* buf = PL_Base64Decode(str.BeginReading(), str.Length(), nsnull);
   if (!buf)
     return NS_ERROR_OUT_OF_MEMORY;
   nsCOMPtr<nsIInputStream> stream;
   nsresult rv = NS_NewCStringInputStream(getter_AddRefs(stream),
-                                         Substring(buf, buf + size));
+                                         Substring(buf, size));
   PR_Free(buf);
   NS_ENSURE_SUCCESS(rv, rv);
 
   nsCOMPtr<nsIObjectInputStream> objstream =
       do_CreateInstance("@mozilla.org/binaryinputstream;1");
   if (!objstream)
     return NS_ERROR_OUT_OF_MEMORY;
 
--- a/netwerk/streamconv/converters/nsIndexedToHTML.cpp
+++ b/netwerk/streamconv/converters/nsIndexedToHTML.cpp
@@ -761,17 +761,17 @@ nsIndexedToHTML::FormatInputStream(nsIRe
       if (buffer) {
         nsMemory::Free(buffer);
         buffer = nsnull;
       }
     }
 
     nsCOMPtr<nsIInputStream> inputData;
     if (buffer) {
-      rv = NS_NewCStringInputStream(getter_AddRefs(inputData), Substring(buffer, buffer + dstLength));
+      rv = NS_NewCStringInputStream(getter_AddRefs(inputData), Substring(buffer, dstLength));
       nsMemory::Free(buffer);
       NS_ENSURE_SUCCESS(rv, rv);
       rv = mListener->OnDataAvailable(aRequest, aContext,
                                       inputData, 0, dstLength);
     }
     else {
       NS_ConvertUTF16toUTF8 utf8Buffer(aBuffer);
       rv = NS_NewCStringInputStream(getter_AddRefs(inputData), utf8Buffer);
--- a/xpcom/build/nsXPCOMStrings.cpp
+++ b/xpcom/build/nsXPCOMStrings.cpp
@@ -169,17 +169,17 @@ NS_StringSetDataRange(nsAString &aStr,
   if (aCutLength == PR_UINT32_MAX)
     aCutLength = aStr.Length() - aCutOffset;
 
   if (aData)
   {
     if (aDataLength == PR_UINT32_MAX)
       aStr.Replace(aCutOffset, aCutLength, nsDependentString(aData));
     else
-      aStr.Replace(aCutOffset, aCutLength, Substring(aData, aData + aDataLength));
+      aStr.Replace(aCutOffset, aCutLength, Substring(aData, aDataLength));
   }
   else
     aStr.Cut(aCutOffset, aCutLength);
 
   return NS_OK; // XXX report errors
 }
 
 XPCOM_API(nsresult)
@@ -328,17 +328,17 @@ NS_CStringSetDataRange(nsACString &aStr,
   if (aCutLength == PR_UINT32_MAX)
     aCutLength = aStr.Length() - aCutOffset;
 
   if (aData)
   {
     if (aDataLength == PR_UINT32_MAX)
       aStr.Replace(aCutOffset, aCutLength, nsDependentCString(aData));
     else
-      aStr.Replace(aCutOffset, aCutLength, Substring(aData, aData + aDataLength));
+      aStr.Replace(aCutOffset, aCutLength, Substring(aData, aDataLength));
   }
   else
     aStr.Cut(aCutOffset, aCutLength);
 
   return NS_OK; // XXX report errors
 }
 
 XPCOM_API(nsresult)
--- a/xpcom/ds/nsVariant.cpp
+++ b/xpcom/ds/nsVariant.cpp
@@ -943,17 +943,17 @@ nsVariant::ConvertToACString(const nsDis
         return NS_OK;
     case nsIDataType::VTYPE_WSTRING_SIZE_IS:
         LossyCopyUTF16toASCII(nsDependentString(data.u.wstr.mWStringValue,
                         data.u.wstr.mWStringLength), _retval);
         return NS_OK;
     case nsIDataType::VTYPE_WCHAR:
     {
         const PRUnichar* str = &data.u.mWCharValue;
-        LossyCopyUTF16toASCII(Substring(str, str + 1), _retval);
+        LossyCopyUTF16toASCII(Substring(str, 1), _retval);
         return NS_OK;
     }
     default:
         return ToString(data, _retval);
     }
 }
 
 /* static */ nsresult
@@ -994,17 +994,17 @@ nsVariant::ConvertToAUTF8String(const ns
     case nsIDataType::VTYPE_WSTRING_SIZE_IS:
         CopyUTF16toUTF8(nsDependentString(data.u.wstr.mWStringValue,
                                           data.u.wstr.mWStringLength),
                         _retval);
         return NS_OK;
     case nsIDataType::VTYPE_WCHAR:
     {
         const PRUnichar* str = &data.u.mWCharValue;
-        CopyUTF16toUTF8(Substring(str, str + 1), _retval);
+        CopyUTF16toUTF8(Substring(str, 1), _retval);
         return NS_OK;
     }
     default:
     {
         nsCAutoString tempCString;
         nsresult rv = ToString(data, tempCString);
         if(NS_FAILED(rv))
             return rv;
--- a/xpcom/io/nsStringStream.cpp
+++ b/xpcom/io/nsStringStream.cpp
@@ -199,17 +199,17 @@ nsStringInputStream::AdoptData(char *dat
 NS_IMETHODIMP
 nsStringInputStream::ShareData(const char *data, PRInt32 dataLen)
 {
     NS_ENSURE_ARG_POINTER(data);
 
     if (dataLen < 0)
         dataLen = strlen(data);
 
-    mData.Rebind(data, data + dataLen);
+    mData.Rebind(data, dataLen);
     mOffset = 0;
     return NS_OK;
 }
 
 /////////
 // nsIInputStream implementation
 /////////
 
--- a/xpcom/string/public/nsString.h
+++ b/xpcom/string/public/nsString.h
@@ -92,17 +92,17 @@ class NS_LossyConvertUTF16toASCII : publ
       explicit
       NS_LossyConvertUTF16toASCII( const PRUnichar* aString )
         {
           LossyAppendUTF16toASCII(aString, *this);
         }
 
       NS_LossyConvertUTF16toASCII( const PRUnichar* aString, PRUint32 aLength )
         {
-          LossyAppendUTF16toASCII(Substring(aString, aString + aLength), *this);
+          LossyAppendUTF16toASCII(Substring(aString, aLength), *this);
         }
 
       explicit
       NS_LossyConvertUTF16toASCII( const nsAString& aString )
         {
           LossyAppendUTF16toASCII(aString, *this);
         }
 
@@ -118,17 +118,17 @@ class NS_ConvertASCIItoUTF16 : public ns
       explicit
       NS_ConvertASCIItoUTF16( const char* aCString )
         {
           AppendASCIItoUTF16(aCString, *this);
         }
 
       NS_ConvertASCIItoUTF16( const char* aCString, PRUint32 aLength )
         {
-          AppendASCIItoUTF16(Substring(aCString, aCString + aLength), *this);
+          AppendASCIItoUTF16(Substring(aCString, aLength), *this);
         }
 
       explicit
       NS_ConvertASCIItoUTF16( const nsACString& aCString )
         {
           AppendASCIItoUTF16(aCString, *this);
         }
 
@@ -147,17 +147,17 @@ class NS_ConvertUTF16toUTF8 : public nsC
       explicit
       NS_ConvertUTF16toUTF8( const PRUnichar* aString )
         {
           AppendUTF16toUTF8(aString, *this);
         }
 
       NS_ConvertUTF16toUTF8( const PRUnichar* aString, PRUint32 aLength )
         {
-          AppendUTF16toUTF8(Substring(aString, aString + aLength), *this);
+          AppendUTF16toUTF8(Substring(aString, aLength), *this);
         }
 
       explicit
       NS_ConvertUTF16toUTF8( const nsAString& aString )
         {
           AppendUTF16toUTF8(aString, *this);
         }
 
@@ -173,17 +173,17 @@ class NS_ConvertUTF8toUTF16 : public nsA
       explicit
       NS_ConvertUTF8toUTF16( const char* aCString )
         {
           AppendUTF8toUTF16(aCString, *this);
         }
 
       NS_ConvertUTF8toUTF16( const char* aCString, PRUint32 aLength )
         {
-          AppendUTF8toUTF16(Substring(aCString, aCString + aLength), *this);
+          AppendUTF8toUTF16(Substring(aCString, aLength), *this);
         }
 
       explicit
       NS_ConvertUTF8toUTF16( const nsACString& aCString )
         {
           AppendUTF8toUTF16(aCString, *this);
         }
 
--- a/xpcom/string/public/nsTDependentSubstring.h
+++ b/xpcom/string/public/nsTDependentSubstring.h
@@ -54,24 +54,32 @@ class nsTDependentSubstring_CharT : publ
     public:
 
       typedef nsTDependentSubstring_CharT    self_type;
 
     public:
 
       void Rebind( const substring_type&, PRUint32 startPos, PRUint32 length = size_type(-1) );
 
-      void Rebind( const char_type* start, const char_type* end );
+      void Rebind( const char_type* data, size_type length );
+
+      void Rebind( const char_type* start, const char_type* end )
+        {
+          Rebind(start, size_type(end - start));
+        }
 
       nsTDependentSubstring_CharT( const substring_type& str, PRUint32 startPos, PRUint32 length = size_type(-1) )
         : substring_type()
         {
           Rebind(str, startPos, length);
         }
 
+      nsTDependentSubstring_CharT( const char_type* data, size_type length )
+        : substring_type(const_cast<char_type*>(data), length, F_NONE) {}
+
       nsTDependentSubstring_CharT( const char_type* start, const char_type* end )
         : substring_type(const_cast<char_type*>(start), PRUint32(end - start), F_NONE) {}
 
       nsTDependentSubstring_CharT( const const_iterator& start, const const_iterator& end )
         : substring_type(const_cast<char_type*>(start.get()), PRUint32(end.get() - start.get()), F_NONE) {}
 
       // Create a nsTDependentSubstring to be bound later
       nsTDependentSubstring_CharT()
@@ -95,16 +103,23 @@ inline
 const nsTDependentSubstring_CharT
 Substring( const nsReadingIterator<CharT>& start, const nsReadingIterator<CharT>& end )
   {
     return nsTDependentSubstring_CharT(start.get(), end.get());
   }
 
 inline
 const nsTDependentSubstring_CharT
+Substring( const CharT* data, PRUint32 length )
+  {
+    return nsTDependentSubstring_CharT(data, length);
+  }
+
+inline
+const nsTDependentSubstring_CharT
 Substring( const CharT* start, const CharT* end )
   {
     return nsTDependentSubstring_CharT(start, end);
   }
 
 inline
 const nsTDependentSubstring_CharT
 StringHead( const nsTSubstring_CharT& str, PRUint32 count )
--- a/xpcom/string/src/nsTDependentSubstring.cpp
+++ b/xpcom/string/src/nsTDependentSubstring.cpp
@@ -49,19 +49,19 @@ nsTDependentSubstring_CharT::Rebind( con
 
     mData = const_cast<char_type*>(str.Data()) + startPos;
     mLength = NS_MIN(length, strLength - startPos);
 
     SetDataFlags(F_NONE);
   }
 
 void
-nsTDependentSubstring_CharT::Rebind( const char_type* start, const char_type* end )
+nsTDependentSubstring_CharT::Rebind( const char_type* data, size_type length )
   {
-    NS_ASSERTION(start && end, "nsTDependentSubstring must wrap a non-NULL buffer");
+    NS_ASSERTION(data, "nsTDependentSubstring must wrap a non-NULL buffer");
 
     // If we currently own a buffer, release it.
     Finalize();
 
-    mData = const_cast<char_type*>(start);
-    mLength = end - start;
+    mData = const_cast<char_type*>(data);
+    mLength = length;
     SetDataFlags(F_NONE);
   }
--- a/xpcom/string/src/nsTStringObsolete.cpp
+++ b/xpcom/string/src/nsTStringObsolete.cpp
@@ -474,17 +474,17 @@ nsTString_CharT::AssignWithConversion( c
       {
         Truncate();
       }
     else
       {
         if (aLength < 0)
           aLength = nsCharTraits<incompatible_char_type>::length(aData);
 
-        AssignWithConversion(Substring(aData, aData + aLength));
+        AssignWithConversion(Substring(aData, aLength));
       }
   }
 
 
   /**
    * nsTString::AppendWithConversion
    */
 
@@ -493,11 +493,11 @@ nsTString_CharT::AppendWithConversion( c
   {
       // for compatibility with the old string implementation, we need to allow
       // for a NULL input buffer :-(
     if (aData)
       {
         if (aLength < 0)
           aLength = nsCharTraits<incompatible_char_type>::length(aData);
 
-        AppendWithConversion(Substring(aData, aData + aLength));
+        AppendWithConversion(Substring(aData, aLength));
       }
   }