tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

ICUUtils.h (3269B)


      1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
      2 /* This Source Code Form is subject to the terms of the Mozilla Public
      3 * License, v. 2.0. If a copy of the MPL was not distributed with this
      4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      5 
      6 #ifndef mozilla_ICUUtils_h__
      7 #define mozilla_ICUUtils_h__
      8 
      9 // The ICU utils implementation needs internal things like XPCOM strings and
     10 // nsGkAtom, so we only build when included into internal libs:
     11 #ifdef MOZILLA_INTERNAL_API
     12 
     13 #  include "nsStringFwd.h"
     14 #  include "unicode/unum.h"  // for UNumberFormat
     15 #  include "mozilla/intl/ICUError.h"
     16 #  include "mozilla/AlreadyAddRefed.h"
     17 
     18 class nsIContent;
     19 class nsAtom;
     20 
     21 class ICUUtils {
     22 public:
     23  /**
     24   * This class is used to encapsulate an nsIContent object to allow lazy
     25   * iteration over its primary and fallback BCP 47 language tags.
     26   */
     27  class LanguageTagIterForContent {
     28   public:
     29    explicit LanguageTagIterForContent(nsIContent* aContent)
     30        : mContent(aContent), mCurrentFallbackIndex(-1) {}
     31 
     32    /**
     33     * Used to iterate over the nsIContent object's primary language tag and
     34     * its fallbacks tags. The following sources of language tag information
     35     * are tried in turn:
     36     *
     37     * 1) the "lang" of the nsIContent object (which is based on the 'lang'/
     38     *    'xml:lang' attribute on itself or the nearest ancestor to have such
     39     *    an attribute, if any);
     40     * 2) the Content-Language HTTP pragma directive or HTTP header;
     41     * 3) the configured language tag of the user-agent.
     42     *
     43     * Once all fallbacks have been exhausted then this function will set
     44     * aBCP47LangTag to the empty string.
     45     */
     46    already_AddRefed<nsAtom> GetNext();
     47 
     48    bool IsAtStart() const { return mCurrentFallbackIndex < 0; }
     49 
     50   private:
     51    nsIContent* mContent;
     52    int8_t mCurrentFallbackIndex;
     53  };
     54 
     55  /**
     56   * Attempts to localize aValue and return the result via the aLocalizedValue
     57   * outparam. Returns true on success. Returns false on failure, in which
     58   * case aLocalizedValue will be untouched.
     59   */
     60  static bool LocalizeNumber(double aValue,
     61                             LanguageTagIterForContent& aLangTags,
     62                             nsAString& aLocalizedValue);
     63 
     64  /**
     65   * Parses the localized number that is serialized in aValue using aLangTags
     66   * and returns the result as a double. Returns NaN on failure.
     67   */
     68  static double ParseNumber(const nsAString& aValue,
     69                            LanguageTagIterForContent& aLangTags);
     70 
     71  static void AssignUCharArrayToString(UChar* aICUString, int32_t aLength,
     72                                       nsAString& aMozString);
     73 
     74  /**
     75   * Map ICUError to nsresult
     76   */
     77  static nsresult ICUErrorToNsResult(const mozilla::intl::ICUError aError);
     78 
     79 #  if 0
     80  // Currently disabled because using C++ API doesn't play nicely with enabling
     81  // system ICU.
     82 
     83  /**
     84   * Converts an IETF BCP 47 language code to an ICU Locale.
     85   */
     86  static Locale BCP47CodeToLocale(const nsAString& aBCP47Code);
     87 
     88  static void ToMozString(UnicodeString& aICUString, nsAString& aMozString);
     89  static void ToICUString(nsAString& aMozString, UnicodeString& aICUString);
     90 #  endif
     91 };
     92 
     93 #endif /* MOZILLA_INTERNAL_API */
     94 
     95 #endif /* mozilla_ICUUtils_h__ */