summaryrefslogtreecommitdiffstats
path: root/intl/unicharutil/util/ICUUtils.h
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-19 00:47:55 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-19 00:47:55 +0000
commit26a029d407be480d791972afb5975cf62c9360a6 (patch)
treef435a8308119effd964b339f76abb83a57c29483 /intl/unicharutil/util/ICUUtils.h
parentInitial commit. (diff)
downloadfirefox-26a029d407be480d791972afb5975cf62c9360a6.tar.xz
firefox-26a029d407be480d791972afb5975cf62c9360a6.zip
Adding upstream version 124.0.1.upstream/124.0.1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'intl/unicharutil/util/ICUUtils.h')
-rw-r--r--intl/unicharutil/util/ICUUtils.h95
1 files changed, 95 insertions, 0 deletions
diff --git a/intl/unicharutil/util/ICUUtils.h b/intl/unicharutil/util/ICUUtils.h
new file mode 100644
index 0000000000..c5d95d773f
--- /dev/null
+++ b/intl/unicharutil/util/ICUUtils.h
@@ -0,0 +1,95 @@
+/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/* 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_ICUUtils_h__
+#define mozilla_ICUUtils_h__
+
+// The ICU utils implementation needs internal things like XPCOM strings and
+// nsGkAtom, so we only build when included into internal libs:
+#ifdef MOZILLA_INTERNAL_API
+
+# include "nsStringFwd.h"
+# include "unicode/unum.h" // for UNumberFormat
+# include "mozilla/intl/ICUError.h"
+# include "mozilla/AlreadyAddRefed.h"
+
+class nsIContent;
+class nsAtom;
+
+class ICUUtils {
+ public:
+ /**
+ * This class is used to encapsulate an nsIContent object to allow lazy
+ * iteration over its primary and fallback BCP 47 language tags.
+ */
+ class LanguageTagIterForContent {
+ public:
+ explicit LanguageTagIterForContent(nsIContent* aContent)
+ : mContent(aContent), mCurrentFallbackIndex(-1) {}
+
+ /**
+ * Used to iterate over the nsIContent object's primary language tag and
+ * its fallbacks tags. The following sources of language tag information
+ * are tried in turn:
+ *
+ * 1) the "lang" of the nsIContent object (which is based on the 'lang'/
+ * 'xml:lang' attribute on itself or the nearest ancestor to have such
+ * an attribute, if any);
+ * 2) the Content-Language HTTP pragma directive or HTTP header;
+ * 3) the configured language tag of the user-agent.
+ *
+ * Once all fallbacks have been exhausted then this function will set
+ * aBCP47LangTag to the empty string.
+ */
+ already_AddRefed<nsAtom> GetNext();
+
+ bool IsAtStart() const { return mCurrentFallbackIndex < 0; }
+
+ private:
+ nsIContent* mContent;
+ int8_t mCurrentFallbackIndex;
+ };
+
+ /**
+ * Attempts to localize aValue and return the result via the aLocalizedValue
+ * outparam. Returns true on success. Returns false on failure, in which
+ * case aLocalizedValue will be untouched.
+ */
+ static bool LocalizeNumber(double aValue,
+ LanguageTagIterForContent& aLangTags,
+ nsAString& aLocalizedValue);
+
+ /**
+ * Parses the localized number that is serialized in aValue using aLangTags
+ * and returns the result as a double. Returns NaN on failure.
+ */
+ static double ParseNumber(const nsAString& aValue,
+ LanguageTagIterForContent& aLangTags);
+
+ static void AssignUCharArrayToString(UChar* aICUString, int32_t aLength,
+ nsAString& aMozString);
+
+ /**
+ * Map ICUError to nsresult
+ */
+ static nsresult ICUErrorToNsResult(const mozilla::intl::ICUError aError);
+
+# if 0
+ // Currently disabled because using C++ API doesn't play nicely with enabling
+ // system ICU.
+
+ /**
+ * Converts an IETF BCP 47 language code to an ICU Locale.
+ */
+ static Locale BCP47CodeToLocale(const nsAString& aBCP47Code);
+
+ static void ToMozString(UnicodeString& aICUString, nsAString& aMozString);
+ static void ToICUString(nsAString& aMozString, UnicodeString& aICUString);
+# endif
+};
+
+#endif /* MOZILLA_INTERNAL_API */
+
+#endif /* mozilla_ICUUtils_h__ */