2014-05-05 10:30:46 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2012-05-21 04:12:37 -07:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
#include "nsReadableUtils.h"
|
2013-03-17 00:55:16 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsMemory.h"
|
|
|
|
#include "nsString.h"
|
2013-03-17 00:55:16 -07:00
|
|
|
#include "nsTArray.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsUTF8Utils.h"
|
|
|
|
|
|
|
|
void
|
2014-05-27 00:15:35 -07:00
|
|
|
LossyCopyUTF16toASCII(const nsAString& aSource, nsACString& aDest)
|
2014-05-05 10:30:46 -07:00
|
|
|
{
|
|
|
|
aDest.Truncate();
|
|
|
|
LossyAppendUTF16toASCII(aSource, aDest);
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
void
|
2014-05-27 00:15:35 -07:00
|
|
|
CopyASCIItoUTF16(const nsACString& aSource, nsAString& aDest)
|
2014-05-05 10:30:46 -07:00
|
|
|
{
|
|
|
|
aDest.Truncate();
|
|
|
|
AppendASCIItoUTF16(aSource, aDest);
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
void
|
2014-05-27 00:15:35 -07:00
|
|
|
LossyCopyUTF16toASCII(const char16_t* aSource, nsACString& aDest)
|
2014-05-05 10:30:46 -07:00
|
|
|
{
|
|
|
|
aDest.Truncate();
|
|
|
|
if (aSource) {
|
|
|
|
LossyAppendUTF16toASCII(nsDependentString(aSource), aDest);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2014-05-05 10:30:46 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
void
|
2014-05-27 00:15:35 -07:00
|
|
|
CopyASCIItoUTF16(const char* aSource, nsAString& aDest)
|
2014-05-05 10:30:46 -07:00
|
|
|
{
|
|
|
|
aDest.Truncate();
|
|
|
|
if (aSource) {
|
|
|
|
AppendASCIItoUTF16(nsDependentCString(aSource), aDest);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2014-05-05 10:30:46 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
void
|
2014-05-27 00:15:35 -07:00
|
|
|
CopyUTF16toUTF8(const nsAString& aSource, nsACString& aDest)
|
2014-05-05 10:30:46 -07:00
|
|
|
{
|
|
|
|
aDest.Truncate();
|
|
|
|
AppendUTF16toUTF8(aSource, aDest);
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
void
|
2014-05-27 00:15:35 -07:00
|
|
|
CopyUTF8toUTF16(const nsACString& aSource, nsAString& aDest)
|
2014-05-05 10:30:46 -07:00
|
|
|
{
|
|
|
|
aDest.Truncate();
|
|
|
|
AppendUTF8toUTF16(aSource, aDest);
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
void
|
2014-05-27 00:15:35 -07:00
|
|
|
CopyUTF16toUTF8(const char16_t* aSource, nsACString& aDest)
|
2014-05-05 10:30:46 -07:00
|
|
|
{
|
|
|
|
aDest.Truncate();
|
|
|
|
AppendUTF16toUTF8(aSource, aDest);
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
void
|
2014-05-27 00:15:35 -07:00
|
|
|
CopyUTF8toUTF16(const char* aSource, nsAString& aDest)
|
2014-05-05 10:30:46 -07:00
|
|
|
{
|
|
|
|
aDest.Truncate();
|
|
|
|
AppendUTF8toUTF16(aSource, aDest);
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
void
|
2014-05-27 00:15:35 -07:00
|
|
|
LossyAppendUTF16toASCII(const nsAString& aSource, nsACString& aDest)
|
2014-05-05 10:30:46 -07:00
|
|
|
{
|
|
|
|
uint32_t old_dest_length = aDest.Length();
|
|
|
|
aDest.SetLength(old_dest_length + aSource.Length());
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:46 -07:00
|
|
|
nsAString::const_iterator fromBegin, fromEnd;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:46 -07:00
|
|
|
nsACString::iterator dest;
|
|
|
|
aDest.BeginWriting(dest);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:46 -07:00
|
|
|
dest.advance(old_dest_length);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:46 -07:00
|
|
|
// right now, this won't work on multi-fragment destinations
|
|
|
|
LossyConvertEncoding16to8 converter(dest.get());
|
2010-08-13 09:15:44 -07:00
|
|
|
|
2014-08-25 12:17:15 -07:00
|
|
|
copy_string(aSource.BeginReading(fromBegin), aSource.EndReading(fromEnd),
|
|
|
|
converter);
|
2014-05-05 10:30:46 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
void
|
2014-05-27 00:15:35 -07:00
|
|
|
AppendASCIItoUTF16(const nsACString& aSource, nsAString& aDest)
|
2014-05-05 10:30:46 -07:00
|
|
|
{
|
|
|
|
if (!AppendASCIItoUTF16(aSource, aDest, mozilla::fallible_t())) {
|
|
|
|
NS_ABORT_OOM(aDest.Length() + aSource.Length());
|
2014-02-11 06:22:45 -08:00
|
|
|
}
|
2014-05-05 10:30:46 -07:00
|
|
|
}
|
2014-02-11 06:22:45 -08:00
|
|
|
|
|
|
|
bool
|
2014-05-27 00:15:35 -07:00
|
|
|
AppendASCIItoUTF16(const nsACString& aSource, nsAString& aDest,
|
|
|
|
const mozilla::fallible_t&)
|
2014-05-05 10:30:46 -07:00
|
|
|
{
|
|
|
|
uint32_t old_dest_length = aDest.Length();
|
2014-08-25 12:17:15 -07:00
|
|
|
if (!aDest.SetLength(old_dest_length + aSource.Length(),
|
|
|
|
mozilla::fallible_t())) {
|
2014-05-05 10:30:46 -07:00
|
|
|
return false;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:46 -07:00
|
|
|
nsACString::const_iterator fromBegin, fromEnd;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:46 -07:00
|
|
|
nsAString::iterator dest;
|
|
|
|
aDest.BeginWriting(dest);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:46 -07:00
|
|
|
dest.advance(old_dest_length);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:46 -07:00
|
|
|
// right now, this won't work on multi-fragment destinations
|
|
|
|
LossyConvertEncoding8to16 converter(dest.get());
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-08-25 12:17:15 -07:00
|
|
|
copy_string(aSource.BeginReading(fromBegin), aSource.EndReading(fromEnd),
|
|
|
|
converter);
|
2014-05-05 10:30:46 -07:00
|
|
|
return true;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
void
|
2014-05-27 00:15:35 -07:00
|
|
|
LossyAppendUTF16toASCII(const char16_t* aSource, nsACString& aDest)
|
2014-05-05 10:30:46 -07:00
|
|
|
{
|
|
|
|
if (aSource) {
|
|
|
|
LossyAppendUTF16toASCII(nsDependentString(aSource), aDest);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2014-05-05 10:30:46 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
void
|
2014-05-27 00:15:35 -07:00
|
|
|
AppendASCIItoUTF16(const char* aSource, nsAString& aDest)
|
2014-05-05 10:30:46 -07:00
|
|
|
{
|
|
|
|
if (aSource) {
|
|
|
|
AppendASCIItoUTF16(nsDependentCString(aSource), aDest);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2014-05-05 10:30:46 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
void
|
2014-05-27 00:15:35 -07:00
|
|
|
AppendUTF16toUTF8(const nsAString& aSource, nsACString& aDest)
|
2014-01-07 07:18:30 -08:00
|
|
|
{
|
|
|
|
if (!AppendUTF16toUTF8(aSource, aDest, mozilla::fallible_t())) {
|
|
|
|
NS_ABORT_OOM(aDest.Length() + aSource.Length());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2014-05-27 00:15:35 -07:00
|
|
|
AppendUTF16toUTF8(const nsAString& aSource, nsACString& aDest,
|
|
|
|
const mozilla::fallible_t&)
|
2014-05-05 10:30:46 -07:00
|
|
|
{
|
|
|
|
nsAString::const_iterator source_start, source_end;
|
|
|
|
CalculateUTF8Size calculator;
|
|
|
|
copy_string(aSource.BeginReading(source_start),
|
|
|
|
aSource.EndReading(source_end), calculator);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:46 -07:00
|
|
|
uint32_t count = calculator.Size();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-27 00:15:35 -07:00
|
|
|
if (count) {
|
2014-05-05 10:30:46 -07:00
|
|
|
uint32_t old_dest_length = aDest.Length();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:46 -07:00
|
|
|
// Grow the buffer if we need to.
|
|
|
|
if (!aDest.SetLength(old_dest_length + count, mozilla::fallible_t())) {
|
|
|
|
return false;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:46 -07:00
|
|
|
// All ready? Time to convert
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:46 -07:00
|
|
|
ConvertUTF16toUTF8 converter(aDest.BeginWriting() + old_dest_length);
|
|
|
|
copy_string(aSource.BeginReading(source_start),
|
|
|
|
aSource.EndReading(source_end), converter);
|
2014-01-07 07:18:30 -08:00
|
|
|
|
2014-05-05 10:30:46 -07:00
|
|
|
NS_ASSERTION(converter.Size() == count,
|
|
|
|
"Unexpected disparity between CalculateUTF8Size and "
|
|
|
|
"ConvertUTF16toUTF8");
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2014-05-05 10:30:46 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
void
|
2014-05-27 00:15:35 -07:00
|
|
|
AppendUTF8toUTF16(const nsACString& aSource, nsAString& aDest)
|
2013-03-08 09:27:58 -08:00
|
|
|
{
|
|
|
|
if (!AppendUTF8toUTF16(aSource, aDest, mozilla::fallible_t())) {
|
2013-11-25 12:06:17 -08:00
|
|
|
NS_ABORT_OOM(aDest.Length() + aSource.Length());
|
2013-03-08 09:27:58 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2014-05-27 00:15:35 -07:00
|
|
|
AppendUTF8toUTF16(const nsACString& aSource, nsAString& aDest,
|
|
|
|
const mozilla::fallible_t&)
|
2014-05-05 10:30:46 -07:00
|
|
|
{
|
|
|
|
nsACString::const_iterator source_start, source_end;
|
|
|
|
CalculateUTF8Length calculator;
|
|
|
|
copy_string(aSource.BeginReading(source_start),
|
|
|
|
aSource.EndReading(source_end), calculator);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:46 -07:00
|
|
|
uint32_t count = calculator.Length();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:46 -07:00
|
|
|
// Avoid making the string mutable if we're appending an empty string
|
2014-05-27 00:15:35 -07:00
|
|
|
if (count) {
|
2014-05-05 10:30:46 -07:00
|
|
|
uint32_t old_dest_length = aDest.Length();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:46 -07:00
|
|
|
// Grow the buffer if we need to.
|
|
|
|
if (!aDest.SetLength(old_dest_length + count, mozilla::fallible_t())) {
|
|
|
|
return false;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:46 -07:00
|
|
|
// All ready? Time to convert
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:46 -07:00
|
|
|
ConvertUTF8toUTF16 converter(aDest.BeginWriting() + old_dest_length);
|
|
|
|
copy_string(aSource.BeginReading(source_start),
|
|
|
|
aSource.EndReading(source_end), converter);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:46 -07:00
|
|
|
NS_ASSERTION(converter.ErrorEncountered() ||
|
|
|
|
converter.Length() == count,
|
|
|
|
"CalculateUTF8Length produced the wrong length");
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-27 00:15:35 -07:00
|
|
|
if (converter.ErrorEncountered()) {
|
2014-05-05 10:30:46 -07:00
|
|
|
NS_ERROR("Input wasn't UTF8 or incorrect length was calculated");
|
|
|
|
aDest.SetLength(old_dest_length);
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2014-05-05 10:30:46 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
void
|
2014-05-27 00:15:35 -07:00
|
|
|
AppendUTF16toUTF8(const char16_t* aSource, nsACString& aDest)
|
2014-05-05 10:30:46 -07:00
|
|
|
{
|
|
|
|
if (aSource) {
|
|
|
|
AppendUTF16toUTF8(nsDependentString(aSource), aDest);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2014-05-05 10:30:46 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
void
|
2014-05-27 00:15:35 -07:00
|
|
|
AppendUTF8toUTF16(const char* aSource, nsAString& aDest)
|
2014-05-05 10:30:46 -07:00
|
|
|
{
|
|
|
|
if (aSource) {
|
|
|
|
AppendUTF8toUTF16(nsDependentCString(aSource), aDest);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2014-05-05 10:30:46 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
|
2014-05-05 10:30:46 -07:00
|
|
|
/**
|
|
|
|
* A helper function that allocates a buffer of the desired character type big enough to hold a copy of the supplied string (plus a zero terminator).
|
|
|
|
*
|
|
|
|
* @param aSource an string you will eventually be making a copy of
|
|
|
|
* @return a new buffer (of the type specified by the second parameter) which you must free with |nsMemory::Free|.
|
|
|
|
*
|
|
|
|
*/
|
2007-03-22 10:30:00 -07:00
|
|
|
template <class FromStringT, class ToCharT>
|
|
|
|
inline
|
|
|
|
ToCharT*
|
2014-05-27 00:15:35 -07:00
|
|
|
AllocateStringCopy(const FromStringT& aSource, ToCharT*)
|
2014-05-05 10:30:46 -07:00
|
|
|
{
|
2014-08-25 12:17:15 -07:00
|
|
|
return static_cast<ToCharT*>(nsMemory::Alloc(
|
|
|
|
(aSource.Length() + 1) * sizeof(ToCharT)));
|
2014-05-05 10:30:46 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
|
|
|
|
char*
|
2014-05-27 00:15:35 -07:00
|
|
|
ToNewCString(const nsAString& aSource)
|
2014-05-05 10:30:46 -07:00
|
|
|
{
|
|
|
|
char* result = AllocateStringCopy(aSource, (char*)0);
|
2014-05-27 00:15:35 -07:00
|
|
|
if (!result) {
|
2014-05-05 10:30:46 -07:00
|
|
|
return nullptr;
|
2014-05-27 00:15:35 -07:00
|
|
|
}
|
2014-05-05 10:30:46 -07:00
|
|
|
|
|
|
|
nsAString::const_iterator fromBegin, fromEnd;
|
|
|
|
LossyConvertEncoding16to8 converter(result);
|
2014-05-27 00:15:35 -07:00
|
|
|
copy_string(aSource.BeginReading(fromBegin), aSource.EndReading(fromEnd),
|
|
|
|
converter).write_terminator();
|
2014-05-05 10:30:46 -07:00
|
|
|
return result;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
char*
|
2014-05-27 00:15:35 -07:00
|
|
|
ToNewUTF8String(const nsAString& aSource, uint32_t* aUTF8Count)
|
2014-05-05 10:30:46 -07:00
|
|
|
{
|
|
|
|
nsAString::const_iterator start, end;
|
|
|
|
CalculateUTF8Size calculator;
|
|
|
|
copy_string(aSource.BeginReading(start), aSource.EndReading(end),
|
|
|
|
calculator);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-27 00:15:35 -07:00
|
|
|
if (aUTF8Count) {
|
2014-05-05 10:30:46 -07:00
|
|
|
*aUTF8Count = calculator.Size();
|
2014-05-27 00:15:35 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-27 00:15:35 -07:00
|
|
|
char* result = static_cast<char*>
|
2014-05-05 10:30:46 -07:00
|
|
|
(nsMemory::Alloc(calculator.Size() + 1));
|
2014-05-27 00:15:35 -07:00
|
|
|
if (!result) {
|
2014-05-05 10:30:46 -07:00
|
|
|
return nullptr;
|
2014-05-27 00:15:35 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:46 -07:00
|
|
|
ConvertUTF16toUTF8 converter(result);
|
|
|
|
copy_string(aSource.BeginReading(start), aSource.EndReading(end),
|
|
|
|
converter).write_terminator();
|
|
|
|
NS_ASSERTION(calculator.Size() == converter.Size(), "length mismatch");
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:46 -07:00
|
|
|
return result;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
char*
|
2014-05-27 00:15:35 -07:00
|
|
|
ToNewCString(const nsACString& aSource)
|
2014-05-05 10:30:46 -07:00
|
|
|
{
|
|
|
|
// no conversion needed, just allocate a buffer of the correct length and copy into it
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:46 -07:00
|
|
|
char* result = AllocateStringCopy(aSource, (char*)0);
|
2014-05-27 00:15:35 -07:00
|
|
|
if (!result) {
|
2014-05-05 10:30:46 -07:00
|
|
|
return nullptr;
|
2014-05-27 00:15:35 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:46 -07:00
|
|
|
nsACString::const_iterator fromBegin, fromEnd;
|
|
|
|
char* toBegin = result;
|
2014-08-25 12:17:15 -07:00
|
|
|
*copy_string(aSource.BeginReading(fromBegin), aSource.EndReading(fromEnd),
|
|
|
|
toBegin) = char(0);
|
2014-05-05 10:30:46 -07:00
|
|
|
return result;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-01-04 07:02:17 -08:00
|
|
|
char16_t*
|
2014-05-27 00:15:35 -07:00
|
|
|
ToNewUnicode(const nsAString& aSource)
|
2014-05-05 10:30:46 -07:00
|
|
|
{
|
|
|
|
// no conversion needed, just allocate a buffer of the correct length and copy into it
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:46 -07:00
|
|
|
char16_t* result = AllocateStringCopy(aSource, (char16_t*)0);
|
2014-05-27 00:15:35 -07:00
|
|
|
if (!result) {
|
2014-05-05 10:30:46 -07:00
|
|
|
return nullptr;
|
2014-05-27 00:15:35 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:46 -07:00
|
|
|
nsAString::const_iterator fromBegin, fromEnd;
|
|
|
|
char16_t* toBegin = result;
|
2014-08-25 12:17:15 -07:00
|
|
|
*copy_string(aSource.BeginReading(fromBegin), aSource.EndReading(fromEnd),
|
|
|
|
toBegin) = char16_t(0);
|
2014-05-05 10:30:46 -07:00
|
|
|
return result;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-01-04 07:02:17 -08:00
|
|
|
char16_t*
|
2014-05-27 00:15:35 -07:00
|
|
|
ToNewUnicode(const nsACString& aSource)
|
2014-05-05 10:30:46 -07:00
|
|
|
{
|
|
|
|
char16_t* result = AllocateStringCopy(aSource, (char16_t*)0);
|
2014-05-27 00:15:35 -07:00
|
|
|
if (!result) {
|
2014-05-05 10:30:46 -07:00
|
|
|
return nullptr;
|
2014-05-27 00:15:35 -07:00
|
|
|
}
|
2014-05-05 10:30:46 -07:00
|
|
|
|
|
|
|
nsACString::const_iterator fromBegin, fromEnd;
|
|
|
|
LossyConvertEncoding8to16 converter(result);
|
2014-05-27 00:15:35 -07:00
|
|
|
copy_string(aSource.BeginReading(fromBegin), aSource.EndReading(fromEnd),
|
|
|
|
converter).write_terminator();
|
2014-05-05 10:30:46 -07:00
|
|
|
return result;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-01-29 07:50:04 -08:00
|
|
|
uint32_t
|
2014-05-27 00:15:35 -07:00
|
|
|
CalcUTF8ToUnicodeLength(const nsACString& aSource)
|
2014-05-05 10:30:46 -07:00
|
|
|
{
|
|
|
|
nsACString::const_iterator start, end;
|
|
|
|
CalculateUTF8Length calculator;
|
|
|
|
copy_string(aSource.BeginReading(start), aSource.EndReading(end),
|
|
|
|
calculator);
|
|
|
|
return calculator.Length();
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-01-04 07:02:17 -08:00
|
|
|
char16_t*
|
2014-05-27 00:15:35 -07:00
|
|
|
UTF8ToUnicodeBuffer(const nsACString& aSource, char16_t* aBuffer,
|
|
|
|
uint32_t* aUTF16Count)
|
2014-05-05 10:30:46 -07:00
|
|
|
{
|
|
|
|
nsACString::const_iterator start, end;
|
|
|
|
ConvertUTF8toUTF16 converter(aBuffer);
|
|
|
|
copy_string(aSource.BeginReading(start),
|
|
|
|
aSource.EndReading(end),
|
|
|
|
converter).write_terminator();
|
2014-05-27 00:15:35 -07:00
|
|
|
if (aUTF16Count) {
|
2014-05-05 10:30:46 -07:00
|
|
|
*aUTF16Count = converter.Length();
|
2014-05-27 00:15:35 -07:00
|
|
|
}
|
2014-05-05 10:30:46 -07:00
|
|
|
return aBuffer;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-01-04 07:02:17 -08:00
|
|
|
char16_t*
|
2014-05-27 00:15:35 -07:00
|
|
|
UTF8ToNewUnicode(const nsACString& aSource, uint32_t* aUTF16Count)
|
2014-05-05 10:30:46 -07:00
|
|
|
{
|
|
|
|
const uint32_t length = CalcUTF8ToUnicodeLength(aSource);
|
|
|
|
const size_t buffer_size = (length + 1) * sizeof(char16_t);
|
2014-05-27 00:15:35 -07:00
|
|
|
char16_t* buffer = static_cast<char16_t*>(nsMemory::Alloc(buffer_size));
|
|
|
|
if (!buffer) {
|
2014-05-05 10:30:46 -07:00
|
|
|
return nullptr;
|
2014-05-27 00:15:35 -07:00
|
|
|
}
|
2014-05-05 10:30:46 -07:00
|
|
|
|
|
|
|
uint32_t copied;
|
|
|
|
UTF8ToUnicodeBuffer(aSource, buffer, &copied);
|
|
|
|
NS_ASSERTION(length == copied, "length mismatch");
|
|
|
|
|
2014-05-27 00:15:35 -07:00
|
|
|
if (aUTF16Count) {
|
2014-05-05 10:30:46 -07:00
|
|
|
*aUTF16Count = copied;
|
2014-05-27 00:15:35 -07:00
|
|
|
}
|
2014-05-05 10:30:46 -07:00
|
|
|
return buffer;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-01-04 07:02:17 -08:00
|
|
|
char16_t*
|
2014-05-27 00:15:35 -07:00
|
|
|
CopyUnicodeTo(const nsAString& aSource, uint32_t aSrcOffset, char16_t* aDest,
|
|
|
|
uint32_t aLength)
|
2014-05-05 10:30:46 -07:00
|
|
|
{
|
|
|
|
nsAString::const_iterator fromBegin, fromEnd;
|
|
|
|
char16_t* toBegin = aDest;
|
2014-05-27 00:15:35 -07:00
|
|
|
copy_string(aSource.BeginReading(fromBegin).advance(int32_t(aSrcOffset)),
|
|
|
|
aSource.BeginReading(fromEnd).advance(int32_t(aSrcOffset + aLength)),
|
|
|
|
toBegin);
|
2014-05-05 10:30:46 -07:00
|
|
|
return aDest;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-08-18 06:46:39 -07:00
|
|
|
void
|
2014-05-27 00:15:35 -07:00
|
|
|
CopyUnicodeTo(const nsAString::const_iterator& aSrcStart,
|
|
|
|
const nsAString::const_iterator& aSrcEnd,
|
|
|
|
nsAString& aDest)
|
2014-05-05 10:30:46 -07:00
|
|
|
{
|
|
|
|
nsAString::iterator writer;
|
|
|
|
aDest.SetLength(Distance(aSrcStart, aSrcEnd));
|
2007-07-02 23:03:11 -07:00
|
|
|
|
2014-05-05 10:30:46 -07:00
|
|
|
aDest.BeginWriting(writer);
|
|
|
|
nsAString::const_iterator fromBegin(aSrcStart);
|
|
|
|
|
|
|
|
copy_string(fromBegin, aSrcEnd, writer);
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-08-18 06:46:39 -07:00
|
|
|
void
|
2014-05-27 00:15:35 -07:00
|
|
|
AppendUnicodeTo(const nsAString::const_iterator& aSrcStart,
|
|
|
|
const nsAString::const_iterator& aSrcEnd,
|
|
|
|
nsAString& aDest)
|
2014-05-05 10:30:46 -07:00
|
|
|
{
|
|
|
|
nsAString::iterator writer;
|
|
|
|
uint32_t oldLength = aDest.Length();
|
|
|
|
aDest.SetLength(oldLength + Distance(aSrcStart, aSrcEnd));
|
|
|
|
|
|
|
|
aDest.BeginWriting(writer).advance(oldLength);
|
|
|
|
nsAString::const_iterator fromBegin(aSrcStart);
|
|
|
|
|
|
|
|
copy_string(fromBegin, aSrcEnd, writer);
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2014-05-27 00:15:35 -07:00
|
|
|
IsASCII(const nsAString& aString)
|
2014-05-05 10:30:46 -07:00
|
|
|
{
|
|
|
|
static const char16_t NOT_ASCII = char16_t(~0x007F);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
|
2014-05-05 10:30:46 -07:00
|
|
|
// Don't want to use |copy_string| for this task, since we can stop at the first non-ASCII character
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:46 -07:00
|
|
|
nsAString::const_iterator iter, done_reading;
|
|
|
|
aString.BeginReading(iter);
|
|
|
|
aString.EndReading(done_reading);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:46 -07:00
|
|
|
const char16_t* c = iter.get();
|
|
|
|
const char16_t* end = done_reading.get();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-27 00:15:35 -07:00
|
|
|
while (c < end) {
|
|
|
|
if (*c++ & NOT_ASCII) {
|
2014-05-05 10:30:46 -07:00
|
|
|
return false;
|
2014-05-27 00:15:35 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2014-05-05 10:30:46 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2014-05-27 00:15:35 -07:00
|
|
|
IsASCII(const nsACString& aString)
|
2014-05-05 10:30:46 -07:00
|
|
|
{
|
|
|
|
static const char NOT_ASCII = char(~0x7F);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
|
2014-05-05 10:30:46 -07:00
|
|
|
// Don't want to use |copy_string| for this task, since we can stop at the first non-ASCII character
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:46 -07:00
|
|
|
nsACString::const_iterator iter, done_reading;
|
|
|
|
aString.BeginReading(iter);
|
|
|
|
aString.EndReading(done_reading);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:46 -07:00
|
|
|
const char* c = iter.get();
|
|
|
|
const char* end = done_reading.get();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-27 00:15:35 -07:00
|
|
|
while (c < end) {
|
|
|
|
if (*c++ & NOT_ASCII) {
|
2014-05-05 10:30:46 -07:00
|
|
|
return false;
|
2014-05-27 00:15:35 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2014-05-05 10:30:46 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2014-05-27 00:15:35 -07:00
|
|
|
IsUTF8(const nsACString& aString, bool aRejectNonChar)
|
2014-05-05 10:30:46 -07:00
|
|
|
{
|
|
|
|
nsReadingIterator<char> done_reading;
|
|
|
|
aString.EndReading(done_reading);
|
|
|
|
|
|
|
|
int32_t state = 0;
|
|
|
|
bool overlong = false;
|
|
|
|
bool surrogate = false;
|
|
|
|
bool nonchar = false;
|
|
|
|
uint16_t olupper = 0; // overlong byte upper bound.
|
|
|
|
uint16_t slower = 0; // surrogate byte lower bound.
|
|
|
|
|
|
|
|
nsReadingIterator<char> iter;
|
|
|
|
aString.BeginReading(iter);
|
|
|
|
|
|
|
|
const char* ptr = iter.get();
|
|
|
|
const char* end = done_reading.get();
|
2014-05-27 00:15:35 -07:00
|
|
|
while (ptr < end) {
|
2014-05-05 10:30:46 -07:00
|
|
|
uint8_t c;
|
|
|
|
|
2014-05-27 00:15:35 -07:00
|
|
|
if (0 == state) {
|
2014-05-05 10:30:46 -07:00
|
|
|
c = *ptr++;
|
|
|
|
|
2014-05-27 00:15:35 -07:00
|
|
|
if (UTF8traits::isASCII(c)) {
|
2014-05-05 10:30:46 -07:00
|
|
|
continue;
|
2014-05-27 00:15:35 -07:00
|
|
|
}
|
2014-05-05 10:30:46 -07:00
|
|
|
|
2014-05-27 00:15:35 -07:00
|
|
|
if (c <= 0xC1) { // [80-BF] where not expected, [C0-C1] for overlong.
|
2014-05-05 10:30:46 -07:00
|
|
|
return false;
|
2014-05-27 00:15:35 -07:00
|
|
|
} else if (UTF8traits::is2byte(c)) {
|
2014-05-05 10:30:46 -07:00
|
|
|
state = 1;
|
2014-05-27 00:15:35 -07:00
|
|
|
} else if (UTF8traits::is3byte(c)) {
|
2014-05-05 10:30:46 -07:00
|
|
|
state = 2;
|
2014-05-27 00:15:35 -07:00
|
|
|
if (c == 0xE0) { // to exclude E0[80-9F][80-BF]
|
2014-05-05 10:30:46 -07:00
|
|
|
overlong = true;
|
|
|
|
olupper = 0x9F;
|
2014-05-27 00:15:35 -07:00
|
|
|
} else if (c == 0xED) { // ED[A0-BF][80-BF] : surrogate codepoint
|
2014-05-05 10:30:46 -07:00
|
|
|
surrogate = true;
|
|
|
|
slower = 0xA0;
|
2014-05-27 00:15:35 -07:00
|
|
|
} else if (c == 0xEF) { // EF BF [BE-BF] : non-character
|
2014-05-05 10:30:46 -07:00
|
|
|
nonchar = true;
|
2014-05-27 00:15:35 -07:00
|
|
|
}
|
|
|
|
} else if (c <= 0xF4) { // XXX replace /w UTF8traits::is4byte when it's updated to exclude [F5-F7].(bug 199090)
|
2014-05-05 10:30:46 -07:00
|
|
|
state = 3;
|
|
|
|
nonchar = true;
|
2014-05-27 00:15:35 -07:00
|
|
|
if (c == 0xF0) { // to exclude F0[80-8F][80-BF]{2}
|
2014-05-05 10:30:46 -07:00
|
|
|
overlong = true;
|
|
|
|
olupper = 0x8F;
|
2014-05-27 00:15:35 -07:00
|
|
|
} else if (c == 0xF4) { // to exclude F4[90-BF][80-BF]
|
2014-05-05 10:30:46 -07:00
|
|
|
// actually not surrogates but codepoints beyond 0x10FFFF
|
|
|
|
surrogate = true;
|
|
|
|
slower = 0x90;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2014-05-27 00:15:35 -07:00
|
|
|
} else {
|
|
|
|
return false; // Not UTF-8 string
|
2014-05-05 10:30:46 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-27 00:15:35 -07:00
|
|
|
if (nonchar && !aRejectNonChar) {
|
2014-05-05 10:30:46 -07:00
|
|
|
nonchar = false;
|
2014-05-27 00:15:35 -07:00
|
|
|
}
|
2014-05-05 10:30:46 -07:00
|
|
|
|
2014-05-27 00:15:35 -07:00
|
|
|
while (ptr < end && state) {
|
2014-05-05 10:30:46 -07:00
|
|
|
c = *ptr++;
|
|
|
|
--state;
|
|
|
|
|
|
|
|
// non-character : EF BF [BE-BF] or F[0-7] [89AB]F BF [BE-BF]
|
2014-05-27 00:15:35 -07:00
|
|
|
if (nonchar &&
|
|
|
|
((!state && c < 0xBE) ||
|
|
|
|
(state == 1 && c != 0xBF) ||
|
|
|
|
(state == 2 && 0x0F != (0x0F & c)))) {
|
2014-05-05 10:30:46 -07:00
|
|
|
nonchar = false;
|
2014-05-27 00:15:35 -07:00
|
|
|
}
|
2014-05-05 10:30:46 -07:00
|
|
|
|
2014-05-27 00:15:35 -07:00
|
|
|
if (!UTF8traits::isInSeq(c) || (overlong && c <= olupper) ||
|
|
|
|
(surrogate && slower <= c) || (nonchar && !state)) {
|
|
|
|
return false; // Not UTF-8 string
|
|
|
|
}
|
2014-05-05 10:30:46 -07:00
|
|
|
|
|
|
|
overlong = surrogate = false;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2014-05-05 10:30:46 -07:00
|
|
|
return !state; // state != 0 at the end indicates an invalid UTF-8 seq.
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:46 -07:00
|
|
|
/**
|
|
|
|
* A character sink for in-place case conversion.
|
|
|
|
*/
|
2007-03-22 10:30:00 -07:00
|
|
|
class ConvertToUpperCase
|
2014-05-05 10:30:46 -07:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
typedef char value_type;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:46 -07:00
|
|
|
uint32_t
|
2014-05-27 00:15:35 -07:00
|
|
|
write(const char* aSource, uint32_t aSourceLength)
|
2014-05-05 10:30:46 -07:00
|
|
|
{
|
|
|
|
char* cp = const_cast<char*>(aSource);
|
|
|
|
const char* end = aSource + aSourceLength;
|
|
|
|
while (cp != end) {
|
|
|
|
char ch = *cp;
|
2014-05-27 00:15:35 -07:00
|
|
|
if (ch >= 'a' && ch <= 'z') {
|
2014-05-05 10:30:46 -07:00
|
|
|
*cp = ch - ('a' - 'A');
|
2014-05-27 00:15:35 -07:00
|
|
|
}
|
2014-05-05 10:30:46 -07:00
|
|
|
++cp;
|
|
|
|
}
|
|
|
|
return aSourceLength;
|
|
|
|
}
|
|
|
|
};
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
void
|
2014-05-27 00:15:35 -07:00
|
|
|
ToUpperCase(nsCSubstring& aCString)
|
2014-05-05 10:30:46 -07:00
|
|
|
{
|
|
|
|
ConvertToUpperCase converter;
|
|
|
|
char* start;
|
|
|
|
converter.write(aCString.BeginWriting(start), aCString.Length());
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:46 -07:00
|
|
|
/**
|
|
|
|
* A character sink for copying with case conversion.
|
|
|
|
*/
|
2007-03-22 10:30:00 -07:00
|
|
|
class CopyToUpperCase
|
2014-05-05 10:30:46 -07:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
typedef char value_type;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-27 00:15:35 -07:00
|
|
|
explicit CopyToUpperCase(nsACString::iterator& aDestIter)
|
2014-05-05 10:30:46 -07:00
|
|
|
: mIter(aDestIter)
|
|
|
|
{
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:46 -07:00
|
|
|
uint32_t
|
2014-05-27 00:15:35 -07:00
|
|
|
write(const char* aSource, uint32_t aSourceLength)
|
2014-05-05 10:30:46 -07:00
|
|
|
{
|
|
|
|
uint32_t len = XPCOM_MIN(uint32_t(mIter.size_forward()), aSourceLength);
|
|
|
|
char* cp = mIter.get();
|
|
|
|
const char* end = aSource + len;
|
|
|
|
while (aSource != end) {
|
|
|
|
char ch = *aSource;
|
2014-05-27 00:15:35 -07:00
|
|
|
if ((ch >= 'a') && (ch <= 'z')) {
|
2014-05-05 10:30:46 -07:00
|
|
|
*cp = ch - ('a' - 'A');
|
2014-05-27 00:15:35 -07:00
|
|
|
} else {
|
2014-05-05 10:30:46 -07:00
|
|
|
*cp = ch;
|
2014-05-27 00:15:35 -07:00
|
|
|
}
|
2014-05-05 10:30:46 -07:00
|
|
|
++aSource;
|
|
|
|
++cp;
|
|
|
|
}
|
|
|
|
mIter.advance(len);
|
|
|
|
return len;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:46 -07:00
|
|
|
protected:
|
|
|
|
nsACString::iterator& mIter;
|
|
|
|
};
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
void
|
2014-05-27 00:15:35 -07:00
|
|
|
ToUpperCase(const nsACString& aSource, nsACString& aDest)
|
2014-05-05 10:30:46 -07:00
|
|
|
{
|
|
|
|
nsACString::const_iterator fromBegin, fromEnd;
|
|
|
|
nsACString::iterator toBegin;
|
|
|
|
aDest.SetLength(aSource.Length());
|
2007-07-02 23:03:11 -07:00
|
|
|
|
2014-05-05 10:30:46 -07:00
|
|
|
CopyToUpperCase converter(aDest.BeginWriting(toBegin));
|
2014-08-25 12:17:15 -07:00
|
|
|
copy_string(aSource.BeginReading(fromBegin), aSource.EndReading(fromEnd),
|
|
|
|
converter);
|
2014-05-05 10:30:46 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:46 -07:00
|
|
|
/**
|
|
|
|
* A character sink for case conversion.
|
|
|
|
*/
|
2007-03-22 10:30:00 -07:00
|
|
|
class ConvertToLowerCase
|
2014-05-05 10:30:46 -07:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
typedef char value_type;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:46 -07:00
|
|
|
uint32_t
|
2014-05-27 00:15:35 -07:00
|
|
|
write(const char* aSource, uint32_t aSourceLength)
|
2014-05-05 10:30:46 -07:00
|
|
|
{
|
|
|
|
char* cp = const_cast<char*>(aSource);
|
|
|
|
const char* end = aSource + aSourceLength;
|
|
|
|
while (cp != end) {
|
|
|
|
char ch = *cp;
|
2014-05-27 00:15:35 -07:00
|
|
|
if ((ch >= 'A') && (ch <= 'Z')) {
|
2014-05-05 10:30:46 -07:00
|
|
|
*cp = ch + ('a' - 'A');
|
2014-05-27 00:15:35 -07:00
|
|
|
}
|
2014-05-05 10:30:46 -07:00
|
|
|
++cp;
|
|
|
|
}
|
|
|
|
return aSourceLength;
|
|
|
|
}
|
|
|
|
};
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
void
|
2014-05-27 00:15:35 -07:00
|
|
|
ToLowerCase(nsCSubstring& aCString)
|
2014-05-05 10:30:46 -07:00
|
|
|
{
|
|
|
|
ConvertToLowerCase converter;
|
|
|
|
char* start;
|
|
|
|
converter.write(aCString.BeginWriting(start), aCString.Length());
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:46 -07:00
|
|
|
/**
|
|
|
|
* A character sink for copying with case conversion.
|
|
|
|
*/
|
2007-03-22 10:30:00 -07:00
|
|
|
class CopyToLowerCase
|
2014-05-05 10:30:46 -07:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
typedef char value_type;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-27 00:15:35 -07:00
|
|
|
explicit CopyToLowerCase(nsACString::iterator& aDestIter)
|
2014-05-05 10:30:46 -07:00
|
|
|
: mIter(aDestIter)
|
|
|
|
{
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:46 -07:00
|
|
|
uint32_t
|
2014-05-27 00:15:35 -07:00
|
|
|
write(const char* aSource, uint32_t aSourceLength)
|
2014-05-05 10:30:46 -07:00
|
|
|
{
|
|
|
|
uint32_t len = XPCOM_MIN(uint32_t(mIter.size_forward()), aSourceLength);
|
|
|
|
char* cp = mIter.get();
|
|
|
|
const char* end = aSource + len;
|
|
|
|
while (aSource != end) {
|
|
|
|
char ch = *aSource;
|
2014-05-27 00:15:35 -07:00
|
|
|
if ((ch >= 'A') && (ch <= 'Z')) {
|
2014-05-05 10:30:46 -07:00
|
|
|
*cp = ch + ('a' - 'A');
|
2014-05-27 00:15:35 -07:00
|
|
|
} else {
|
2014-05-05 10:30:46 -07:00
|
|
|
*cp = ch;
|
2014-05-27 00:15:35 -07:00
|
|
|
}
|
2014-05-05 10:30:46 -07:00
|
|
|
++aSource;
|
|
|
|
++cp;
|
|
|
|
}
|
|
|
|
mIter.advance(len);
|
|
|
|
return len;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:46 -07:00
|
|
|
protected:
|
|
|
|
nsACString::iterator& mIter;
|
|
|
|
};
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
void
|
2014-05-27 00:15:35 -07:00
|
|
|
ToLowerCase(const nsACString& aSource, nsACString& aDest)
|
2014-05-05 10:30:46 -07:00
|
|
|
{
|
|
|
|
nsACString::const_iterator fromBegin, fromEnd;
|
|
|
|
nsACString::iterator toBegin;
|
|
|
|
aDest.SetLength(aSource.Length());
|
2007-07-02 23:03:11 -07:00
|
|
|
|
2014-05-05 10:30:46 -07:00
|
|
|
CopyToLowerCase converter(aDest.BeginWriting(toBegin));
|
2014-08-25 12:17:15 -07:00
|
|
|
copy_string(aSource.BeginReading(fromBegin), aSource.EndReading(fromEnd),
|
|
|
|
converter);
|
2014-05-05 10:30:46 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2014-05-05 10:30:46 -07:00
|
|
|
ParseString(const nsACString& aSource, char aDelimiter,
|
2009-01-21 20:15:34 -08:00
|
|
|
nsTArray<nsCString>& aArray)
|
2014-05-05 10:30:46 -07:00
|
|
|
{
|
|
|
|
nsACString::const_iterator start, end;
|
|
|
|
aSource.BeginReading(start);
|
|
|
|
aSource.EndReading(end);
|
2009-01-21 20:15:34 -08:00
|
|
|
|
2014-05-05 10:30:46 -07:00
|
|
|
uint32_t oldLength = aArray.Length();
|
2009-01-21 20:15:34 -08:00
|
|
|
|
2014-05-27 00:15:35 -07:00
|
|
|
for (;;) {
|
2014-05-05 10:30:46 -07:00
|
|
|
nsACString::const_iterator delimiter = start;
|
|
|
|
FindCharInReadable(aDelimiter, delimiter, end);
|
|
|
|
|
2014-05-27 00:15:35 -07:00
|
|
|
if (delimiter != start) {
|
|
|
|
if (!aArray.AppendElement(Substring(start, delimiter))) {
|
2014-05-05 10:30:46 -07:00
|
|
|
aArray.RemoveElementsAt(oldLength, aArray.Length() - oldLength);
|
|
|
|
return false;
|
2009-01-21 20:15:34 -08:00
|
|
|
}
|
2014-05-05 10:30:46 -07:00
|
|
|
}
|
2009-01-21 20:15:34 -08:00
|
|
|
|
2014-05-27 00:15:35 -07:00
|
|
|
if (delimiter == end) {
|
2014-05-05 10:30:46 -07:00
|
|
|
break;
|
2014-05-27 00:15:35 -07:00
|
|
|
}
|
2014-05-05 10:30:46 -07:00
|
|
|
start = ++delimiter;
|
2014-05-27 00:15:35 -07:00
|
|
|
if (start == end) {
|
2014-05-05 10:30:46 -07:00
|
|
|
break;
|
2014-05-27 00:15:35 -07:00
|
|
|
}
|
2009-01-21 20:15:34 -08:00
|
|
|
}
|
|
|
|
|
2014-05-05 10:30:46 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
template <class StringT, class IteratorT, class Comparator>
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2014-05-27 00:15:35 -07:00
|
|
|
FindInReadable_Impl(const StringT& aPattern, IteratorT& aSearchStart,
|
|
|
|
IteratorT& aSearchEnd, const Comparator& aCompare)
|
2014-05-05 10:30:46 -07:00
|
|
|
{
|
|
|
|
bool found_it = false;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:46 -07:00
|
|
|
// only bother searching at all if we're given a non-empty range to search
|
2014-05-27 00:15:35 -07:00
|
|
|
if (aSearchStart != aSearchEnd) {
|
2014-05-05 10:30:46 -07:00
|
|
|
IteratorT aPatternStart, aPatternEnd;
|
|
|
|
aPattern.BeginReading(aPatternStart);
|
|
|
|
aPattern.EndReading(aPatternEnd);
|
|
|
|
|
|
|
|
// outer loop keeps searching till we find it or run out of string to search
|
2014-05-27 00:15:35 -07:00
|
|
|
while (!found_it) {
|
2014-05-05 10:30:46 -07:00
|
|
|
// fast inner loop (that's what it's called, not what it is) looks for a potential match
|
2014-05-27 00:15:35 -07:00
|
|
|
while (aSearchStart != aSearchEnd &&
|
|
|
|
aCompare(aPatternStart.get(), aSearchStart.get(), 1, 1)) {
|
2014-05-05 10:30:46 -07:00
|
|
|
++aSearchStart;
|
2014-05-27 00:15:35 -07:00
|
|
|
}
|
2014-05-05 10:30:46 -07:00
|
|
|
|
|
|
|
// if we broke out of the `fast' loop because we're out of string ... we're done: no match
|
2014-05-27 00:15:35 -07:00
|
|
|
if (aSearchStart == aSearchEnd) {
|
2014-05-05 10:30:46 -07:00
|
|
|
break;
|
2014-05-27 00:15:35 -07:00
|
|
|
}
|
2014-05-05 10:30:46 -07:00
|
|
|
|
|
|
|
// otherwise, we're at a potential match, let's see if we really hit one
|
|
|
|
IteratorT testPattern(aPatternStart);
|
|
|
|
IteratorT testSearch(aSearchStart);
|
|
|
|
|
|
|
|
// slow inner loop verifies the potential match (found by the `fast' loop) at the current position
|
2014-05-27 00:15:35 -07:00
|
|
|
for (;;) {
|
2014-05-05 10:30:46 -07:00
|
|
|
// we already compared the first character in the outer loop,
|
|
|
|
// so we'll advance before the next comparison
|
|
|
|
++testPattern;
|
|
|
|
++testSearch;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:46 -07:00
|
|
|
// if we verified all the way to the end of the pattern, then we found it!
|
2014-05-27 00:15:35 -07:00
|
|
|
if (testPattern == aPatternEnd) {
|
2014-05-05 10:30:46 -07:00
|
|
|
found_it = true;
|
|
|
|
aSearchEnd = testSearch; // return the exact found range through the parameters
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// if we got to end of the string we're searching before we hit the end of the
|
|
|
|
// pattern, we'll never find what we're looking for
|
2014-05-27 00:15:35 -07:00
|
|
|
if (testSearch == aSearchEnd) {
|
2014-05-05 10:30:46 -07:00
|
|
|
aSearchStart = aSearchEnd;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// else if we mismatched ... it's time to advance to the next search position
|
|
|
|
// and get back into the `fast' loop
|
2014-05-27 00:15:35 -07:00
|
|
|
if (aCompare(testPattern.get(), testSearch.get(), 1, 1)) {
|
2014-05-05 10:30:46 -07:00
|
|
|
++aSearchStart;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2014-05-05 10:30:46 -07:00
|
|
|
return found_it;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This searches the entire string from right to left, and returns the first match found, if any.
|
|
|
|
*/
|
2007-07-23 18:30:19 -07:00
|
|
|
template <class StringT, class IteratorT, class Comparator>
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2014-05-27 00:15:35 -07:00
|
|
|
RFindInReadable_Impl(const StringT& aPattern, IteratorT& aSearchStart,
|
|
|
|
IteratorT& aSearchEnd, const Comparator& aCompare)
|
2014-05-05 10:30:46 -07:00
|
|
|
{
|
|
|
|
IteratorT patternStart, patternEnd, searchEnd = aSearchEnd;
|
|
|
|
aPattern.BeginReading(patternStart);
|
|
|
|
aPattern.EndReading(patternEnd);
|
|
|
|
|
|
|
|
// Point to the last character in the pattern
|
|
|
|
--patternEnd;
|
|
|
|
// outer loop keeps searching till we run out of string to search
|
2014-05-27 00:15:35 -07:00
|
|
|
while (aSearchStart != searchEnd) {
|
2014-05-05 10:30:46 -07:00
|
|
|
// Point to the end position of the next possible match
|
|
|
|
--searchEnd;
|
|
|
|
|
|
|
|
// Check last character, if a match, explore further from here
|
2014-05-27 00:15:35 -07:00
|
|
|
if (aCompare(patternEnd.get(), searchEnd.get(), 1, 1) == 0) {
|
2014-05-05 10:30:46 -07:00
|
|
|
// We're at a potential match, let's see if we really hit one
|
|
|
|
IteratorT testPattern(patternEnd);
|
|
|
|
IteratorT testSearch(searchEnd);
|
|
|
|
|
|
|
|
// inner loop verifies the potential match at the current position
|
2014-05-27 00:15:35 -07:00
|
|
|
do {
|
2014-05-05 10:30:46 -07:00
|
|
|
// if we verified all the way to the end of the pattern, then we found it!
|
2014-05-27 00:15:35 -07:00
|
|
|
if (testPattern == patternStart) {
|
2014-05-05 10:30:46 -07:00
|
|
|
aSearchStart = testSearch; // point to start of match
|
|
|
|
aSearchEnd = ++searchEnd; // point to end of match
|
|
|
|
return true;
|
|
|
|
}
|
2007-07-23 18:30:19 -07:00
|
|
|
|
2014-05-05 10:30:46 -07:00
|
|
|
// if we got to end of the string we're searching before we hit the end of the
|
|
|
|
// pattern, we'll never find what we're looking for
|
2014-05-27 00:15:35 -07:00
|
|
|
if (testSearch == aSearchStart) {
|
2014-05-05 10:30:46 -07:00
|
|
|
aSearchStart = aSearchEnd;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// test previous character for a match
|
|
|
|
--testPattern;
|
|
|
|
--testSearch;
|
2014-05-27 00:15:35 -07:00
|
|
|
} while (aCompare(testPattern.get(), testSearch.get(), 1, 1) == 0);
|
2014-05-05 10:30:46 -07:00
|
|
|
}
|
2007-07-23 18:30:19 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:46 -07:00
|
|
|
aSearchStart = aSearchEnd;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2014-05-27 00:15:35 -07:00
|
|
|
FindInReadable(const nsAString& aPattern,
|
|
|
|
nsAString::const_iterator& aSearchStart,
|
|
|
|
nsAString::const_iterator& aSearchEnd,
|
|
|
|
const nsStringComparator& aComparator)
|
2014-05-05 10:30:46 -07:00
|
|
|
{
|
|
|
|
return FindInReadable_Impl(aPattern, aSearchStart, aSearchEnd, aComparator);
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2014-05-27 00:15:35 -07:00
|
|
|
FindInReadable(const nsACString& aPattern,
|
|
|
|
nsACString::const_iterator& aSearchStart,
|
|
|
|
nsACString::const_iterator& aSearchEnd,
|
|
|
|
const nsCStringComparator& aComparator)
|
2014-05-05 10:30:46 -07:00
|
|
|
{
|
|
|
|
return FindInReadable_Impl(aPattern, aSearchStart, aSearchEnd, aComparator);
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2014-05-27 00:15:35 -07:00
|
|
|
CaseInsensitiveFindInReadable(const nsACString& aPattern,
|
|
|
|
nsACString::const_iterator& aSearchStart,
|
|
|
|
nsACString::const_iterator& aSearchEnd)
|
2014-05-05 10:30:46 -07:00
|
|
|
{
|
2014-05-27 00:15:35 -07:00
|
|
|
return FindInReadable_Impl(aPattern, aSearchStart, aSearchEnd,
|
|
|
|
nsCaseInsensitiveCStringComparator());
|
2014-05-05 10:30:46 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2014-05-27 00:15:35 -07:00
|
|
|
RFindInReadable(const nsAString& aPattern,
|
|
|
|
nsAString::const_iterator& aSearchStart,
|
|
|
|
nsAString::const_iterator& aSearchEnd,
|
|
|
|
const nsStringComparator& aComparator)
|
2014-05-05 10:30:46 -07:00
|
|
|
{
|
|
|
|
return RFindInReadable_Impl(aPattern, aSearchStart, aSearchEnd, aComparator);
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2014-05-27 00:15:35 -07:00
|
|
|
RFindInReadable(const nsACString& aPattern,
|
|
|
|
nsACString::const_iterator& aSearchStart,
|
|
|
|
nsACString::const_iterator& aSearchEnd,
|
|
|
|
const nsCStringComparator& aComparator)
|
2014-05-05 10:30:46 -07:00
|
|
|
{
|
|
|
|
return RFindInReadable_Impl(aPattern, aSearchStart, aSearchEnd, aComparator);
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2014-05-27 00:15:35 -07:00
|
|
|
FindCharInReadable(char16_t aChar, nsAString::const_iterator& aSearchStart,
|
|
|
|
const nsAString::const_iterator& aSearchEnd)
|
2014-05-05 10:30:46 -07:00
|
|
|
{
|
|
|
|
int32_t fragmentLength = aSearchEnd.get() - aSearchStart.get();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-27 00:15:35 -07:00
|
|
|
const char16_t* charFoundAt =
|
|
|
|
nsCharTraits<char16_t>::find(aSearchStart.get(), fragmentLength, aChar);
|
|
|
|
if (charFoundAt) {
|
|
|
|
aSearchStart.advance(charFoundAt - aSearchStart.get());
|
2014-05-05 10:30:46 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2014-05-05 10:30:46 -07:00
|
|
|
aSearchStart.advance(fragmentLength);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2014-05-27 00:15:35 -07:00
|
|
|
FindCharInReadable(char aChar, nsACString::const_iterator& aSearchStart,
|
|
|
|
const nsACString::const_iterator& aSearchEnd)
|
2014-05-05 10:30:46 -07:00
|
|
|
{
|
|
|
|
int32_t fragmentLength = aSearchEnd.get() - aSearchStart.get();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-27 00:15:35 -07:00
|
|
|
const char* charFoundAt =
|
|
|
|
nsCharTraits<char>::find(aSearchStart.get(), fragmentLength, aChar);
|
|
|
|
if (charFoundAt) {
|
|
|
|
aSearchStart.advance(charFoundAt - aSearchStart.get());
|
2014-05-05 10:30:46 -07:00
|
|
|
return true;
|
2011-08-18 06:46:39 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:46 -07:00
|
|
|
aSearchStart.advance(fragmentLength);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t
|
2014-05-27 00:15:35 -07:00
|
|
|
CountCharInReadable(const nsAString& aStr, char16_t aChar)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t count = 0;
|
2007-03-22 10:30:00 -07:00
|
|
|
nsAString::const_iterator begin, end;
|
2014-05-05 10:30:46 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
aStr.BeginReading(begin);
|
|
|
|
aStr.EndReading(end);
|
2014-05-05 10:30:46 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
while (begin != end) {
|
|
|
|
if (*begin == aChar) {
|
|
|
|
++count;
|
|
|
|
}
|
|
|
|
++begin;
|
|
|
|
}
|
|
|
|
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t
|
2014-05-27 00:15:35 -07:00
|
|
|
CountCharInReadable(const nsACString& aStr, char aChar)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t count = 0;
|
2007-03-22 10:30:00 -07:00
|
|
|
nsACString::const_iterator begin, end;
|
2014-05-05 10:30:46 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
aStr.BeginReading(begin);
|
|
|
|
aStr.EndReading(end);
|
2014-05-05 10:30:46 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
while (begin != end) {
|
|
|
|
if (*begin == aChar) {
|
|
|
|
++count;
|
|
|
|
}
|
|
|
|
++begin;
|
|
|
|
}
|
|
|
|
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2014-05-27 00:15:35 -07:00
|
|
|
StringBeginsWith(const nsAString& aSource, const nsAString& aSubstring,
|
|
|
|
const nsStringComparator& aComparator)
|
2014-05-05 10:30:46 -07:00
|
|
|
{
|
|
|
|
nsAString::size_type src_len = aSource.Length(),
|
|
|
|
sub_len = aSubstring.Length();
|
2014-05-27 00:15:35 -07:00
|
|
|
if (sub_len > src_len) {
|
2014-05-05 10:30:46 -07:00
|
|
|
return false;
|
2014-05-27 00:15:35 -07:00
|
|
|
}
|
2014-05-05 10:30:46 -07:00
|
|
|
return Substring(aSource, 0, sub_len).Equals(aSubstring, aComparator);
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2014-05-27 00:15:35 -07:00
|
|
|
StringBeginsWith(const nsACString& aSource, const nsACString& aSubstring,
|
|
|
|
const nsCStringComparator& aComparator)
|
2014-05-05 10:30:46 -07:00
|
|
|
{
|
|
|
|
nsACString::size_type src_len = aSource.Length(),
|
|
|
|
sub_len = aSubstring.Length();
|
2014-05-27 00:15:35 -07:00
|
|
|
if (sub_len > src_len) {
|
2014-05-05 10:30:46 -07:00
|
|
|
return false;
|
2014-05-27 00:15:35 -07:00
|
|
|
}
|
2014-05-05 10:30:46 -07:00
|
|
|
return Substring(aSource, 0, sub_len).Equals(aSubstring, aComparator);
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2014-05-27 00:15:35 -07:00
|
|
|
StringEndsWith(const nsAString& aSource, const nsAString& aSubstring,
|
|
|
|
const nsStringComparator& aComparator)
|
2014-05-05 10:30:46 -07:00
|
|
|
{
|
|
|
|
nsAString::size_type src_len = aSource.Length(),
|
|
|
|
sub_len = aSubstring.Length();
|
2014-05-27 00:15:35 -07:00
|
|
|
if (sub_len > src_len) {
|
2014-05-05 10:30:46 -07:00
|
|
|
return false;
|
2014-05-27 00:15:35 -07:00
|
|
|
}
|
2014-05-05 10:30:46 -07:00
|
|
|
return Substring(aSource, src_len - sub_len, sub_len).Equals(aSubstring,
|
|
|
|
aComparator);
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2014-05-27 00:15:35 -07:00
|
|
|
StringEndsWith(const nsACString& aSource, const nsACString& aSubstring,
|
|
|
|
const nsCStringComparator& aComparator)
|
2014-05-05 10:30:46 -07:00
|
|
|
{
|
|
|
|
nsACString::size_type src_len = aSource.Length(),
|
|
|
|
sub_len = aSubstring.Length();
|
2014-05-27 00:15:35 -07:00
|
|
|
if (sub_len > src_len) {
|
2014-05-05 10:30:46 -07:00
|
|
|
return false;
|
2014-05-27 00:15:35 -07:00
|
|
|
}
|
2014-05-05 10:30:46 -07:00
|
|
|
return Substring(aSource, src_len - sub_len, sub_len).Equals(aSubstring,
|
|
|
|
aComparator);
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
|
|
|
|
|
2014-01-04 07:02:17 -08:00
|
|
|
static const char16_t empty_buffer[1] = { '\0' };
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
const nsAFlatString&
|
|
|
|
EmptyString()
|
2014-05-05 10:30:46 -07:00
|
|
|
{
|
|
|
|
static const nsDependentString sEmpty(empty_buffer);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:46 -07:00
|
|
|
return sEmpty;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
const nsAFlatCString&
|
|
|
|
EmptyCString()
|
2014-05-05 10:30:46 -07:00
|
|
|
{
|
2014-05-27 00:15:35 -07:00
|
|
|
static const nsDependentCString sEmpty((const char*)empty_buffer);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:46 -07:00
|
|
|
return sEmpty;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-12-20 11:46:00 -08:00
|
|
|
const nsAFlatString&
|
|
|
|
NullString()
|
2014-05-05 10:30:46 -07:00
|
|
|
{
|
|
|
|
static const nsXPIDLString sNull;
|
2011-12-20 11:46:00 -08:00
|
|
|
|
2014-05-05 10:30:46 -07:00
|
|
|
return sNull;
|
|
|
|
}
|
2011-12-20 11:46:00 -08:00
|
|
|
|
|
|
|
const nsAFlatCString&
|
|
|
|
NullCString()
|
2014-05-05 10:30:46 -07:00
|
|
|
{
|
|
|
|
static const nsXPIDLCString sNull;
|
2011-12-20 11:46:00 -08:00
|
|
|
|
2014-05-05 10:30:46 -07:00
|
|
|
return sNull;
|
|
|
|
}
|
2011-12-20 11:46:00 -08:00
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t
|
2007-03-22 10:30:00 -07:00
|
|
|
CompareUTF8toUTF16(const nsASingleFragmentCString& aUTF8String,
|
|
|
|
const nsASingleFragmentString& aUTF16String)
|
2014-05-05 10:30:46 -07:00
|
|
|
{
|
|
|
|
static const uint32_t NOT_ASCII = uint32_t(~0x7F);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-27 00:15:35 -07:00
|
|
|
const char* u8;
|
|
|
|
const char* u8end;
|
2014-05-05 10:30:46 -07:00
|
|
|
aUTF8String.BeginReading(u8);
|
|
|
|
aUTF8String.EndReading(u8end);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-27 00:15:35 -07:00
|
|
|
const char16_t* u16;
|
|
|
|
const char16_t* u16end;
|
2014-05-05 10:30:46 -07:00
|
|
|
aUTF16String.BeginReading(u16);
|
|
|
|
aUTF16String.EndReading(u16end);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-27 00:15:35 -07:00
|
|
|
while (u8 != u8end && u16 != u16end) {
|
2014-05-05 10:30:46 -07:00
|
|
|
// Cast away the signedness of *u8 to prevent signextension when
|
|
|
|
// converting to uint32_t
|
|
|
|
uint32_t c8_32 = (uint8_t)*u8;
|
|
|
|
|
2014-05-27 00:15:35 -07:00
|
|
|
if (c8_32 & NOT_ASCII) {
|
2014-05-05 10:30:46 -07:00
|
|
|
bool err;
|
|
|
|
c8_32 = UTF8CharEnumerator::NextChar(&u8, u8end, &err);
|
2014-05-27 00:15:35 -07:00
|
|
|
if (err) {
|
2014-05-05 10:30:46 -07:00
|
|
|
return INT32_MIN;
|
2014-05-27 00:15:35 -07:00
|
|
|
}
|
2014-05-05 10:30:46 -07:00
|
|
|
|
|
|
|
uint32_t c16_32 = UTF16CharEnumerator::NextChar(&u16, u16end);
|
|
|
|
// The above UTF16CharEnumerator::NextChar() calls can
|
|
|
|
// fail, but if it does for anything other than no data to
|
|
|
|
// look at (which can't happen here), it returns the
|
|
|
|
// Unicode replacement character 0xFFFD for the invalid
|
|
|
|
// data they were fed. Ignore that error and treat invalid
|
|
|
|
// UTF16 as 0xFFFD.
|
|
|
|
//
|
|
|
|
// This matches what our UTF16 to UTF8 conversion code
|
|
|
|
// does, and thus a UTF8 string that came from an invalid
|
|
|
|
// UTF16 string will compare equal to the invalid UTF16
|
|
|
|
// string it came from. Same is true for any other UTF16
|
|
|
|
// string differs only in the invalid part of the string.
|
|
|
|
|
2014-05-27 00:15:35 -07:00
|
|
|
if (c8_32 != c16_32) {
|
2014-05-05 10:30:46 -07:00
|
|
|
return c8_32 < c16_32 ? -1 : 1;
|
2014-05-27 00:15:35 -07:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (c8_32 != *u16) {
|
2014-05-05 10:30:46 -07:00
|
|
|
return c8_32 > *u16 ? 1 : -1;
|
2014-05-27 00:15:35 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:46 -07:00
|
|
|
++u8;
|
|
|
|
++u16;
|
|
|
|
}
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-27 00:15:35 -07:00
|
|
|
if (u8 != u8end) {
|
2014-05-05 10:30:46 -07:00
|
|
|
// We get to the end of the UTF16 string, but no to the end of
|
|
|
|
// the UTF8 string. The UTF8 string is longer than the UTF16
|
|
|
|
// string
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:46 -07:00
|
|
|
return 1;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-27 00:15:35 -07:00
|
|
|
if (u16 != u16end) {
|
2014-05-05 10:30:46 -07:00
|
|
|
// We get to the end of the UTF8 string, but no to the end of
|
|
|
|
// the UTF16 string. The UTF16 string is longer than the UTF8
|
|
|
|
// string
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:46 -07:00
|
|
|
return -1;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2014-05-05 10:30:46 -07:00
|
|
|
// The two strings match.
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
void
|
2012-08-22 08:56:38 -07:00
|
|
|
AppendUCS4ToUTF16(const uint32_t aSource, nsAString& aDest)
|
2014-05-05 10:30:46 -07:00
|
|
|
{
|
|
|
|
NS_ASSERTION(IS_VALID_CHAR(aSource), "Invalid UCS4 char");
|
2014-05-27 00:15:35 -07:00
|
|
|
if (IS_IN_BMP(aSource)) {
|
2014-05-05 10:30:46 -07:00
|
|
|
aDest.Append(char16_t(aSource));
|
2014-05-27 00:15:35 -07:00
|
|
|
} else {
|
2014-05-05 10:30:46 -07:00
|
|
|
aDest.Append(H_SURROGATE(aSource));
|
|
|
|
aDest.Append(L_SURROGATE(aSource));
|
|
|
|
}
|
|
|
|
}
|