Source code

Revision control

Other Tools

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
#ifndef builtin_intl_NumberFormat_h
8
#define builtin_intl_NumberFormat_h
9
10
#include "mozilla/Attributes.h"
11
12
#include <stdint.h>
13
14
#include "builtin/SelfHostingDefines.h"
15
#include "gc/Barrier.h"
16
#include "js/Class.h"
17
#include "vm/NativeObject.h"
18
#include "vm/Runtime.h"
19
20
struct UFormattedNumber;
21
struct UFormattedValue;
22
struct UNumberFormatter;
23
24
namespace js {
25
26
class NumberFormatObject : public NativeObject {
27
public:
28
static const JSClass class_;
29
static const JSClass& protoClass_;
30
31
static constexpr uint32_t INTERNALS_SLOT = 0;
32
static constexpr uint32_t UNUMBER_FORMATTER_SLOT = 1;
33
static constexpr uint32_t UFORMATTED_NUMBER_SLOT = 2;
34
static constexpr uint32_t SLOT_COUNT = 3;
35
36
static_assert(INTERNALS_SLOT == INTL_INTERNALS_OBJECT_SLOT,
37
"INTERNALS_SLOT must match self-hosting define for internals "
38
"object slot");
39
40
UNumberFormatter* getNumberFormatter() const {
41
const auto& slot = getFixedSlot(UNUMBER_FORMATTER_SLOT);
42
if (slot.isUndefined()) {
43
return nullptr;
44
}
45
return static_cast<UNumberFormatter*>(slot.toPrivate());
46
}
47
48
void setNumberFormatter(UNumberFormatter* formatter) {
49
setFixedSlot(UNUMBER_FORMATTER_SLOT, PrivateValue(formatter));
50
}
51
52
UFormattedNumber* getFormattedNumber() const {
53
const auto& slot = getFixedSlot(UFORMATTED_NUMBER_SLOT);
54
if (slot.isUndefined()) {
55
return nullptr;
56
}
57
return static_cast<UFormattedNumber*>(slot.toPrivate());
58
}
59
60
void setFormattedNumber(UFormattedNumber* formatted) {
61
setFixedSlot(UFORMATTED_NUMBER_SLOT, PrivateValue(formatted));
62
}
63
64
private:
65
static const JSClassOps classOps_;
66
static const ClassSpec classSpec_;
67
68
static void finalize(JSFreeOp* fop, JSObject* obj);
69
};
70
71
/**
72
* Returns a new instance of the standard built-in NumberFormat constructor.
73
* Self-hosted code cannot cache this constructor (as it does for others in
74
* Utilities.js) because it is initialized after self-hosted code is compiled.
75
*
76
* Usage: numberFormat = intl_NumberFormat(locales, options)
77
*/
78
extern MOZ_MUST_USE bool intl_NumberFormat(JSContext* cx, unsigned argc,
79
Value* vp);
80
81
/**
82
* Returns the numbering system type identifier per Unicode
83
* Technical Standard 35, Unicode Locale Data Markup Language, for the
84
* default numbering system for the given locale.
85
*
86
* Usage: defaultNumberingSystem = intl_numberingSystem(locale)
87
*/
88
extern MOZ_MUST_USE bool intl_numberingSystem(JSContext* cx, unsigned argc,
89
Value* vp);
90
91
/**
92
* Returns a string representing the number x according to the effective
93
* locale and the formatting options of the given NumberFormat.
94
*
95
* Spec: ECMAScript Internationalization API Specification, 11.3.2.
96
*
97
* Usage: formatted = intl_FormatNumber(numberFormat, x, formatToParts)
98
*/
99
extern MOZ_MUST_USE bool intl_FormatNumber(JSContext* cx, unsigned argc,
100
Value* vp);
101
102
#if DEBUG || MOZ_SYSTEM_ICU
103
/**
104
* Returns an object with all available measurement units.
105
*
106
* Usage: units = intl_availableMeasurementUnits()
107
*/
108
extern MOZ_MUST_USE bool intl_availableMeasurementUnits(JSContext* cx,
109
unsigned argc,
110
Value* vp);
111
#endif
112
113
namespace intl {
114
115
/**
116
* Class to create a number formatter skeleton.
117
*
118
* The skeleton syntax is documented at:
120
*/
121
class MOZ_STACK_CLASS NumberFormatterSkeleton final {
122
static constexpr size_t DefaultVectorSize = 128;
123
using SkeletonVector = Vector<char16_t, DefaultVectorSize>;
124
125
SkeletonVector vector_;
126
127
bool append(char16_t c) { return vector_.append(c); }
128
129
bool appendN(char16_t c, size_t times) { return vector_.appendN(c, times); }
130
131
template <size_t N>
132
bool append(const char16_t (&chars)[N]) {
133
static_assert(N > 0,
134
"should only be used with string literals or properly "
135
"null-terminated arrays");
136
MOZ_ASSERT(chars[N - 1] == '\0',
137
"should only be used with string literals or properly "
138
"null-terminated arrays");
139
return vector_.append(chars, N - 1); // Without trailing \0.
140
}
141
142
template <size_t N>
143
bool appendToken(const char16_t (&token)[N]) {
144
return append(token) && append(' ');
145
}
146
147
bool append(const char* chars, size_t length) {
148
return vector_.append(chars, length);
149
}
150
151
public:
152
explicit NumberFormatterSkeleton(JSContext* cx) : vector_(cx) {}
153
154
/**
155
* Return a new UNumberFormatter based on this skeleton.
156
*/
157
UNumberFormatter* toFormatter(JSContext* cx, const char* locale);
158
159
/**
160
* Set this skeleton to display a currency amount. |currency| must be a
161
* three-letter currency code.
162
*
164
*/
165
MOZ_MUST_USE bool currency(JSLinearString* currency);
166
167
enum class CurrencyDisplay { Code, Name, Symbol, NarrowSymbol };
168
169
/**
170
* Set the currency display style for this skeleton.
171
*
173
*/
174
MOZ_MUST_USE bool currencyDisplay(CurrencyDisplay display);
175
176
/**
177
* Set this skeleton to display a unit amount. |unit| must be a well-formed
178
* unit identifier.
179
*
182
*/
183
MOZ_MUST_USE bool unit(JSLinearString* unit);
184
185
enum class UnitDisplay { Short, Narrow, Long };
186
187
/**
188
* Set the unit display style for this skeleton.
189
*
191
*/
192
MOZ_MUST_USE bool unitDisplay(UnitDisplay display);
193
194
/**
195
* Set this skeleton to display a percent number.
196
*
199
*/
200
MOZ_MUST_USE bool percent();
201
202
/**
203
* Set the fraction digits settings for this skeleton. |min| can be zero,
204
* |max| must be larger-or-equal to |min|.
205
*
207
*/
208
MOZ_MUST_USE bool fractionDigits(uint32_t min, uint32_t max);
209
210
/**
211
* Set the integer-width settings for this skeleton. |min| must be a non-zero
212
* number.
213
*
215
*/
216
MOZ_MUST_USE bool integerWidth(uint32_t min);
217
218
/**
219
* Set the significant digits settings for this skeleton. |min| must be a
220
* non-zero number, |max| must be larger-or-equal to |min|.
221
*
223
*/
224
MOZ_MUST_USE bool significantDigits(uint32_t min, uint32_t max);
225
226
/**
227
* Enable or disable grouping for this skeleton.
228
*
230
*/
231
MOZ_MUST_USE bool useGrouping(bool on);
232
233
enum class Notation {
234
Standard,
235
Scientific,
236
Engineering,
237
CompactShort,
238
CompactLong
239
};
240
241
/**
242
* Set the notation style for this skeleton.
243
*
245
*/
246
MOZ_MUST_USE bool notation(Notation style);
247
248
enum class SignDisplay {
249
Auto,
250
Never,
251
Always,
252
ExceptZero,
253
Accounting,
254
AccountingAlways,
255
AccountingExceptZero
256
};
257
258
/**
259
* Set the sign-display for this skeleton.
260
*
262
*/
263
MOZ_MUST_USE bool signDisplay(SignDisplay display);
264
265
/**
266
* Set the rounding mode to 'half-up' for this skeleton.
267
*
269
*/
270
MOZ_MUST_USE bool roundingModeHalfUp();
271
};
272
273
using FieldType = js::ImmutablePropertyNamePtr JSAtomState::*;
274
275
#ifndef U_HIDE_DRAFT_API
276
MOZ_MUST_USE bool FormattedNumberToParts(JSContext* cx,
277
const UFormattedValue* formattedValue,
278
HandleValue number, FieldType unitType,
279
MutableHandleValue result);
280
#endif
281
282
} // namespace intl
283
} // namespace js
284
285
#endif /* builtin_intl_NumberFormat_h */