2007-03-22 10:30:00 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
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 "nsCollation.h"
|
|
|
|
#include "nsCollationCID.h"
|
2010-07-29 12:22:16 -07:00
|
|
|
#include "nsUnicharUtils.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "prmem.h"
|
2013-09-27 09:45:04 -07:00
|
|
|
#include "nsIUnicodeEncoder.h"
|
|
|
|
#include "nsServiceManagerUtils.h"
|
2014-05-08 02:32:00 -07:00
|
|
|
#include "mozilla/dom/EncodingUtils.h"
|
|
|
|
|
|
|
|
using mozilla::dom::EncodingUtils;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
NS_DEFINE_CID(kCollationCID, NS_COLLATION_CID);
|
|
|
|
|
2014-04-27 00:06:00 -07:00
|
|
|
NS_IMPL_ISUPPORTS(nsCollationFactory, nsICollationFactory)
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
nsresult nsCollationFactory::CreateCollation(nsILocale* locale, nsICollation** instancePtr)
|
|
|
|
{
|
|
|
|
// Create a collation interface instance.
|
|
|
|
//
|
|
|
|
nsICollation *inst;
|
|
|
|
nsresult res;
|
|
|
|
|
|
|
|
res = CallCreateInstance(kCollationCID, &inst);
|
|
|
|
if (NS_FAILED(res)) {
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
inst->Initialize(locale);
|
|
|
|
*instancePtr = inst;
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
nsCollation::nsCollation()
|
|
|
|
{
|
|
|
|
MOZ_COUNT_CTOR(nsCollation);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCollation::~nsCollation()
|
|
|
|
{
|
|
|
|
MOZ_COUNT_DTOR(nsCollation);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult nsCollation::NormalizeString(const nsAString& stringIn, nsAString& stringOut)
|
|
|
|
{
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t aLength = stringIn.Length();
|
2010-07-29 12:22:16 -07:00
|
|
|
|
|
|
|
if (aLength <= 64) {
|
2014-01-04 07:02:17 -08:00
|
|
|
char16_t conversionBuffer[64];
|
2010-07-29 12:22:16 -07:00
|
|
|
ToLowerCase(PromiseFlatString(stringIn).get(), conversionBuffer, aLength);
|
|
|
|
stringOut.Assign(conversionBuffer, aLength);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
else {
|
2014-01-04 07:02:17 -08:00
|
|
|
char16_t* conversionBuffer;
|
|
|
|
conversionBuffer = new char16_t[aLength];
|
2010-07-29 12:22:16 -07:00
|
|
|
if (!conversionBuffer) {
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2010-07-29 12:22:16 -07:00
|
|
|
ToLowerCase(PromiseFlatString(stringIn).get(), conversionBuffer, aLength);
|
|
|
|
stringOut.Assign(conversionBuffer, aLength);
|
|
|
|
delete [] conversionBuffer;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult nsCollation::SetCharset(const char* aCharset)
|
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(aCharset);
|
|
|
|
|
2014-05-08 02:32:00 -07:00
|
|
|
nsDependentCString label(aCharset);
|
|
|
|
nsAutoCString encoding;
|
|
|
|
if (!EncodingUtils::FindEncodingForLabelNoReplacement(label, encoding)) {
|
|
|
|
return NS_ERROR_UCONV_NOCONV;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2014-05-08 02:32:00 -07:00
|
|
|
mEncoder = EncodingUtils::EncoderForEncoding(encoding);
|
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult nsCollation::UnicodeToChar(const nsAString& aSrc, char** dst)
|
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(dst);
|
|
|
|
|
|
|
|
nsresult res = NS_OK;
|
|
|
|
if (!mEncoder)
|
|
|
|
res = SetCharset("ISO-8859-1");
|
|
|
|
|
|
|
|
if (NS_SUCCEEDED(res)) {
|
|
|
|
const nsPromiseFlatString& src = PromiseFlatString(aSrc);
|
2014-01-04 07:02:17 -08:00
|
|
|
const char16_t *unichars = src.get();
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t unicharLength = src.Length();
|
|
|
|
int32_t dstLength;
|
2007-03-22 10:30:00 -07:00
|
|
|
res = mEncoder->GetMaxLength(unichars, unicharLength, &dstLength);
|
|
|
|
if (NS_SUCCEEDED(res)) {
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t bufLength = dstLength + 1 + 32; // extra 32 bytes for Finish() call
|
2007-03-22 10:30:00 -07:00
|
|
|
*dst = (char *) PR_Malloc(bufLength);
|
|
|
|
if (*dst) {
|
|
|
|
**dst = '\0';
|
|
|
|
res = mEncoder->Convert(unichars, &unicharLength, *dst, &dstLength);
|
|
|
|
|
|
|
|
if (NS_SUCCEEDED(res) || (NS_ERROR_UENC_NOMAPPING == res)) {
|
|
|
|
// Finishes the conversion. The converter has the possibility to write some
|
|
|
|
// extra data and flush its final state.
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t finishLength = bufLength - dstLength; // remaining unused buffer length
|
2007-03-22 10:30:00 -07:00
|
|
|
if (finishLength > 0) {
|
|
|
|
res = mEncoder->Finish((*dst + dstLength), &finishLength);
|
|
|
|
if (NS_SUCCEEDED(res)) {
|
|
|
|
(*dst)[dstLength + finishLength] = '\0';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (NS_FAILED(res)) {
|
|
|
|
PR_Free(*dst);
|
2012-07-30 07:20:58 -07:00
|
|
|
*dst = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
res = NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|