| 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 |
#include "builtin/intl/LanguageTag.h" |
| 8 |
|
| 9 |
#include "mozilla/intl/Locale.h" |
| 10 |
#include "mozilla/Span.h" |
| 11 |
|
| 12 |
#include "builtin/intl/StringAsciiChars.h" |
| 13 |
#include "gc/Tracer.h" |
| 14 |
#include "vm/JSContext.h" |
| 15 |
|
| 16 |
namespace js { |
| 17 |
namespace intl { |
| 18 |
|
| 19 |
[[nodiscard]] bool ParseLocale(JSContext* cx, Handle<JSLinearString*> str, |
| 20 |
mozilla::intl::Locale& result) { |
| 21 |
if (StringIsAscii(str)) { |
| 22 |
intl::StringAsciiChars chars(str); |
| 23 |
if (!chars.init(cx)) { |
| 24 |
return false; |
| 25 |
} |
| 26 |
|
| 27 |
if (mozilla::intl::LocaleParser::TryParse(chars, result).isOk()) { |
| 28 |
return true; |
| 29 |
} |
| 30 |
} |
| 31 |
|
| 32 |
if (UniqueChars localeChars = QuoteString(cx, str, '"')) { |
| 33 |
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, |
| 34 |
JSMSG_INVALID_LANGUAGE_TAG, localeChars.get()); |
| 35 |
} |
| 36 |
return false; |
| 37 |
} |
| 38 |
|
| 39 |
bool ParseStandaloneLanguageTag(Handle<JSLinearString*> str, |
| 40 |
mozilla::intl::LanguageSubtag& result) { |
| 41 |
// Tell the analysis the |IsStructurallyValidLanguageTag| function can't GC. |
| 42 |
JS::AutoSuppressGCAnalysis nogc; |
| 43 |
|
| 44 |
if (str->hasLatin1Chars()) { |
| 45 |
if (!mozilla::intl::IsStructurallyValidLanguageTag<Latin1Char>( |
| 46 |
str->latin1Range(nogc))) { |
| 47 |
return false; |
| 48 |
} |
| 49 |
result.Set<Latin1Char>(str->latin1Range(nogc)); |
| 50 |
} else { |
| 51 |
if (!mozilla::intl::IsStructurallyValidLanguageTag<char16_t>( |
| 52 |
str->twoByteRange(nogc))) { |
| 53 |
return false; |
| 54 |
} |
| 55 |
result.Set<char16_t>(str->twoByteRange(nogc)); |
| 56 |
} |
| 57 |
return true; |
| 58 |
} |
| 59 |
|
| 60 |
bool ParseStandaloneScriptTag(Handle<JSLinearString*> str, |
| 61 |
mozilla::intl::ScriptSubtag& result) { |
| 62 |
// Tell the analysis the |IsStructurallyValidScriptTag| function can't GC. |
| 63 |
JS::AutoSuppressGCAnalysis nogc; |
| 64 |
|
| 65 |
if (str->hasLatin1Chars()) { |
| 66 |
if (!mozilla::intl::IsStructurallyValidScriptTag<Latin1Char>( |
| 67 |
str->latin1Range(nogc))) { |
| 68 |
return false; |
| 69 |
} |
| 70 |
result.Set<Latin1Char>(str->latin1Range(nogc)); |
| 71 |
} else { |
| 72 |
if (!mozilla::intl::IsStructurallyValidScriptTag<char16_t>( |
| 73 |
str->twoByteRange(nogc))) { |
| 74 |
return false; |
| 75 |
} |
| 76 |
result.Set<char16_t>(str->twoByteRange(nogc)); |
| 77 |
} |
| 78 |
return true; |
| 79 |
} |
| 80 |
|
| 81 |
bool ParseStandaloneRegionTag(Handle<JSLinearString*> str, |
| 82 |
mozilla::intl::RegionSubtag& result) { |
| 83 |
// Tell the analysis the |IsStructurallyValidRegionTag| function can't GC. |
| 84 |
JS::AutoSuppressGCAnalysis nogc; |
| 85 |
|
| 86 |
if (str->hasLatin1Chars()) { |
| 87 |
if (!mozilla::intl::IsStructurallyValidRegionTag<Latin1Char>( |
| 88 |
str->latin1Range(nogc))) { |
| 89 |
return false; |
| 90 |
} |
| 91 |
result.Set<Latin1Char>(str->latin1Range(nogc)); |
| 92 |
} else { |
| 93 |
if (!mozilla::intl::IsStructurallyValidRegionTag<char16_t>( |
| 94 |
str->twoByteRange(nogc))) { |
| 95 |
return false; |
| 96 |
} |
| 97 |
result.Set<char16_t>(str->twoByteRange(nogc)); |
| 98 |
} |
| 99 |
return true; |
| 100 |
} |
| 101 |
|
| 102 |
template <typename CharT> |
| 103 |
static bool IsAsciiLowercaseAlpha(mozilla::Span<const CharT> span) { |
| 104 |
// Tell the analysis the |std::all_of| function can't GC. |
| 105 |
JS::AutoSuppressGCAnalysis nogc; |
| 106 |
|
| 107 |
const CharT* ptr = span.data(); |
| 108 |
size_t length = span.size(); |
| 109 |
return std::all_of(ptr, ptr + length, mozilla::IsAsciiLowercaseAlpha<CharT>); |
| 110 |
} |
| 111 |
|
| 112 |
static bool IsAsciiLowercaseAlpha(const JSLinearString* str) { |
| 113 |
JS::AutoCheckCannotGC nogc; |
| 114 |
if (str->hasLatin1Chars()) { |
| 115 |
return IsAsciiLowercaseAlpha<Latin1Char>(str->latin1Range(nogc)); |
| 116 |
} |
| 117 |
return IsAsciiLowercaseAlpha<char16_t>(str->twoByteRange(nogc)); |
| 118 |
} |
| 119 |
|
| 120 |
template <typename CharT> |
| 121 |
static bool IsAsciiAlpha(mozilla::Span<const CharT> span) { |
| 122 |
// Tell the analysis the |std::all_of| function can't GC. |
| 123 |
JS::AutoSuppressGCAnalysis nogc; |
| 124 |
|
| 125 |
const CharT* ptr = span.data(); |
| 126 |
size_t length = span.size(); |
| 127 |
return std::all_of(ptr, ptr + length, mozilla::IsAsciiAlpha<CharT>); |
| 128 |
} |
| 129 |
|
| 130 |
static bool IsAsciiAlpha(const JSLinearString* str) { |
| 131 |
JS::AutoCheckCannotGC nogc; |
| 132 |
if (str->hasLatin1Chars()) { |
| 133 |
return IsAsciiAlpha<Latin1Char>(str->latin1Range(nogc)); |
| 134 |
} |
| 135 |
return IsAsciiAlpha<char16_t>(str->twoByteRange(nogc)); |
| 136 |
} |
| 137 |
|
| 138 |
JS::Result<JSString*> ParseStandaloneISO639LanguageTag( |
| 139 |
JSContext* cx, Handle<JSLinearString*> str) { |
| 140 |
// ISO-639 language codes contain either two or three characters. |
| 141 |
size_t length = str->length(); |
| 142 |
if (length != 2 && length != 3) { |
| 143 |
return nullptr; |
| 144 |
} |
| 145 |
|
| 146 |
// We can directly the return the input below if it's in the correct case. |
| 147 |
bool isLowerCase = IsAsciiLowercaseAlpha(str); |
| 148 |
if (!isLowerCase) { |
| 149 |
// Must be an ASCII alpha string. |
| 150 |
if (!IsAsciiAlpha(str)) { |
| 151 |
return nullptr; |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
mozilla::intl::LanguageSubtag languageTag; |
| 156 |
if (str->hasLatin1Chars()) { |
| 157 |
JS::AutoCheckCannotGC nogc; |
| 158 |
languageTag.Set<Latin1Char>(str->latin1Range(nogc)); |
| 159 |
} else { |
| 160 |
JS::AutoCheckCannotGC nogc; |
| 161 |
languageTag.Set<char16_t>(str->twoByteRange(nogc)); |
| 162 |
} |
| 163 |
|
| 164 |
if (!isLowerCase) { |
| 165 |
// The language subtag is canonicalized to lower case. |
| 166 |
languageTag.ToLowerCase(); |
| 167 |
} |
| 168 |
|
| 169 |
// Reject the input if the canonical tag contains more than just a single |
| 170 |
// language subtag. |
| 171 |
if (mozilla::intl::Locale::ComplexLanguageMapping(languageTag)) { |
| 172 |
return nullptr; |
| 173 |
} |
| 174 |
|
| 175 |
// Take care to replace deprecated subtags with their preferred values. |
| 176 |
JSString* result; |
| 177 |
if (mozilla::intl::Locale::LanguageMapping(languageTag) || !isLowerCase) { |
| 178 |
result = NewStringCopy<CanGC>(cx, languageTag.Span()); |
| 179 |
} else { |
| 180 |
result = str; |
| 181 |
} |
| 182 |
if (!result) { |
| 183 |
return cx->alreadyReportedOOM(); |
| 184 |
} |
| 185 |
return result; |
| 186 |
} |
| 187 |
|
| 188 |
void js::intl::UnicodeExtensionKeyword::trace(JSTracer* trc) { |
| 189 |
TraceRoot(trc, &type_, "UnicodeExtensionKeyword::type"); |
| 190 |
} |
| 191 |
|
| 192 |
} // namespace intl |
| 193 |
} // namespace js |