/work/obj-fuzz/dist/include/ICUUtils.h
Line | Count | Source (jump to first uncovered line) |
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 "mozilla/Scoped.h" |
14 | | #include "nsString.h" |
15 | | #include "unicode/unum.h" // for UNumberFormat |
16 | | |
17 | | class nsIContent; |
18 | | |
19 | | struct ScopedUNumberFormatTraits { |
20 | | typedef UNumberFormat* type; |
21 | | static type empty() { return nullptr; } |
22 | | static void release(type handle) { if (handle) unum_close(handle); } |
23 | | }; |
24 | | typedef mozilla::Scoped<ScopedUNumberFormatTraits> AutoCloseUNumberFormat; |
25 | | |
26 | | class ICUUtils |
27 | | { |
28 | | public: |
29 | | |
30 | | /** |
31 | | * This class is used to encapsulate an nsIContent object to allow lazy |
32 | | * iteration over its primary and fallback BCP 47 language tags. |
33 | | */ |
34 | | class LanguageTagIterForContent { |
35 | | public: |
36 | | explicit LanguageTagIterForContent(nsIContent* aContent) |
37 | | : mContent(aContent) |
38 | | , mCurrentFallbackIndex(-1) |
39 | 0 | {} |
40 | | |
41 | | /** |
42 | | * Used to iterate over the nsIContent object's primary language tag and |
43 | | * its fallbacks tags. The following sources of language tag information |
44 | | * are tried in turn: |
45 | | * |
46 | | * 1) the "lang" of the nsIContent object (which is based on the 'lang'/ |
47 | | * 'xml:lang' attribute on itself or the nearest ancestor to have such |
48 | | * an attribute, if any); |
49 | | * 2) the Content-Language HTTP pragma directive or HTTP header; |
50 | | * 3) the configured language tag of the user-agent. |
51 | | * |
52 | | * Once all fallbacks have been exhausted then this function will set |
53 | | * aBCP47LangTag to the empty string. |
54 | | */ |
55 | | void GetNext(nsACString& aBCP47LangTag); |
56 | | |
57 | | bool IsAtStart() const { |
58 | | return mCurrentFallbackIndex < 0; |
59 | | } |
60 | | |
61 | | private: |
62 | | nsIContent* mContent; |
63 | | int8_t mCurrentFallbackIndex; |
64 | | }; |
65 | | |
66 | | /** |
67 | | * Attempts to localize aValue and return the result via the aLocalizedValue |
68 | | * outparam. Returns true on success. Returns false on failure, in which |
69 | | * case aLocalizedValue will be untouched. |
70 | | */ |
71 | | static bool LocalizeNumber(double aValue, |
72 | | LanguageTagIterForContent& aLangTags, |
73 | | nsAString& aLocalizedValue); |
74 | | |
75 | | /** |
76 | | * Parses the localized number that is serialized in aValue using aLangTags |
77 | | * and returns the result as a double. Returns NaN on failure. |
78 | | */ |
79 | | static double ParseNumber(nsAString& aValue, |
80 | | LanguageTagIterForContent& aLangTags); |
81 | | |
82 | | static void AssignUCharArrayToString(UChar* aICUString, |
83 | | int32_t aLength, |
84 | | nsAString& aMozString); |
85 | | |
86 | | /** |
87 | | * Map ICU UErrorCode to nsresult |
88 | | */ |
89 | | static nsresult UErrorToNsResult(const UErrorCode aErrorCode); |
90 | | |
91 | | #if 0 |
92 | | // Currently disabled because using C++ API doesn't play nicely with enabling |
93 | | // system ICU. |
94 | | |
95 | | /** |
96 | | * Converts an IETF BCP 47 language code to an ICU Locale. |
97 | | */ |
98 | | static Locale BCP47CodeToLocale(const nsAString& aBCP47Code); |
99 | | |
100 | | static void ToMozString(UnicodeString& aICUString, nsAString& aMozString); |
101 | | static void ToICUString(nsAString& aMozString, UnicodeString& aICUString); |
102 | | #endif |
103 | | }; |
104 | | |
105 | | #endif /* MOZILLA_INTERNAL_API */ |
106 | | |
107 | | #endif /* mozilla_ICUUtils_h__ */ |