nsACString::const_iterator source_start, source_end; CalculateUTF8Length calculator; copy_string(aSource.BeginReading(source_start), aSource.EndReading(source_end), calculator);  PRUint32 count = calculator.Length();  if (count) { PRUint32 old_dest_length = aDest.Length();   aDest.SetLength(old_dest_length + count);  nsAString::iterator dest; aDest.BeginWriting(dest);  dest.advance(old_dest_length);  if (count <= (PRUint32)dest.size_forward()) {       ConvertUTF8toUCS2 converter(dest.get()); copy_string(aSource.BeginReading(source_start), aSource.EndReading(source_end), converter);  if (converter.Length() != count) { NS_ERROR("Input wasn't UTF8 or incorrect length was calculated"); aDest.SetLength(old_dest_length); } } else {      aDest.Replace(old_dest_length, count, NS_ConvertUTF8toUCS2(aSource)); } }
if (aSource) { AppendUTF8toUTF16(nsDependentCString(aSource), aDest); }
AppendUTF8toUTF16( const nsACString& aSource, nsAString& aDest )  {    nsACString::const_iterator source_start, source_end;    CalculateUTF8Length calculator;    copy_string(aSource.BeginReading(source_start),                aSource.EndReading(source_end), calculator);    PRUint32 count = calculator.Length();    if (count)      {        PRUint32 old_dest_length = aDest.Length();        // Grow the buffer if we need to.        aDest.SetLength(old_dest_length + count);        nsAString::iterator dest;        aDest.BeginWriting(dest);        dest.advance(old_dest_length);        if (count <= (PRUint32)dest.size_forward())          {            // aDest has enough room in the fragment just past the end            // of its old data that it can hold what we're about to            // append. Append using copy_string().            // All ready? Time to convert            ConvertUTF8toUCS2 converter(dest.get());            copy_string(aSource.BeginReading(source_start),                        aSource.EndReading(source_end), converter);            if (converter.Length() != count)              {                NS_ERROR("Input wasn't UTF8 or incorrect length was calculated");                aDest.SetLength(old_dest_length);              }          }        else          {            // This isn't the fastest way to do this, but it gets            // complicated to convert parts of a UTF8 string into a            // UTF16 string, so we'll take the easy way out here in            // this rare situation.            aDest.Replace(old_dest_length, count,                          NS_ConvertUTF8toUCS2(aSource));          }      }  }