Wed, 31 Dec 2014 07:22:50 +0100
Correct previous dual key logic pending first delivery installment.
michael@0 | 1 | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
michael@0 | 2 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | #include "nsCollation.h" |
michael@0 | 7 | #include "nsCollationCID.h" |
michael@0 | 8 | #include "nsUnicharUtils.h" |
michael@0 | 9 | #include "prmem.h" |
michael@0 | 10 | #include "nsIUnicodeEncoder.h" |
michael@0 | 11 | #include "nsICharsetConverterManager.h" |
michael@0 | 12 | #include "nsServiceManagerUtils.h" |
michael@0 | 13 | |
michael@0 | 14 | //////////////////////////////////////////////////////////////////////////////// |
michael@0 | 15 | |
michael@0 | 16 | NS_DEFINE_CID(kCollationCID, NS_COLLATION_CID); |
michael@0 | 17 | |
michael@0 | 18 | NS_IMPL_ISUPPORTS(nsCollationFactory, nsICollationFactory) |
michael@0 | 19 | |
michael@0 | 20 | nsresult nsCollationFactory::CreateCollation(nsILocale* locale, nsICollation** instancePtr) |
michael@0 | 21 | { |
michael@0 | 22 | // Create a collation interface instance. |
michael@0 | 23 | // |
michael@0 | 24 | nsICollation *inst; |
michael@0 | 25 | nsresult res; |
michael@0 | 26 | |
michael@0 | 27 | res = CallCreateInstance(kCollationCID, &inst); |
michael@0 | 28 | if (NS_FAILED(res)) { |
michael@0 | 29 | return res; |
michael@0 | 30 | } |
michael@0 | 31 | |
michael@0 | 32 | inst->Initialize(locale); |
michael@0 | 33 | *instancePtr = inst; |
michael@0 | 34 | |
michael@0 | 35 | return res; |
michael@0 | 36 | } |
michael@0 | 37 | |
michael@0 | 38 | //////////////////////////////////////////////////////////////////////////////// |
michael@0 | 39 | |
michael@0 | 40 | nsCollation::nsCollation() |
michael@0 | 41 | { |
michael@0 | 42 | MOZ_COUNT_CTOR(nsCollation); |
michael@0 | 43 | } |
michael@0 | 44 | |
michael@0 | 45 | nsCollation::~nsCollation() |
michael@0 | 46 | { |
michael@0 | 47 | MOZ_COUNT_DTOR(nsCollation); |
michael@0 | 48 | } |
michael@0 | 49 | |
michael@0 | 50 | nsresult nsCollation::NormalizeString(const nsAString& stringIn, nsAString& stringOut) |
michael@0 | 51 | { |
michael@0 | 52 | int32_t aLength = stringIn.Length(); |
michael@0 | 53 | |
michael@0 | 54 | if (aLength <= 64) { |
michael@0 | 55 | char16_t conversionBuffer[64]; |
michael@0 | 56 | ToLowerCase(PromiseFlatString(stringIn).get(), conversionBuffer, aLength); |
michael@0 | 57 | stringOut.Assign(conversionBuffer, aLength); |
michael@0 | 58 | } |
michael@0 | 59 | else { |
michael@0 | 60 | char16_t* conversionBuffer; |
michael@0 | 61 | conversionBuffer = new char16_t[aLength]; |
michael@0 | 62 | if (!conversionBuffer) { |
michael@0 | 63 | return NS_ERROR_OUT_OF_MEMORY; |
michael@0 | 64 | } |
michael@0 | 65 | ToLowerCase(PromiseFlatString(stringIn).get(), conversionBuffer, aLength); |
michael@0 | 66 | stringOut.Assign(conversionBuffer, aLength); |
michael@0 | 67 | delete [] conversionBuffer; |
michael@0 | 68 | } |
michael@0 | 69 | return NS_OK; |
michael@0 | 70 | } |
michael@0 | 71 | |
michael@0 | 72 | nsresult nsCollation::SetCharset(const char* aCharset) |
michael@0 | 73 | { |
michael@0 | 74 | NS_ENSURE_ARG_POINTER(aCharset); |
michael@0 | 75 | |
michael@0 | 76 | nsresult rv; |
michael@0 | 77 | nsCOMPtr <nsICharsetConverterManager> charsetConverterManager = do_GetService(NS_CHARSETCONVERTERMANAGER_CONTRACTID, &rv); |
michael@0 | 78 | if (NS_SUCCEEDED(rv)) { |
michael@0 | 79 | rv = charsetConverterManager->GetUnicodeEncoder(aCharset, |
michael@0 | 80 | getter_AddRefs(mEncoder)); |
michael@0 | 81 | } |
michael@0 | 82 | return rv; |
michael@0 | 83 | } |
michael@0 | 84 | |
michael@0 | 85 | nsresult nsCollation::UnicodeToChar(const nsAString& aSrc, char** dst) |
michael@0 | 86 | { |
michael@0 | 87 | NS_ENSURE_ARG_POINTER(dst); |
michael@0 | 88 | |
michael@0 | 89 | nsresult res = NS_OK; |
michael@0 | 90 | if (!mEncoder) |
michael@0 | 91 | res = SetCharset("ISO-8859-1"); |
michael@0 | 92 | |
michael@0 | 93 | if (NS_SUCCEEDED(res)) { |
michael@0 | 94 | const nsPromiseFlatString& src = PromiseFlatString(aSrc); |
michael@0 | 95 | const char16_t *unichars = src.get(); |
michael@0 | 96 | int32_t unicharLength = src.Length(); |
michael@0 | 97 | int32_t dstLength; |
michael@0 | 98 | res = mEncoder->GetMaxLength(unichars, unicharLength, &dstLength); |
michael@0 | 99 | if (NS_SUCCEEDED(res)) { |
michael@0 | 100 | int32_t bufLength = dstLength + 1 + 32; // extra 32 bytes for Finish() call |
michael@0 | 101 | *dst = (char *) PR_Malloc(bufLength); |
michael@0 | 102 | if (*dst) { |
michael@0 | 103 | **dst = '\0'; |
michael@0 | 104 | res = mEncoder->Convert(unichars, &unicharLength, *dst, &dstLength); |
michael@0 | 105 | |
michael@0 | 106 | if (NS_SUCCEEDED(res) || (NS_ERROR_UENC_NOMAPPING == res)) { |
michael@0 | 107 | // Finishes the conversion. The converter has the possibility to write some |
michael@0 | 108 | // extra data and flush its final state. |
michael@0 | 109 | int32_t finishLength = bufLength - dstLength; // remaining unused buffer length |
michael@0 | 110 | if (finishLength > 0) { |
michael@0 | 111 | res = mEncoder->Finish((*dst + dstLength), &finishLength); |
michael@0 | 112 | if (NS_SUCCEEDED(res)) { |
michael@0 | 113 | (*dst)[dstLength + finishLength] = '\0'; |
michael@0 | 114 | } |
michael@0 | 115 | } |
michael@0 | 116 | } |
michael@0 | 117 | if (NS_FAILED(res)) { |
michael@0 | 118 | PR_Free(*dst); |
michael@0 | 119 | *dst = nullptr; |
michael@0 | 120 | } |
michael@0 | 121 | } |
michael@0 | 122 | else { |
michael@0 | 123 | res = NS_ERROR_OUT_OF_MEMORY; |
michael@0 | 124 | } |
michael@0 | 125 | } |
michael@0 | 126 | } |
michael@0 | 127 | |
michael@0 | 128 | return res; |
michael@0 | 129 | } |
michael@0 | 130 | |
michael@0 | 131 | |
michael@0 | 132 |