michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "mozilla/dom/EncodingUtils.h" michael@0: michael@0: #include "mozilla/ArrayUtils.h" // ArrayLength michael@0: #include "nsUConvPropertySearch.h" michael@0: #include "nsIUnicodeDecoder.h" michael@0: #include "nsIUnicodeEncoder.h" michael@0: #include "nsComponentManagerUtils.h" michael@0: michael@0: namespace mozilla { michael@0: namespace dom { michael@0: michael@0: static const char* labelsEncodings[][3] = { michael@0: #include "labelsencodings.properties.h" michael@0: }; michael@0: michael@0: bool michael@0: EncodingUtils::FindEncodingForLabel(const nsACString& aLabel, michael@0: nsACString& aOutEncoding) michael@0: { michael@0: // Save aLabel first because it may refer the same string as aOutEncoding. michael@0: nsCString label(aLabel); michael@0: michael@0: EncodingUtils::TrimSpaceCharacters(label); michael@0: if (label.IsEmpty()) { michael@0: aOutEncoding.Truncate(); michael@0: return false; michael@0: } michael@0: michael@0: ToLowerCase(label); michael@0: return NS_SUCCEEDED(nsUConvPropertySearch::SearchPropertyValue( michael@0: labelsEncodings, ArrayLength(labelsEncodings), label, aOutEncoding)); michael@0: } michael@0: michael@0: bool michael@0: EncodingUtils::IsAsciiCompatible(const nsACString& aPreferredName) michael@0: { michael@0: return !(aPreferredName.LowerCaseEqualsLiteral("utf-16") || michael@0: aPreferredName.LowerCaseEqualsLiteral("utf-16be") || michael@0: aPreferredName.LowerCaseEqualsLiteral("utf-16le") || michael@0: aPreferredName.LowerCaseEqualsLiteral("replacement") || michael@0: aPreferredName.LowerCaseEqualsLiteral("hz-gb-2312") || michael@0: aPreferredName.LowerCaseEqualsLiteral("utf-7") || michael@0: aPreferredName.LowerCaseEqualsLiteral("x-imap4-modified-utf7")); michael@0: } michael@0: michael@0: already_AddRefed michael@0: EncodingUtils::DecoderForEncoding(const nsACString& aEncoding) michael@0: { michael@0: nsAutoCString contractId(NS_UNICODEDECODER_CONTRACTID_BASE); michael@0: contractId.Append(aEncoding); michael@0: michael@0: nsCOMPtr decoder = do_CreateInstance(contractId.get()); michael@0: MOZ_ASSERT(decoder, "Tried to create decoder for unknown encoding."); michael@0: return decoder.forget(); michael@0: } michael@0: michael@0: already_AddRefed michael@0: EncodingUtils::EncoderForEncoding(const nsACString& aEncoding) michael@0: { michael@0: nsAutoCString contractId(NS_UNICODEENCODER_CONTRACTID_BASE); michael@0: contractId.Append(aEncoding); michael@0: michael@0: nsCOMPtr encoder = do_CreateInstance(contractId.get()); michael@0: MOZ_ASSERT(encoder, "Tried to create encoder for unknown encoding."); michael@0: return encoder.forget(); michael@0: } michael@0: michael@0: } // namespace dom michael@0: } // namespace mozilla