intl/unicharutil/util/ICUUtils.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/intl/unicharutil/util/ICUUtils.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,109 @@
     1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +#ifndef mozilla_ICUUtils_h__
    1.10 +#define mozilla_ICUUtils_h__
    1.11 +
    1.12 +// We only build the ICU utils if we're building ICU:
    1.13 +#ifdef ENABLE_INTL_API
    1.14 +
    1.15 +// The ICU utils implementation needs internal things like XPCOM strings and
    1.16 +// nsGkAtom, so we only build when included into internal libs:
    1.17 +#ifdef MOZILLA_INTERNAL_API
    1.18 +
    1.19 +#include "mozilla/Scoped.h"
    1.20 +#include "nsStringGlue.h"
    1.21 +#include "unicode/unum.h" // for UNumberFormat
    1.22 +
    1.23 +class nsIContent;
    1.24 +
    1.25 +namespace {
    1.26 +  struct ScopedUNumberFormatTraits {
    1.27 +    typedef UNumberFormat* type;
    1.28 +    static type empty() { return nullptr; }
    1.29 +    static void release(type handle) { if (handle) unum_close(handle); }
    1.30 +  };
    1.31 +};
    1.32 +typedef mozilla::Scoped<ScopedUNumberFormatTraits> AutoCloseUNumberFormat;
    1.33 +
    1.34 +class ICUUtils
    1.35 +{
    1.36 +public:
    1.37 +
    1.38 +  /**
    1.39 +   * This class is used to encapsulate an nsIContent object to allow lazy
    1.40 +   * iteration over its primary and fallback BCP 47 language tags.
    1.41 +   */
    1.42 +  class LanguageTagIterForContent {
    1.43 +  public:
    1.44 +    LanguageTagIterForContent(nsIContent* aContent)
    1.45 +      : mContent(aContent)
    1.46 +      , mCurrentFallbackIndex(-1)
    1.47 +    {}
    1.48 +
    1.49 +    /**
    1.50 +     * Used to iterate over the nsIContent object's primary language tag and
    1.51 +     * its fallbacks tags. The following sources of language tag information
    1.52 +     * are tried in turn:
    1.53 +     *
    1.54 +     * 1) the "lang" of the nsIContent object (which is based on the 'lang'/
    1.55 +     *    'xml:lang' attribute on itself or the nearest ancestor to have such
    1.56 +     *    an attribute, if any);
    1.57 +     * 2) the Content-Language HTTP pragma directive or HTTP header;
    1.58 +     * 3) the configured language tag of the user-agent.
    1.59 +     *
    1.60 +     * Once all fallbacks have been exhausted then this function will set
    1.61 +     * aBCP47LangTag to the empty string.
    1.62 +     */
    1.63 +    void GetNext(nsACString& aBCP47LangTag);
    1.64 +
    1.65 +    bool IsAtStart() const {
    1.66 +      return mCurrentFallbackIndex < 0;
    1.67 +    }
    1.68 +
    1.69 +  private:
    1.70 +    nsIContent* mContent;
    1.71 +    int8_t mCurrentFallbackIndex;
    1.72 +  };
    1.73 +
    1.74 +  /**
    1.75 +   * Attempts to localize aValue and return the result via the aLocalizedValue
    1.76 +   * outparam. Returns true on success. Returns false on failure, in which
    1.77 +   * case aLocalizedValue will be untouched.
    1.78 +   */
    1.79 +  static bool LocalizeNumber(double aValue,
    1.80 +                             LanguageTagIterForContent& aLangTags,
    1.81 +                             nsAString& aLocalizedValue);
    1.82 +
    1.83 +  /**
    1.84 +   * Parses the localized number that is serialized in aValue using aLangTags
    1.85 +   * and returns the result as a double. Returns NaN on failure.
    1.86 +   */
    1.87 +  static double ParseNumber(nsAString& aValue,
    1.88 +                            LanguageTagIterForContent& aLangTags);
    1.89 +
    1.90 +  static void AssignUCharArrayToString(UChar* aICUString,
    1.91 +                                       int32_t aLength,
    1.92 +                                       nsAString& aMozString);
    1.93 +
    1.94 +#if 0
    1.95 +  // Currently disabled because using C++ API doesn't play nicely with enabling
    1.96 +  // system ICU.
    1.97 +
    1.98 +  /**
    1.99 +   * Converts an IETF BCP 47 language code to an ICU Locale.
   1.100 +   */
   1.101 +  static Locale BCP47CodeToLocale(const nsAString& aBCP47Code);
   1.102 +
   1.103 +  static void ToMozString(UnicodeString& aICUString, nsAString& aMozString);
   1.104 +  static void ToICUString(nsAString& aMozString, UnicodeString& aICUString);
   1.105 +#endif
   1.106 +};
   1.107 +
   1.108 +#endif /* ENABLE_INTL_API */
   1.109 +#endif /* MOZILLA_INTERNAL_API */
   1.110 +
   1.111 +#endif /* mozilla_ICUUtils_h__ */
   1.112 +

mercurial