Bug 863263 part A - Modify some code in nsReadableUtils to support fallible and infallible variations of AppendUTF8toUTF16, r=jlebar
authorBenjamin Smedberg <benjamin@smedbergs.us>
Fri, 08 Mar 2013 12:27:58 -0500
changeset 124240 917070d58a1701379ca6dfc9f82dc8ac4ac751cc
parent 124239 e25ca1ca1f17090a78dcb2fa4426bfe1978e77ba
child 124241 821013d8066deb0337b8fdc2d9789a1d0643b365
push id24412
push userryanvm@gmail.com
push dateSun, 10 Mar 2013 00:01:53 +0000
treeherdermozilla-central@9e6232e86000 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjlebar
bugs863263
milestone22.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 863263 part A - Modify some code in nsReadableUtils to support fallible and infallible variations of AppendUTF8toUTF16, r=jlebar
xpcom/string/public/nsReadableUtils.h
xpcom/string/src/nsReadableUtils.cpp
--- a/xpcom/string/public/nsReadableUtils.h
+++ b/xpcom/string/public/nsReadableUtils.h
@@ -40,16 +40,18 @@ void CopyUTF8toUTF16( const char* aSourc
 void LossyAppendUTF16toASCII( const nsAString& aSource, nsACString& aDest );
 void AppendASCIItoUTF16( const nsACString& aSource, nsAString& aDest );
 
 void LossyAppendUTF16toASCII( const PRUnichar* aSource, nsACString& aDest );
 void AppendASCIItoUTF16( const char* aSource, nsAString& aDest );
 
 void AppendUTF16toUTF8( const nsAString& aSource, nsACString& aDest );
 void AppendUTF8toUTF16( const nsACString& aSource, nsAString& aDest );
+bool AppendUTF8toUTF16( const nsACString& aSource, nsAString& aDest,
+                        const mozilla::fallible_t& ) NS_WARN_UNUSED_RESULT;
 
 void AppendUTF16toUTF8( const PRUnichar* aSource, nsACString& aDest );
 void AppendUTF8toUTF16( const char* aSource, nsAString& aDest );
 
   /**
    * Returns a new |char| buffer containing a zero-terminated copy of |aSource|.
    *
    * Allocates and returns a new |char| buffer which you must free with |nsMemory::Free|.
--- a/xpcom/string/src/nsReadableUtils.cpp
+++ b/xpcom/string/src/nsReadableUtils.cpp
@@ -63,43 +63,21 @@ CopyUTF16toUTF8( const PRUnichar* aSourc
 
 void
 CopyUTF8toUTF16( const char* aSource, nsAString& aDest )
   {
     aDest.Truncate();
     AppendUTF8toUTF16(aSource, aDest);
   }
 
-// Like GetMutableData, but returns false if it can't
-// allocate enough memory (e.g. due to OOM) rather than
-// returning zero (which could have other meanings) and
-// throws away the out-param pointer.
-bool
-SetLengthForWriting(nsAString& aDest, uint32_t aDesiredLength)
-  {
-    PRUnichar* dummy;
-    uint32_t len = aDest.GetMutableData(&dummy, aDesiredLength);
-    return (len >= aDesiredLength);
-  }
-
-bool
-SetLengthForWritingC(nsACString& aDest, uint32_t aDesiredLength)
-  {
-    char* dummy;
-    uint32_t len = aDest.GetMutableData(&dummy, aDesiredLength);
-    return (len >= aDesiredLength);
-  }
-
-
 void
 LossyAppendUTF16toASCII( const nsAString& aSource, nsACString& aDest )
   {
     uint32_t old_dest_length = aDest.Length();
-    if (!SetLengthForWritingC(aDest, old_dest_length + aSource.Length()))
-        return;
+    aDest.SetLength(old_dest_length + aSource.Length());
 
     nsAString::const_iterator fromBegin, fromEnd;
 
     nsACString::iterator dest;
     aDest.BeginWriting(dest);
 
     dest.advance(old_dest_length);
 
@@ -108,18 +86,17 @@ LossyAppendUTF16toASCII( const nsAString
 
     copy_string(aSource.BeginReading(fromBegin), aSource.EndReading(fromEnd), converter);
   }
 
 void
 AppendASCIItoUTF16( const nsACString& aSource, nsAString& aDest )
   {
     uint32_t old_dest_length = aDest.Length();
-    if (!SetLengthForWriting(aDest, old_dest_length + aSource.Length()))
-        return;
+    aDest.SetLength(old_dest_length + aSource.Length());
 
     nsACString::const_iterator fromBegin, fromEnd;
 
     nsAString::iterator dest;
     aDest.BeginWriting(dest);
 
     dest.advance(old_dest_length);
 
@@ -155,49 +132,58 @@ AppendUTF16toUTF8( const nsAString& aSou
 
     uint32_t count = calculator.Size();
 
     if (count)
       {
         uint32_t old_dest_length = aDest.Length();
 
         // Grow the buffer if we need to.
-        if(!SetLengthForWritingC(aDest, old_dest_length + count))
-            return;
+        aDest.SetLength(old_dest_length + count);
 
         // All ready? Time to convert
 
         ConvertUTF16toUTF8 converter(aDest.BeginWriting() + old_dest_length);
         copy_string(aSource.BeginReading(source_start),
                     aSource.EndReading(source_end), converter);
 
         NS_ASSERTION(converter.Size() == count,
                      "Unexpected disparity between CalculateUTF8Size and "
                      "ConvertUTF16toUTF8");
       }
   }
 
 void
 AppendUTF8toUTF16( const nsACString& aSource, nsAString& aDest )
+{
+  if (!AppendUTF8toUTF16(aSource, aDest, mozilla::fallible_t())) {
+    NS_RUNTIMEABORT("OOM");
+  }
+}
+
+bool
+AppendUTF8toUTF16( const nsACString& aSource, nsAString& aDest,
+                   const mozilla::fallible_t& )
   {
     nsACString::const_iterator source_start, source_end;
     CalculateUTF8Length calculator;
     copy_string(aSource.BeginReading(source_start),
                 aSource.EndReading(source_end), calculator);
 
     uint32_t count = calculator.Length();
 
     // Avoid making the string mutable if we're appending an empty string
     if (count)
       {
         uint32_t old_dest_length = aDest.Length();
 
         // Grow the buffer if we need to.
-        if(!SetLengthForWriting(aDest, old_dest_length + count))
-          return;
+        if (!aDest.SetLength(old_dest_length + count, mozilla::fallible_t())) {
+          return false;
+        }
 
         // All ready? Time to convert
 
         ConvertUTF8toUTF16 converter(aDest.BeginWriting() + old_dest_length);
         copy_string(aSource.BeginReading(source_start),
                     aSource.EndReading(source_end), converter);
 
         NS_ASSERTION(converter.ErrorEncountered() ||
@@ -205,16 +191,18 @@ AppendUTF8toUTF16( const nsACString& aSo
                      "CalculateUTF8Length produced the wrong length");
 
         if (converter.ErrorEncountered())
           {
             NS_ERROR("Input wasn't UTF8 or incorrect length was calculated");
             aDest.SetLength(old_dest_length);
           }
       }
+
+    return true;
   }
 
 void
 AppendUTF16toUTF8( const PRUnichar* aSource, nsACString& aDest )
   {
     if (aSource) {
       AppendUTF16toUTF8(nsDependentString(aSource), aDest);
     }
@@ -376,34 +364,32 @@ CopyUnicodeTo( const nsAString& aSource,
   }
 
 void
 CopyUnicodeTo( const nsAString::const_iterator& aSrcStart,
                const nsAString::const_iterator& aSrcEnd,
                nsAString& aDest )
   {
     nsAString::iterator writer;
-    if (!SetLengthForWriting(aDest, Distance(aSrcStart, aSrcEnd)))
-        return;
+    aDest.SetLength(Distance(aSrcStart, aSrcEnd));
 
     aDest.BeginWriting(writer);
     nsAString::const_iterator fromBegin(aSrcStart);
     
     copy_string(fromBegin, aSrcEnd, writer);
   }
 
 void
 AppendUnicodeTo( const nsAString::const_iterator& aSrcStart,
                  const nsAString::const_iterator& aSrcEnd,
                  nsAString& aDest )
   {
     nsAString::iterator writer;
     uint32_t oldLength = aDest.Length();
-    if(!SetLengthForWriting(aDest, oldLength + Distance(aSrcStart, aSrcEnd)))
-        return;
+    aDest.SetLength(oldLength + Distance(aSrcStart, aSrcEnd));
 
     aDest.BeginWriting(writer).advance(oldLength);
     nsAString::const_iterator fromBegin(aSrcStart);
     
     copy_string(fromBegin, aSrcEnd, writer);
   }
 
 bool
@@ -615,18 +601,17 @@ class CopyToUpperCase
       nsACString::iterator& mIter;
   };
 
 void
 ToUpperCase( const nsACString& aSource, nsACString& aDest )
   {
     nsACString::const_iterator fromBegin, fromEnd;
     nsACString::iterator toBegin;
-    if (!SetLengthForWritingC(aDest, aSource.Length()))
-        return;
+    aDest.SetLength(aSource.Length());
 
     CopyToUpperCase converter(aDest.BeginWriting(toBegin));
     copy_string(aSource.BeginReading(fromBegin), aSource.EndReading(fromEnd), converter);
   }
 
   /**
    * A character sink for case conversion.
    */
@@ -694,18 +679,17 @@ class CopyToLowerCase
       nsACString::iterator& mIter;
   };
 
 void
 ToLowerCase( const nsACString& aSource, nsACString& aDest )
   {
     nsACString::const_iterator fromBegin, fromEnd;
     nsACString::iterator toBegin;
-    if (!SetLengthForWritingC(aDest, aSource.Length()))
-        return;
+    aDest.SetLength(aSource.Length());
 
     CopyToLowerCase converter(aDest.BeginWriting(toBegin));
     copy_string(aSource.BeginReading(fromBegin), aSource.EndReading(fromEnd), converter);
   }
 
 bool
 ParseString(const nsACString& aSource, char aDelimiter, 
             nsTArray<nsCString>& aArray)