summaryrefslogtreecommitdiffstats
path: root/js/src/builtin/intl/LanguageTag.h
blob: e896411e1959036c18cd44f228d285fd16c9c409 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
 * vim: set ts=8 sts=2 et sw=2 tw=80:
 * 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/. */

/* Structured representation of Unicode locale IDs used with Intl functions. */

#ifndef builtin_intl_LanguageTag_h
#define builtin_intl_LanguageTag_h

#include "mozilla/intl/Locale.h"
#include "mozilla/Span.h"

#include "js/Result.h"
#include "js/RootingAPI.h"

struct JS_PUBLIC_API JSContext;
class JSLinearString;
class JS_PUBLIC_API JSString;
class JS_PUBLIC_API JSTracer;

namespace js {

namespace intl {

/**
 * Parse a string Unicode BCP 47 locale identifier. If successful, store in
 * |result| and return true. Otherwise return false.
 */
[[nodiscard]] bool ParseLocale(JSContext* cx, JS::Handle<JSLinearString*> str,
                               mozilla::intl::Locale& result);

/**
 * Parse a string as a standalone |language| tag. If |str| is a standalone
 * language tag, store it in |result| and return true. Otherwise return false.
 */
[[nodiscard]] bool ParseStandaloneLanguageTag(
    JS::Handle<JSLinearString*> str, mozilla::intl::LanguageSubtag& result);

/**
 * Parse a string as a standalone |script| tag. If |str| is a standalone script
 * tag, store it in |result| and return true. Otherwise return false.
 */
[[nodiscard]] bool ParseStandaloneScriptTag(
    JS::Handle<JSLinearString*> str, mozilla::intl::ScriptSubtag& result);

/**
 * Parse a string as a standalone |region| tag. If |str| is a standalone region
 * tag, store it in |result| and return true. Otherwise return false.
 */
[[nodiscard]] bool ParseStandaloneRegionTag(
    JS::Handle<JSLinearString*> str, mozilla::intl::RegionSubtag& result);

/**
 * Parse a string as an ISO-639 language code. Return |nullptr| in the result if
 * the input could not be parsed or the canonical form of the resulting language
 * tag contains more than a single language subtag.
 */
JS::Result<JSString*> ParseStandaloneISO639LanguageTag(
    JSContext* cx, JS::Handle<JSLinearString*> str);

class UnicodeExtensionKeyword final {
  char key_[mozilla::intl::LanguageTagLimits::UnicodeKeyLength];
  JSLinearString* type_;

 public:
  using UnicodeKey =
      const char (&)[mozilla::intl::LanguageTagLimits::UnicodeKeyLength + 1];
  using UnicodeKeySpan =
      mozilla::Span<const char,
                    mozilla::intl::LanguageTagLimits::UnicodeKeyLength>;

  UnicodeExtensionKeyword(UnicodeKey key, JSLinearString* type)
      : key_{key[0], key[1]}, type_(type) {}

  UnicodeKeySpan key() const { return {key_, sizeof(key_)}; }
  JSLinearString* type() const { return type_; }

  void trace(JSTracer* trc);
};

[[nodiscard]] extern bool ApplyUnicodeExtensionToTag(
    JSContext* cx, mozilla::intl::Locale& tag,
    JS::HandleVector<UnicodeExtensionKeyword> keywords);

}  // namespace intl

}  // namespace js

#endif /* builtin_intl_LanguageTag_h */