uint32_t CalcUTF8ToUnicodeLength( const nsACString& aSource) { nsACString::const_iterator start, end; CalculateUTF8Length calculator; copy_string(aSource.BeginReading(start), aSource.EndReading(end), calculator); return calculator.Length(); }
PRUnichar* UTF8ToNewUnicode( const nsACString& aSource, PRUint32 *aUTF16Count ) { nsACString::const_iterator start, end; CalculateUTF8Length calculator; copy_string(aSource.BeginReading(start), aSource.EndReading(end), calculator); if (aUTF16Count) *aUTF16Count = calculator.Length(); PRUnichar *result = static_cast<PRUnichar*> (nsMemory::Alloc(sizeof(PRUnichar) * (calculator.Length() + 1))); if (!result) return nsnull; ConvertUTF8toUTF16 converter(result); copy_string(aSource.BeginReading(start), aSource.EndReading(end), converter).write_terminator(); NS_ASSERTION(calculator.Length() == converter.Length(), "length mismatch"); return result; }
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 (!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() || converter.Length() == count, "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; }