dom/encoding/EncodingUtils.cpp

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 #include "mozilla/dom/EncodingUtils.h"
michael@0 6
michael@0 7 #include "mozilla/ArrayUtils.h" // ArrayLength
michael@0 8 #include "nsUConvPropertySearch.h"
michael@0 9 #include "nsIUnicodeDecoder.h"
michael@0 10 #include "nsIUnicodeEncoder.h"
michael@0 11 #include "nsComponentManagerUtils.h"
michael@0 12
michael@0 13 namespace mozilla {
michael@0 14 namespace dom {
michael@0 15
michael@0 16 static const char* labelsEncodings[][3] = {
michael@0 17 #include "labelsencodings.properties.h"
michael@0 18 };
michael@0 19
michael@0 20 bool
michael@0 21 EncodingUtils::FindEncodingForLabel(const nsACString& aLabel,
michael@0 22 nsACString& aOutEncoding)
michael@0 23 {
michael@0 24 // Save aLabel first because it may refer the same string as aOutEncoding.
michael@0 25 nsCString label(aLabel);
michael@0 26
michael@0 27 EncodingUtils::TrimSpaceCharacters(label);
michael@0 28 if (label.IsEmpty()) {
michael@0 29 aOutEncoding.Truncate();
michael@0 30 return false;
michael@0 31 }
michael@0 32
michael@0 33 ToLowerCase(label);
michael@0 34 return NS_SUCCEEDED(nsUConvPropertySearch::SearchPropertyValue(
michael@0 35 labelsEncodings, ArrayLength(labelsEncodings), label, aOutEncoding));
michael@0 36 }
michael@0 37
michael@0 38 bool
michael@0 39 EncodingUtils::IsAsciiCompatible(const nsACString& aPreferredName)
michael@0 40 {
michael@0 41 return !(aPreferredName.LowerCaseEqualsLiteral("utf-16") ||
michael@0 42 aPreferredName.LowerCaseEqualsLiteral("utf-16be") ||
michael@0 43 aPreferredName.LowerCaseEqualsLiteral("utf-16le") ||
michael@0 44 aPreferredName.LowerCaseEqualsLiteral("replacement") ||
michael@0 45 aPreferredName.LowerCaseEqualsLiteral("hz-gb-2312") ||
michael@0 46 aPreferredName.LowerCaseEqualsLiteral("utf-7") ||
michael@0 47 aPreferredName.LowerCaseEqualsLiteral("x-imap4-modified-utf7"));
michael@0 48 }
michael@0 49
michael@0 50 already_AddRefed<nsIUnicodeDecoder>
michael@0 51 EncodingUtils::DecoderForEncoding(const nsACString& aEncoding)
michael@0 52 {
michael@0 53 nsAutoCString contractId(NS_UNICODEDECODER_CONTRACTID_BASE);
michael@0 54 contractId.Append(aEncoding);
michael@0 55
michael@0 56 nsCOMPtr<nsIUnicodeDecoder> decoder = do_CreateInstance(contractId.get());
michael@0 57 MOZ_ASSERT(decoder, "Tried to create decoder for unknown encoding.");
michael@0 58 return decoder.forget();
michael@0 59 }
michael@0 60
michael@0 61 already_AddRefed<nsIUnicodeEncoder>
michael@0 62 EncodingUtils::EncoderForEncoding(const nsACString& aEncoding)
michael@0 63 {
michael@0 64 nsAutoCString contractId(NS_UNICODEENCODER_CONTRACTID_BASE);
michael@0 65 contractId.Append(aEncoding);
michael@0 66
michael@0 67 nsCOMPtr<nsIUnicodeEncoder> encoder = do_CreateInstance(contractId.get());
michael@0 68 MOZ_ASSERT(encoder, "Tried to create encoder for unknown encoding.");
michael@0 69 return encoder.forget();
michael@0 70 }
michael@0 71
michael@0 72 } // namespace dom
michael@0 73 } // namespace mozilla

mercurial