dom/encoding/EncodingUtils.h

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

     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 file,
     3  * You can obtain one at http://mozilla.org/MPL/2.0/. */
     5 #ifndef mozilla_dom_encodingutils_h_
     6 #define mozilla_dom_encodingutils_h_
     8 #include "nsDataHashtable.h"
     9 #include "nsString.h"
    11 class nsIUnicodeDecoder;
    12 class nsIUnicodeEncoder;
    14 namespace mozilla {
    15 namespace dom {
    17 class EncodingUtils
    18 {
    19 public:
    21   /**
    22    * Implements get an encoding algorithm from Encoding spec.
    23    * http://encoding.spec.whatwg.org/#concept-encoding-get
    24    * Given a label, this function returns the corresponding encoding or a
    25    * false.
    26    * The returned name may not be lowercased due to compatibility with
    27    * our internal implementations.
    28    *
    29    * @param      aLabel, incoming label describing charset to be decoded.
    30    * @param      aRetEncoding, returning corresponding encoding for label.
    31    * @return     false if no encoding was found for label.
    32    *             true if valid encoding found.
    33    */
    34   static bool FindEncodingForLabel(const nsACString& aLabel,
    35                                    nsACString& aOutEncoding);
    37   static bool FindEncodingForLabel(const nsAString& aLabel,
    38                                    nsACString& aOutEncoding)
    39   {
    40     return FindEncodingForLabel(NS_ConvertUTF16toUTF8(aLabel), aOutEncoding);
    41   }
    43   /**
    44    * Remove any leading and trailing space characters, following the
    45    * definition of space characters from Encoding spec.
    46    * http://encoding.spec.whatwg.org/#terminology
    47    * Note that nsAString::StripWhitespace() doesn't exactly match the
    48    * definition. It also removes all matching chars in the string,
    49    * not just leading and trailing.
    50    *
    51    * @param      aString, string to be trimmed.
    52    */
    53   template<class T>
    54   static void TrimSpaceCharacters(T& aString)
    55   {
    56     aString.Trim(" \t\n\f\r");
    57   }
    59   /**
    60    * Check is the encoding is ASCII-compatible in the sense that Basic Latin
    61    * encodes to ASCII bytes. (The reverse may not be true!)
    62    *
    63    * @param aPreferredName a preferred encoding label
    64    * @return whether the encoding is ASCII-compatible
    65    */
    66   static bool IsAsciiCompatible(const nsACString& aPreferredName);
    68   /**
    69    * Instantiates a decoder for an encoding. The input must be a
    70    * Gecko-canonical encoding name.
    71    * @param aEncoding a Gecko-canonical encoding name
    72    * @return a decoder
    73    */
    74   static already_AddRefed<nsIUnicodeDecoder>
    75   DecoderForEncoding(const char* aEncoding)
    76   {
    77     nsDependentCString encoding(aEncoding);
    78     return DecoderForEncoding(encoding);
    79   }
    81   /**
    82    * Instantiates a decoder for an encoding. The input must be a
    83    * Gecko-canonical encoding name
    84    * @param aEncoding a Gecko-canonical encoding name
    85    * @return a decoder
    86    */
    87   static already_AddRefed<nsIUnicodeDecoder>
    88   DecoderForEncoding(const nsACString& aEncoding);
    90   /**
    91    * Instantiates an encoder for an encoding. The input must be a
    92    * Gecko-canonical encoding name.
    93    * @param aEncoding a Gecko-canonical encoding name
    94    * @return an encoder
    95    */
    96   static already_AddRefed<nsIUnicodeEncoder>
    97   EncoderForEncoding(const char* aEncoding)
    98   {
    99     nsDependentCString encoding(aEncoding);
   100     return EncoderForEncoding(encoding);
   101   }
   103   /**
   104    * Instantiates an encoder for an encoding. The input must be a
   105    * Gecko-canonical encoding name.
   106    * @param aEncoding a Gecko-canonical encoding name
   107    * @return an encoder
   108    */
   109   static already_AddRefed<nsIUnicodeEncoder>
   110   EncoderForEncoding(const nsACString& aEncoding);
   112 private:
   113   EncodingUtils() MOZ_DELETE;
   114 };
   116 } // dom
   117 } // mozilla
   119 #endif // mozilla_dom_encodingutils_h_

mercurial