| 1 |
/* This Source Code Form is subject to the terms of the Mozilla Public |
| 2 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 4 |
|
| 5 |
/** |
| 6 |
* Intl.Segmenter internal properties. |
| 7 |
*/ |
| 8 |
function segmenterLocaleData() { |
| 9 |
// Segmenter doesn't support any extension keys. |
| 10 |
return {}; |
| 11 |
} |
| 12 |
var segmenterInternalProperties = { |
| 13 |
localeData: segmenterLocaleData, |
| 14 |
relevantExtensionKeys: [], |
| 15 |
}; |
| 16 |
|
| 17 |
/** |
| 18 |
* Intl.Segmenter ( [ locales [ , options ] ] ) |
| 19 |
* |
| 20 |
* Compute an internal properties object from |lazySegmenterData|. |
| 21 |
*/ |
| 22 |
function resolveSegmenterInternals(lazySegmenterData) { |
| 23 |
assert(IsObject(lazySegmenterData), "lazy data not an object?"); |
| 24 |
|
| 25 |
var internalProps = std_Object_create(null); |
| 26 |
|
| 27 |
var Segmenter = segmenterInternalProperties; |
| 28 |
|
| 29 |
// Compute effective locale. |
| 30 |
|
| 31 |
// Step 9. |
| 32 |
var localeData = Segmenter.localeData; |
| 33 |
|
| 34 |
// Step 10. |
| 35 |
var r = ResolveLocale( |
| 36 |
"Segmenter", |
| 37 |
lazySegmenterData.requestedLocales, |
| 38 |
lazySegmenterData.opt, |
| 39 |
Segmenter.relevantExtensionKeys, |
| 40 |
localeData |
| 41 |
); |
| 42 |
|
| 43 |
// Step 11. |
| 44 |
internalProps.locale = r.locale; |
| 45 |
|
| 46 |
// Step 13. |
| 47 |
internalProps.granularity = lazySegmenterData.granularity; |
| 48 |
|
| 49 |
// The caller is responsible for associating |internalProps| with the right |
| 50 |
// object using |setInternalProperties|. |
| 51 |
return internalProps; |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* Returns an object containing the Segmenter internal properties of |obj|. |
| 56 |
*/ |
| 57 |
function getSegmenterInternals(obj) { |
| 58 |
assert(IsObject(obj), "getSegmenterInternals called with non-object"); |
| 59 |
assert( |
| 60 |
intl_GuardToSegmenter(obj) !== null, |
| 61 |
"getSegmenterInternals called with non-Segmenter" |
| 62 |
); |
| 63 |
|
| 64 |
var internals = getIntlObjectInternals(obj); |
| 65 |
assert( |
| 66 |
internals.type === "Segmenter", |
| 67 |
"bad type escaped getIntlObjectInternals" |
| 68 |
); |
| 69 |
|
| 70 |
// If internal properties have already been computed, use them. |
| 71 |
var internalProps = maybeInternalProperties(internals); |
| 72 |
if (internalProps) { |
| 73 |
return internalProps; |
| 74 |
} |
| 75 |
|
| 76 |
// Otherwise it's time to fully create them. |
| 77 |
internalProps = resolveSegmenterInternals(internals.lazyData); |
| 78 |
setInternalProperties(internals, internalProps); |
| 79 |
return internalProps; |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* Intl.Segmenter ( [ locales [ , options ] ] ) |
| 84 |
* |
| 85 |
* Initializes an object as a Segmenter. |
| 86 |
* |
| 87 |
* This method is complicated a moderate bit by its implementing initialization |
| 88 |
* as a *lazy* concept. Everything that must happen now, does -- but we defer |
| 89 |
* all the work we can until the object is actually used as a Segmenter. |
| 90 |
* This later work occurs in |resolveSegmenterInternals|; steps not noted here |
| 91 |
* occur there. |
| 92 |
*/ |
| 93 |
function InitializeSegmenter(segmenter, locales, options) { |
| 94 |
assert(IsObject(segmenter), "InitializeSegmenter called with non-object"); |
| 95 |
assert( |
| 96 |
intl_GuardToSegmenter(segmenter) !== null, |
| 97 |
"InitializeSegmenter called with non-Segmenter" |
| 98 |
); |
| 99 |
|
| 100 |
// Lazy Segmenter data has the following structure: |
| 101 |
// |
| 102 |
// { |
| 103 |
// requestedLocales: List of locales, |
| 104 |
// |
| 105 |
// opt: // opt object computed in InitializeSegmenter |
| 106 |
// { |
| 107 |
// localeMatcher: "lookup" / "best fit", |
| 108 |
// } |
| 109 |
// |
| 110 |
// granularity: "grapheme" / "word" / "sentence", |
| 111 |
// } |
| 112 |
// |
| 113 |
// Note that lazy data is only installed as a final step of initialization, |
| 114 |
// so every Segmenter lazy data object has *all* these properties, never a |
| 115 |
// subset of them. |
| 116 |
var lazySegmenterData = std_Object_create(null); |
| 117 |
|
| 118 |
// Step 4. |
| 119 |
var requestedLocales = CanonicalizeLocaleList(locales); |
| 120 |
lazySegmenterData.requestedLocales = requestedLocales; |
| 121 |
|
| 122 |
// Step 5. |
| 123 |
if (options === undefined) { |
| 124 |
options = std_Object_create(null); |
| 125 |
} else if (!IsObject(options)) { |
| 126 |
ThrowTypeError( |
| 127 |
JSMSG_OBJECT_REQUIRED, |
| 128 |
options === null ? "null" : typeof options |
| 129 |
); |
| 130 |
} |
| 131 |
|
| 132 |
// Step 6. |
| 133 |
var opt = new_Record(); |
| 134 |
lazySegmenterData.opt = opt; |
| 135 |
|
| 136 |
// Steps 7-8. |
| 137 |
var matcher = GetOption( |
| 138 |
options, |
| 139 |
"localeMatcher", |
| 140 |
"string", |
| 141 |
["lookup", "best fit"], |
| 142 |
"best fit" |
| 143 |
); |
| 144 |
opt.localeMatcher = matcher; |
| 145 |
|
| 146 |
// Steps 12-13. |
| 147 |
var granularity = GetOption( |
| 148 |
options, |
| 149 |
"granularity", |
| 150 |
"string", |
| 151 |
["grapheme", "word", "sentence"], |
| 152 |
"grapheme" |
| 153 |
); |
| 154 |
lazySegmenterData.granularity = granularity; |
| 155 |
|
| 156 |
// We've done everything that must be done now: mark the lazy data as fully |
| 157 |
// computed and install it. |
| 158 |
initializeIntlObject(segmenter, "Segmenter", lazySegmenterData); |
| 159 |
} |
| 160 |
|
| 161 |
/** |
| 162 |
* Intl.Segmenter.supportedLocalesOf ( locales [, options ]) |
| 163 |
* |
| 164 |
* Returns the subset of the given locale list for which this locale list has a |
| 165 |
* matching (possibly fallback) locale. Locales appear in the same order in the |
| 166 |
* returned list as in the input list. |
| 167 |
*/ |
| 168 |
function Intl_Segmenter_supportedLocalesOf(locales /*, options*/) { |
| 169 |
var options = ArgumentsLength() > 1 ? GetArgument(1) : undefined; |
| 170 |
|
| 171 |
// Step 1. |
| 172 |
var availableLocales = "Segmenter"; |
| 173 |
|
| 174 |
// Step 2. |
| 175 |
var requestedLocales = CanonicalizeLocaleList(locales); |
| 176 |
|
| 177 |
// Step 3. |
| 178 |
return SupportedLocales(availableLocales, requestedLocales, options); |
| 179 |
} |
| 180 |
|
| 181 |
/** |
| 182 |
* Intl.Segmenter.prototype.segment ( string ) |
| 183 |
* |
| 184 |
* Create a new Segments object. |
| 185 |
*/ |
| 186 |
function Intl_Segmenter_segment(value) { |
| 187 |
// Step 1. |
| 188 |
var segmenter = this; |
| 189 |
|
| 190 |
// Step 2. |
| 191 |
if ( |
| 192 |
!IsObject(segmenter) || |
| 193 |
(segmenter = intl_GuardToSegmenter(segmenter)) === null |
| 194 |
) { |
| 195 |
return callFunction( |
| 196 |
intl_CallSegmenterMethodIfWrapped, |
| 197 |
this, |
| 198 |
value, |
| 199 |
"Intl_Segmenter_segment" |
| 200 |
); |
| 201 |
} |
| 202 |
|
| 203 |
// Ensure the Segmenter internals are resolved. |
| 204 |
getSegmenterInternals(segmenter); |
| 205 |
|
| 206 |
// Step 3. |
| 207 |
var string = ToString(value); |
| 208 |
|
| 209 |
// Step 4. |
| 210 |
return intl_CreateSegmentsObject(segmenter, string); |
| 211 |
} |
| 212 |
|
| 213 |
/** |
| 214 |
* Intl.Segmenter.prototype.resolvedOptions () |
| 215 |
* |
| 216 |
* Returns the resolved options for a Segmenter object. |
| 217 |
*/ |
| 218 |
function Intl_Segmenter_resolvedOptions() { |
| 219 |
// Step 1. |
| 220 |
var segmenter = this; |
| 221 |
|
| 222 |
// Step 2. |
| 223 |
if ( |
| 224 |
!IsObject(segmenter) || |
| 225 |
(segmenter = intl_GuardToSegmenter(segmenter)) === null |
| 226 |
) { |
| 227 |
return callFunction( |
| 228 |
intl_CallSegmenterMethodIfWrapped, |
| 229 |
this, |
| 230 |
"Intl_Segmenter_resolvedOptions" |
| 231 |
); |
| 232 |
} |
| 233 |
|
| 234 |
var internals = getSegmenterInternals(segmenter); |
| 235 |
|
| 236 |
// Steps 3-4. |
| 237 |
var options = { |
| 238 |
locale: internals.locale, |
| 239 |
granularity: internals.granularity, |
| 240 |
}; |
| 241 |
|
| 242 |
// Step 5. |
| 243 |
return options; |
| 244 |
} |
| 245 |
|
| 246 |
/** |
| 247 |
* CreateSegmentDataObject ( segmenter, string, startIndex, endIndex ) |
| 248 |
*/ |
| 249 |
function CreateSegmentDataObject(string, boundaries) { |
| 250 |
assert(typeof string === "string", "CreateSegmentDataObject"); |
| 251 |
assert( |
| 252 |
IsPackedArray(boundaries) && boundaries.length === 3, |
| 253 |
"CreateSegmentDataObject" |
| 254 |
); |
| 255 |
|
| 256 |
var startIndex = boundaries[0]; |
| 257 |
assert( |
| 258 |
typeof startIndex === "number" && (startIndex | 0) === startIndex, |
| 259 |
"startIndex is an int32-value" |
| 260 |
); |
| 261 |
|
| 262 |
var endIndex = boundaries[1]; |
| 263 |
assert( |
| 264 |
typeof endIndex === "number" && (endIndex | 0) === endIndex, |
| 265 |
"endIndex is an int32-value" |
| 266 |
); |
| 267 |
|
| 268 |
// In our implementation |granularity| is encoded in |isWordLike|. |
| 269 |
var isWordLike = boundaries[2]; |
| 270 |
assert( |
| 271 |
typeof isWordLike === "boolean" || isWordLike === undefined, |
| 272 |
"isWordLike is either a boolean or undefined" |
| 273 |
); |
| 274 |
|
| 275 |
// Step 1 (Not applicable). |
| 276 |
|
| 277 |
// Step 2. |
| 278 |
assert(startIndex >= 0, "startIndex is a positive number"); |
| 279 |
|
| 280 |
// Step 3. |
| 281 |
assert( |
| 282 |
endIndex <= string.length, |
| 283 |
"endIndex is less-than-equals the string length" |
| 284 |
); |
| 285 |
|
| 286 |
// Step 4. |
| 287 |
assert(startIndex < endIndex, "startIndex is strictly less than endIndex"); |
| 288 |
|
| 289 |
// Step 6. |
| 290 |
var segment = Substring(string, startIndex, endIndex - startIndex); |
| 291 |
|
| 292 |
// Steps 5, 7-12. |
| 293 |
if (isWordLike === undefined) { |
| 294 |
return { |
| 295 |
segment, |
| 296 |
index: startIndex, |
| 297 |
input: string, |
| 298 |
}; |
| 299 |
} |
| 300 |
|
| 301 |
return { |
| 302 |
segment, |
| 303 |
index: startIndex, |
| 304 |
input: string, |
| 305 |
isWordLike, |
| 306 |
}; |
| 307 |
} |
| 308 |
|
| 309 |
/** |
| 310 |
* %Segments.prototype%.containing ( index ) |
| 311 |
* |
| 312 |
* Return a Segment Data object describing the segment at the given index. If |
| 313 |
* the index exceeds the string bounds, undefined is returned. |
| 314 |
*/ |
| 315 |
function Intl_Segments_containing(index) { |
| 316 |
// Step 1. |
| 317 |
var segments = this; |
| 318 |
|
| 319 |
// Step 2. |
| 320 |
if ( |
| 321 |
!IsObject(segments) || |
| 322 |
(segments = intl_GuardToSegments(segments)) === null |
| 323 |
) { |
| 324 |
return callFunction( |
| 325 |
intl_CallSegmentsMethodIfWrapped, |
| 326 |
this, |
| 327 |
index, |
| 328 |
"Intl_Segments_containing" |
| 329 |
); |
| 330 |
} |
| 331 |
|
| 332 |
// Step 3 (not applicable). |
| 333 |
|
| 334 |
// Step 4. |
| 335 |
var string = UnsafeGetStringFromReservedSlot( |
| 336 |
segments, |
| 337 |
INTL_SEGMENTS_STRING_SLOT |
| 338 |
); |
| 339 |
|
| 340 |
// Step 5. |
| 341 |
var len = string.length; |
| 342 |
|
| 343 |
// Step 6. |
| 344 |
var n = ToInteger(index); |
| 345 |
|
| 346 |
// Step 7. |
| 347 |
if (n < 0 || n >= len) { |
| 348 |
return undefined; |
| 349 |
} |
| 350 |
|
| 351 |
// Steps 8-9. |
| 352 |
var boundaries = intl_FindSegmentBoundaries(segments, n | 0); |
| 353 |
|
| 354 |
// Step 10. |
| 355 |
return CreateSegmentDataObject(string, boundaries); |
| 356 |
} |
| 357 |
|
| 358 |
/** |
| 359 |
* %Segments.prototype% [ @@iterator ] () |
| 360 |
* |
| 361 |
* Create a new Segment Iterator object. |
| 362 |
*/ |
| 363 |
function Intl_Segments_iterator() { |
| 364 |
// Step 1. |
| 365 |
var segments = this; |
| 366 |
|
| 367 |
// Step 2. |
| 368 |
if ( |
| 369 |
!IsObject(segments) || |
| 370 |
(segments = intl_GuardToSegments(segments)) === null |
| 371 |
) { |
| 372 |
return callFunction( |
| 373 |
intl_CallSegmentsMethodIfWrapped, |
| 374 |
this, |
| 375 |
"Intl_Segments_iterator" |
| 376 |
); |
| 377 |
} |
| 378 |
|
| 379 |
// Steps 3-5. |
| 380 |
return intl_CreateSegmentIterator(segments); |
| 381 |
} |
| 382 |
|
| 383 |
/** |
| 384 |
* %SegmentIterator.prototype%.next () |
| 385 |
* |
| 386 |
* Advance the Segment iterator to the next segment within the string. |
| 387 |
*/ |
| 388 |
function Intl_SegmentIterator_next() { |
| 389 |
// Step 1. |
| 390 |
var iterator = this; |
| 391 |
|
| 392 |
// Step 2. |
| 393 |
if ( |
| 394 |
!IsObject(iterator) || |
| 395 |
(iterator = intl_GuardToSegmentIterator(iterator)) === null) |
| 396 |
{ |
| 397 |
return callFunction( |
| 398 |
intl_CallSegmentIteratorMethodIfWrapped, |
| 399 |
this, |
| 400 |
"Intl_SegmentIterator_next" |
| 401 |
); |
| 402 |
} |
| 403 |
|
| 404 |
// Step 3 (Not applicable). |
| 405 |
|
| 406 |
// Step 4. |
| 407 |
var string = UnsafeGetStringFromReservedSlot( |
| 408 |
iterator, |
| 409 |
INTL_SEGMENT_ITERATOR_STRING_SLOT |
| 410 |
); |
| 411 |
|
| 412 |
// Step 5. |
| 413 |
var index = UnsafeGetInt32FromReservedSlot( |
| 414 |
iterator, |
| 415 |
INTL_SEGMENT_ITERATOR_INDEX_SLOT |
| 416 |
); |
| 417 |
|
| 418 |
var result = { value: undefined, done: false }; |
| 419 |
|
| 420 |
// Step 7. |
| 421 |
if (index === string.length) { |
| 422 |
result.done = true; |
| 423 |
return result; |
| 424 |
} |
| 425 |
|
| 426 |
// Steps 6, 8. |
| 427 |
var boundaries = intl_FindNextSegmentBoundaries(iterator); |
| 428 |
|
| 429 |
// Step 9. |
| 430 |
result.value = CreateSegmentDataObject(string, boundaries); |
| 431 |
|
| 432 |
// Step 10. |
| 433 |
return result; |
| 434 |
} |