1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2  * vim: set ts=8 sts=2 et sw=2 tw=80:
3  * This Source Code Form is subject to the terms of the Mozilla Public
4  * License, v. 2.0. If a copy of the MPL was not distributed with this
5  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 
7 /* Structured representation of Unicode locale IDs used with Intl functions. */
8 
9 #ifndef builtin_intl_LanguageTag_h
10 #define builtin_intl_LanguageTag_h
11 
12 #include "mozilla/intl/Locale.h"
13 #include "mozilla/Span.h"
14 
15 #include "js/AllocPolicy.h"
16 #include "js/GCAPI.h"
17 #include "js/Result.h"
18 #include "js/RootingAPI.h"
19 #include "js/Utility.h"
20 #include "js/Vector.h"
21 
22 struct JS_PUBLIC_API JSContext;
23 class JSLinearString;
24 class JS_PUBLIC_API JSString;
25 class JS_PUBLIC_API JSTracer;
26 
27 namespace js {
28 
29 namespace intl {
30 
31 /**
32  * Parse a string Unicode BCP 47 locale identifier. If successful, store in
33  * |result| and return true. Otherwise return false.
34  */
35 [[nodiscard]] bool ParseLocale(JSContext* cx, JS::Handle<JSLinearString*> str,
36                                mozilla::intl::Locale& result);
37 
38 /**
39  * Parse a string as a standalone |language| tag. If |str| is a standalone
40  * language tag, store it in |result| and return true. Otherwise return false.
41  */
42 [[nodiscard]] bool ParseStandaloneLanguageTag(
43     JS::Handle<JSLinearString*> str, mozilla::intl::LanguageSubtag& result);
44 
45 /**
46  * Parse a string as a standalone |script| tag. If |str| is a standalone script
47  * tag, store it in |result| and return true. Otherwise return false.
48  */
49 [[nodiscard]] bool ParseStandaloneScriptTag(
50     JS::Handle<JSLinearString*> str, mozilla::intl::ScriptSubtag& result);
51 
52 /**
53  * Parse a string as a standalone |region| tag. If |str| is a standalone region
54  * tag, store it in |result| and return true. Otherwise return false.
55  */
56 [[nodiscard]] bool ParseStandaloneRegionTag(
57     JS::Handle<JSLinearString*> str, mozilla::intl::RegionSubtag& result);
58 
59 /**
60  * Parse a string as an ISO-639 language code. Return |nullptr| in the result if
61  * the input could not be parsed or the canonical form of the resulting language
62  * tag contains more than a single language subtag.
63  */
64 JS::Result<JSString*> ParseStandaloneISO639LanguageTag(
65     JSContext* cx, JS::Handle<JSLinearString*> str);
66 
67 class UnicodeExtensionKeyword final {
68   char key_[mozilla::intl::LanguageTagLimits::UnicodeKeyLength];
69   JSLinearString* type_;
70 
71  public:
72   using UnicodeKey =
73       const char (&)[mozilla::intl::LanguageTagLimits::UnicodeKeyLength + 1];
74   using UnicodeKeySpan =
75       mozilla::Span<const char,
76                     mozilla::intl::LanguageTagLimits::UnicodeKeyLength>;
77 
UnicodeExtensionKeyword(UnicodeKey key,JSLinearString * type)78   UnicodeExtensionKeyword(UnicodeKey key, JSLinearString* type)
79       : key_{key[0], key[1]}, type_(type) {}
80 
key()81   UnicodeKeySpan key() const { return {key_, sizeof(key_)}; }
type()82   JSLinearString* type() const { return type_; }
83 
84   void trace(JSTracer* trc);
85 };
86 
87 [[nodiscard]] extern bool ApplyUnicodeExtensionToTag(
88     JSContext* cx, mozilla::intl::Locale& tag,
89     JS::HandleVector<UnicodeExtensionKeyword> keywords);
90 
91 }  // namespace intl
92 
93 }  // namespace js
94 
95 #endif /* builtin_intl_LanguageTag_h */
96