1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/dom/encoding/EncodingUtils.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,73 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 + 1.8 +#include "mozilla/dom/EncodingUtils.h" 1.9 + 1.10 +#include "mozilla/ArrayUtils.h" // ArrayLength 1.11 +#include "nsUConvPropertySearch.h" 1.12 +#include "nsIUnicodeDecoder.h" 1.13 +#include "nsIUnicodeEncoder.h" 1.14 +#include "nsComponentManagerUtils.h" 1.15 + 1.16 +namespace mozilla { 1.17 +namespace dom { 1.18 + 1.19 +static const char* labelsEncodings[][3] = { 1.20 +#include "labelsencodings.properties.h" 1.21 +}; 1.22 + 1.23 +bool 1.24 +EncodingUtils::FindEncodingForLabel(const nsACString& aLabel, 1.25 + nsACString& aOutEncoding) 1.26 +{ 1.27 + // Save aLabel first because it may refer the same string as aOutEncoding. 1.28 + nsCString label(aLabel); 1.29 + 1.30 + EncodingUtils::TrimSpaceCharacters(label); 1.31 + if (label.IsEmpty()) { 1.32 + aOutEncoding.Truncate(); 1.33 + return false; 1.34 + } 1.35 + 1.36 + ToLowerCase(label); 1.37 + return NS_SUCCEEDED(nsUConvPropertySearch::SearchPropertyValue( 1.38 + labelsEncodings, ArrayLength(labelsEncodings), label, aOutEncoding)); 1.39 +} 1.40 + 1.41 +bool 1.42 +EncodingUtils::IsAsciiCompatible(const nsACString& aPreferredName) 1.43 +{ 1.44 + return !(aPreferredName.LowerCaseEqualsLiteral("utf-16") || 1.45 + aPreferredName.LowerCaseEqualsLiteral("utf-16be") || 1.46 + aPreferredName.LowerCaseEqualsLiteral("utf-16le") || 1.47 + aPreferredName.LowerCaseEqualsLiteral("replacement") || 1.48 + aPreferredName.LowerCaseEqualsLiteral("hz-gb-2312") || 1.49 + aPreferredName.LowerCaseEqualsLiteral("utf-7") || 1.50 + aPreferredName.LowerCaseEqualsLiteral("x-imap4-modified-utf7")); 1.51 +} 1.52 + 1.53 +already_AddRefed<nsIUnicodeDecoder> 1.54 +EncodingUtils::DecoderForEncoding(const nsACString& aEncoding) 1.55 +{ 1.56 + nsAutoCString contractId(NS_UNICODEDECODER_CONTRACTID_BASE); 1.57 + contractId.Append(aEncoding); 1.58 + 1.59 + nsCOMPtr<nsIUnicodeDecoder> decoder = do_CreateInstance(contractId.get()); 1.60 + MOZ_ASSERT(decoder, "Tried to create decoder for unknown encoding."); 1.61 + return decoder.forget(); 1.62 +} 1.63 + 1.64 +already_AddRefed<nsIUnicodeEncoder> 1.65 +EncodingUtils::EncoderForEncoding(const nsACString& aEncoding) 1.66 +{ 1.67 + nsAutoCString contractId(NS_UNICODEENCODER_CONTRACTID_BASE); 1.68 + contractId.Append(aEncoding); 1.69 + 1.70 + nsCOMPtr<nsIUnicodeEncoder> encoder = do_CreateInstance(contractId.get()); 1.71 + MOZ_ASSERT(encoder, "Tried to create encoder for unknown encoding."); 1.72 + return encoder.forget(); 1.73 +} 1.74 + 1.75 +} // namespace dom 1.76 +} // namespace mozilla