LanguageTag.h (3540B)
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 #include "js/Utility.h" 18 19 struct JS_PUBLIC_API JSContext; 20 class JSLinearString; 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 a standalone |variant| tag sequence. If |str| is a 57 * standalone variant tag sequence, store all variant tags in |result| and 58 * return true. Otherwise return false. 59 */ 60 [[nodiscard]] bool ParseStandaloneVariantTag( 61 JS::Handle<JSLinearString*> str, 62 mozilla::intl::Locale::VariantsVector& result, bool* success); 63 64 /** 65 * Parse a string as an ISO-639 language code. Return |nullptr| in the result if 66 * the input could not be parsed or the canonical form of the resulting language 67 * tag contains more than a single language subtag. 68 */ 69 JS::Result<JSLinearString*> ParseStandaloneISO639LanguageTag( 70 JSContext* cx, JS::Handle<JSLinearString*> str); 71 72 class UnicodeExtensionKeyword final { 73 char key_[mozilla::intl::LanguageTagLimits::UnicodeKeyLength]; 74 JSLinearString* type_; 75 76 public: 77 using UnicodeKey = 78 const char (&)[mozilla::intl::LanguageTagLimits::UnicodeKeyLength + 1]; 79 using UnicodeKeySpan = 80 mozilla::Span<const char, 81 mozilla::intl::LanguageTagLimits::UnicodeKeyLength>; 82 83 UnicodeExtensionKeyword(UnicodeKey key, JSLinearString* type) 84 : key_{key[0], key[1]}, type_(type) {} 85 86 UnicodeKeySpan key() const { return {key_, sizeof(key_)}; } 87 JSLinearString* type() const { return type_; } 88 89 void trace(JSTracer* trc); 90 }; 91 92 [[nodiscard]] extern bool ApplyUnicodeExtensionToTag( 93 JSContext* cx, mozilla::intl::Locale& tag, 94 JS::HandleVector<UnicodeExtensionKeyword> keywords); 95 96 JS::UniqueChars FormatLocale( 97 JSContext* cx, JS::Handle<JSObject*> internals, 98 JS::HandleVector<UnicodeExtensionKeyword> keywords); 99 100 } // namespace intl 101 102 } // namespace js 103 104 #endif /* builtin_intl_LanguageTag_h */