dom/encoding/EncodingUtils.h
author Bobby Holley <bobbyholley@gmail.com>
Fri, 31 Jan 2014 14:51:41 -0800
changeset 166310 8c206e77eb64ed7671ced59ddf599c4af79351ed
parent 157481 04500118250742378ac412dcbfc50509d26b4cc1
child 182095 15680e55195c68c876414ca8521a546dd2292915
permissions -rw-r--r--
Bug 962449 - Don't call initIteratorClasses directly, and use ensureConstructor. r=luke This isn't strictly necessary at this point. The basic issue is that there are a few new classes (iterator stuff, intl stuff, and typed objects) that use this kind of constructor bootstrap scheme, and each of them will need something like this to move to ClassSpecs. But that doesn't actually need to happen until we convert them. I wrote this patch before I realized this, and I believe it's correct, so I figured I'd get it landed along with everything. But I'm also ok to skip it for now.

/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifndef mozilla_dom_encodingutils_h_
#define mozilla_dom_encodingutils_h_

#include "nsDataHashtable.h"
#include "nsString.h"

class nsIUnicodeDecoder;
class nsIUnicodeEncoder;

namespace mozilla {
namespace dom {

class EncodingUtils
{
public:

  /**
   * Implements get an encoding algorithm from Encoding spec.
   * http://encoding.spec.whatwg.org/#concept-encoding-get
   * Given a label, this function returns the corresponding encoding or a
   * false.
   * The returned name may not be lowercased due to compatibility with
   * our internal implementations.
   *
   * @param      aLabel, incoming label describing charset to be decoded.
   * @param      aRetEncoding, returning corresponding encoding for label.
   * @return     false if no encoding was found for label.
   *             true if valid encoding found.
   */
  static bool FindEncodingForLabel(const nsACString& aLabel,
                                   nsACString& aOutEncoding);

  static bool FindEncodingForLabel(const nsAString& aLabel,
                                   nsACString& aOutEncoding)
  {
    return FindEncodingForLabel(NS_ConvertUTF16toUTF8(aLabel), aOutEncoding);
  }

  /**
   * Remove any leading and trailing space characters, following the
   * definition of space characters from Encoding spec.
   * http://encoding.spec.whatwg.org/#terminology
   * Note that nsAString::StripWhitespace() doesn't exactly match the
   * definition. It also removes all matching chars in the string,
   * not just leading and trailing.
   *
   * @param      aString, string to be trimmed.
   */
  template<class T>
  static void TrimSpaceCharacters(T& aString)
  {
    aString.Trim(" \t\n\f\r");
  }

  /**
   * Check is the encoding is ASCII-compatible in the sense that Basic Latin
   * encodes to ASCII bytes. (The reverse may not be true!)
   *
   * @param aPreferredName a preferred encoding label
   * @return whether the encoding is ASCII-compatible
   */
  static bool IsAsciiCompatible(const nsACString& aPreferredName);

  /**
   * Instantiates a decoder for an encoding. The input must be a
   * Gecko-canonical encoding name.
   * @param aEncoding a Gecko-canonical encoding name
   * @return a decoder
   */
  static already_AddRefed<nsIUnicodeDecoder>
  DecoderForEncoding(const char* aEncoding)
  {
    nsDependentCString encoding(aEncoding);
    return DecoderForEncoding(encoding);
  }

  /**
   * Instantiates a decoder for an encoding. The input must be a
   * Gecko-canonical encoding name
   * @param aEncoding a Gecko-canonical encoding name
   * @return a decoder
   */
  static already_AddRefed<nsIUnicodeDecoder>
  DecoderForEncoding(const nsACString& aEncoding);

  /**
   * Instantiates an encoder for an encoding. The input must be a
   * Gecko-canonical encoding name.
   * @param aEncoding a Gecko-canonical encoding name
   * @return an encoder
   */
  static already_AddRefed<nsIUnicodeEncoder>
  EncoderForEncoding(const char* aEncoding)
  {
    nsDependentCString encoding(aEncoding);
    return EncoderForEncoding(encoding);
  }

  /**
   * Instantiates an encoder for an encoding. The input must be a
   * Gecko-canonical encoding name.
   * @param aEncoding a Gecko-canonical encoding name
   * @return an encoder
   */
  static already_AddRefed<nsIUnicodeEncoder>
  EncoderForEncoding(const nsACString& aEncoding);

private:
  EncodingUtils() MOZ_DELETE;
};

} // dom
} // mozilla

#endif // mozilla_dom_encodingutils_h_