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