Bug 1362194 - Part 1: Add a fallible CopyASCIItoUTF16 function. r=mccr8, a=gchang
☠☠ backed out by e6d59d3fce9c ☠ ☠
authorNathan Froyd <froydnj@mozilla.com>
Fri, 05 May 2017 11:33:36 -0400
changeset 396226 6d27074d2779a928f29407042dc553cccfd43118
parent 396225 40c0c72fd4ef79fa8fa7acea3f53e2925124156d
child 396227 cb4ac444390794347fa89c101c8743196bf33824
push id1468
push userasasaki@mozilla.com
push dateMon, 05 Jun 2017 19:31:07 +0000
treeherdermozilla-release@0641fc6ee9d1 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmccr8, gchang
bugs1362194
milestone54.0
Bug 1362194 - Part 1: Add a fallible CopyASCIItoUTF16 function. r=mccr8, a=gchang We already have all the machinery to expose a function like this, we just need to write it.
xpcom/string/nsReadableUtils.cpp
xpcom/string/nsReadableUtils.h
--- a/xpcom/string/nsReadableUtils.cpp
+++ b/xpcom/string/nsReadableUtils.cpp
@@ -83,18 +83,29 @@ LossyCopyUTF16toASCII(const nsAString& a
 {
   aDest.Truncate();
   LossyAppendUTF16toASCII(aSource, aDest);
 }
 
 void
 CopyASCIItoUTF16(const nsACString& aSource, nsAString& aDest)
 {
+  if (!CopyASCIItoUTF16(aSource, aDest, mozilla::fallible)) {
+    // Note that this may wildly underestimate the allocation that failed, as
+    // we report the length of aSource as UTF-16 instead of UTF-8.
+    aDest.AllocFailed(aDest.Length() + aSource.Length());
+  }
+}
+
+bool
+CopyASCIItoUTF16(const nsACString& aSource, nsAString& aDest,
+                 const mozilla::fallible_t& aFallible)
+{
   aDest.Truncate();
-  AppendASCIItoUTF16(aSource, aDest);
+  return AppendASCIItoUTF16(aSource, aDest, aFallible);
 }
 
 void
 LossyCopyUTF16toASCII(const char16ptr_t aSource, nsACString& aDest)
 {
   aDest.Truncate();
   if (aSource) {
     LossyAppendUTF16toASCII(nsDependentString(aSource), aDest);
--- a/xpcom/string/nsReadableUtils.h
+++ b/xpcom/string/nsReadableUtils.h
@@ -30,16 +30,18 @@ Distance(const nsReadingIterator<char>& 
          const nsReadingIterator<char>& aEnd)
 {
   MOZ_ASSERT(aStart.get() <= aEnd.get());
   return static_cast<size_t>(aEnd.get() - aStart.get());
 }
 
 void LossyCopyUTF16toASCII(const nsAString& aSource, nsACString& aDest);
 void CopyASCIItoUTF16(const nsACString& aSource, nsAString& aDest);
+MOZ_MUST_USE bool CopyASCIItoUTF16(const nsACString& aSource, nsAString& aDest,
+                                   const mozilla::fallible_t&);
 
 void LossyCopyUTF16toASCII(const char16ptr_t aSource, nsACString& aDest);
 void CopyASCIItoUTF16(const char* aSource, nsAString& aDest);
 
 void CopyUTF16toUTF8(const nsAString& aSource, nsACString& aDest);
 MOZ_MUST_USE bool CopyUTF16toUTF8(const nsAString& aSource, nsACString& aDest,
                                   const mozilla::fallible_t&);
 void CopyUTF8toUTF16(const nsACString& aSource, nsAString& aDest);