diff options
Diffstat (limited to 'js/src/builtin')
-rw-r--r-- | js/src/builtin/Intl.js | 24 |
1 files changed, 21 insertions, 3 deletions
diff --git a/js/src/builtin/Intl.js b/js/src/builtin/Intl.js index 493062c1c..509168d7a 100644 --- a/js/src/builtin/Intl.js +++ b/js/src/builtin/Intl.js @@ -432,7 +432,7 @@ function CanonicalizeLanguageTag(locale) { subtags[i] = subtag; i++; } - var normal = callFunction(std_Array_join, callFunction(std_Array_slice, subtags, 0, i), "-"); + var normal = ArrayJoinRange(subtags, "-", 0, i); // Extension sequences are sorted by their singleton characters. // "u-ca-chinese-t-zh-latn" -> "t-zh-latn-u-ca-chinese" @@ -442,7 +442,7 @@ function CanonicalizeLanguageTag(locale) { i++; while (i < subtags.length && subtags[i].length > 1) i++; - var extension = callFunction(std_Array_join, callFunction(std_Array_slice, subtags, extensionStart, i), "-"); + var extension = ArrayJoinRange(subtags, "-", extensionStart, i); callFunction(std_Array_push, extensions, extension); } callFunction(std_Array_sort, extensions); @@ -450,7 +450,7 @@ function CanonicalizeLanguageTag(locale) { // Private use sequences are left as is. "x-private" var privateUse = ""; if (i < subtags.length) - privateUse = callFunction(std_Array_join, callFunction(std_Array_slice, subtags, i), "-"); + privateUse = ArrayJoinRange(subtags, "-", i); // Put everything back together. var canonical = normal; @@ -467,6 +467,24 @@ function CanonicalizeLanguageTag(locale) { return canonical; } +/** + * Joins the array elements in the given range with the supplied separator. + */ +function ArrayJoinRange(array, separator, from, to = array.length) { + assert(typeof separator === "string", "|separator| is a string value"); + assert(typeof from === "number", "|from| is a number value"); + assert(typeof to === "number", "|to| is a number value"); + assert(0 <= from && from <= to && to <= array.length, "|from| and |to| form a valid range"); + + if (from === to) + return ""; + + var result = array[from]; + for (var i = from + 1; i < to; i++) { + result += separator + array[i]; + } + return result; +} function localeContainsNoUnicodeExtensions(locale) { // No "-u-", no possible Unicode extension. |