| 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/Result.h" |
| 16 |
#include "js/RootingAPI.h" |
| 17 |
|
| 18 |
struct JS_PUBLIC_API JSContext; |
| 19 |
class JSLinearString; |
| 20 |
class JS_PUBLIC_API JSString; |
| 21 |
class JS_PUBLIC_API JSTracer; |
| 22 |
|
| 23 |
namespace js { |
| 24 |
|
| 25 |
namespace intl { |
| 26 |
|
| 27 |
/** |
| 28 |
* Parse a string Unicode BCP 47 locale identifier. If successful, store in |
| 29 |
* |result| and return true. Otherwise return false. |
| 30 |
*/ |
| 31 |
[[nodiscard]] bool ParseLocale(JSContext* cx, JS::Handle<JSLinearString*> str, |
| 32 |
mozilla::intl::Locale& result); |
| 33 |
|
| 34 |
/** |
| 35 |
* Parse a string as a standalone |language| tag. If |str| is a standalone |
| 36 |
* language tag, store it in |result| and return true. Otherwise return false. |
| 37 |
*/ |
| 38 |
[[nodiscard]] bool ParseStandaloneLanguageTag( |
| 39 |
JS::Handle<JSLinearString*> str, mozilla::intl::LanguageSubtag& result); |
| 40 |
|
| 41 |
/** |
| 42 |
* Parse a string as a standalone |script| tag. If |str| is a standalone script |
| 43 |
* tag, store it in |result| and return true. Otherwise return false. |
| 44 |
*/ |
| 45 |
[[nodiscard]] bool ParseStandaloneScriptTag( |
| 46 |
JS::Handle<JSLinearString*> str, mozilla::intl::ScriptSubtag& result); |
| 47 |
|
| 48 |
/** |
| 49 |
* Parse a string as a standalone |region| tag. If |str| is a standalone region |
| 50 |
* tag, store it in |result| and return true. Otherwise return false. |
| 51 |
*/ |
| 52 |
[[nodiscard]] bool ParseStandaloneRegionTag( |
| 53 |
JS::Handle<JSLinearString*> str, mozilla::intl::RegionSubtag& result); |
| 54 |
|
| 55 |
/** |
| 56 |
* Parse a string as an ISO-639 language code. Return |nullptr| in the result if |
| 57 |
* the input could not be parsed or the canonical form of the resulting language |
| 58 |
* tag contains more than a single language subtag. |
| 59 |
*/ |
| 60 |
JS::Result<JSString*> ParseStandaloneISO639LanguageTag( |
| 61 |
JSContext* cx, JS::Handle<JSLinearString*> str); |
| 62 |
|
| 63 |
class UnicodeExtensionKeyword final { |
| 64 |
char key_[mozilla::intl::LanguageTagLimits::UnicodeKeyLength]; |
| 65 |
JSLinearString* type_; |
| 66 |
|
| 67 |
public: |
| 68 |
using UnicodeKey = |
| 69 |
const char (&)[mozilla::intl::LanguageTagLimits::UnicodeKeyLength + 1]; |
| 70 |
using UnicodeKeySpan = |
| 71 |
mozilla::Span<const char, |
| 72 |
mozilla::intl::LanguageTagLimits::UnicodeKeyLength>; |
| 73 |
|
| 74 |
UnicodeExtensionKeyword(UnicodeKey key, JSLinearString* type) |
| 75 |
: key_{key[0], key[1]}, type_(type) {} |
| 76 |
|
| 77 |
UnicodeKeySpan key() const { return {key_, sizeof(key_)}; } |
| 78 |
JSLinearString* type() const { return type_; } |
| 79 |
|
| 80 |
void trace(JSTracer* trc); |
| 81 |
}; |
| 82 |
|
| 83 |
[[nodiscard]] extern bool ApplyUnicodeExtensionToTag( |
| 84 |
JSContext* cx, mozilla::intl::Locale& tag, |
| 85 |
JS::HandleVector<UnicodeExtensionKeyword> keywords); |
| 86 |
|
| 87 |
} // namespace intl |
| 88 |
|
| 89 |
} // namespace js |
| 90 |
|
| 91 |
#endif /* builtin_intl_LanguageTag_h */ |