dom/encoding/EncodingUtils.cpp

Fri, 16 Jan 2015 18:13:44 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Fri, 16 Jan 2015 18:13:44 +0100
branch
TOR_BUG_9701
changeset 14
925c144e1f1f
permissions
-rw-r--r--

Integrate suggestion from review to improve consistency with existing code.

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

mercurial