summaryrefslogtreecommitdiff
path: root/deps/v8/src/builtins/string-trim.tq
diff options
context:
space:
mode:
authorMichaël Zasso <targos@protonmail.com>2021-02-24 14:47:06 +0100
committerMichaël Zasso <targos@protonmail.com>2021-02-25 00:14:47 +0100
commitc5ff019a4e93891106859cb272ded1197a92c7e5 (patch)
treecaf6b7e50b0ceac09878ac4402d9f725b8685dd7 /deps/v8/src/builtins/string-trim.tq
parent67dc2bf2084b125dec43689874d237d125562cdf (diff)
downloadnode-new-c5ff019a4e93891106859cb272ded1197a92c7e5.tar.gz
deps: update V8 to 8.9.255.19
PR-URL: https://github.com/nodejs/node/pull/37330 Reviewed-By: Jiawen Geng <technicalcute@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com>
Diffstat (limited to 'deps/v8/src/builtins/string-trim.tq')
-rw-r--r--deps/v8/src/builtins/string-trim.tq115
1 files changed, 60 insertions, 55 deletions
diff --git a/deps/v8/src/builtins/string-trim.tq b/deps/v8/src/builtins/string-trim.tq
index eef0ccd84f..daaec99738 100644
--- a/deps/v8/src/builtins/string-trim.tq
+++ b/deps/v8/src/builtins/string-trim.tq
@@ -13,41 +13,41 @@ extern enum TrimMode extends uint31 constexpr 'String::TrimMode' {
}
@export
-macro IsWhiteSpaceOrLineTerminator(charCode: int32): bool {
+macro IsWhiteSpaceOrLineTerminator(charCode: char16|char8): bool {
// 0x0020 - SPACE (Intentionally out of order to fast path a commmon case)
- if (charCode == Int32Constant(0x0020)) {
+ if (charCode == 0x0020) {
return true;
}
+ // Common Non-whitespace characters from (0x000E, 0x00A0)
+ if (Unsigned(Convert<int32>(charCode) - 0x000E) < 0x0092) {
+ return false;
+ }
+
// 0x0009 - HORIZONTAL TAB
- if (charCode < Int32Constant(0x0009)) {
+ if (charCode < 0x0009) {
return false;
}
// 0x000A - LINE FEED OR NEW LINE
// 0x000B - VERTICAL TAB
// 0x000C - FORMFEED
// 0x000D - HORIZONTAL TAB
- if (charCode <= Int32Constant(0x000D)) {
+ if (charCode <= 0x000D) {
return true;
}
- // Common Non-whitespace characters
- if (charCode < Int32Constant(0x00A0)) {
- return false;
- }
-
// 0x00A0 - NO-BREAK SPACE
- if (charCode == Int32Constant(0x00A0)) {
+ if (charCode == 0x00A0) {
return true;
}
// 0x1680 - Ogham Space Mark
- if (charCode == Int32Constant(0x1680)) {
+ if (charCode == 0x1680) {
return true;
}
// 0x2000 - EN QUAD
- if (charCode < Int32Constant(0x2000)) {
+ if (charCode < 0x2000) {
return false;
}
// 0x2001 - EM QUAD
@@ -60,81 +60,91 @@ macro IsWhiteSpaceOrLineTerminator(charCode: int32): bool {
// 0x2008 - PUNCTUATION SPACE
// 0x2009 - THIN SPACE
// 0x200A - HAIR SPACE
- if (charCode <= Int32Constant(0x200A)) {
+ if (charCode <= 0x200A) {
return true;
}
// 0x2028 - LINE SEPARATOR
- if (charCode == Int32Constant(0x2028)) {
+ if (charCode == 0x2028) {
return true;
}
// 0x2029 - PARAGRAPH SEPARATOR
- if (charCode == Int32Constant(0x2029)) {
+ if (charCode == 0x2029) {
return true;
}
// 0x202F - NARROW NO-BREAK SPACE
- if (charCode == Int32Constant(0x202F)) {
+ if (charCode == 0x202F) {
return true;
}
// 0x205F - MEDIUM MATHEMATICAL SPACE
- if (charCode == Int32Constant(0x205F)) {
+ if (charCode == 0x205F) {
return true;
}
// 0xFEFF - BYTE ORDER MARK
- if (charCode == Int32Constant(0xFEFF)) {
+ if (charCode == 0xFEFF) {
return true;
}
// 0x3000 - IDEOGRAPHIC SPACE
- if (charCode == Int32Constant(0x3000)) {
+ if (charCode == 0x3000) {
return true;
}
return false;
}
-transitioning macro StringTrim(implicit context: Context)(
- receiver: JSAny, _arguments: Arguments, methodName: constexpr string,
- variant: constexpr TrimMode): String {
- const receiverString: String = ToThisString(receiver, methodName);
- const stringLength: intptr = receiverString.length_intptr;
+transitioning macro StringTrimLoop<T: type>(implicit context: Context)(
+ stringSlice: ConstSlice<T>, startIndex: intptr, endIndex: intptr,
+ increment: intptr): intptr {
+ let index = startIndex;
+ while (true) {
+ if (index == endIndex) {
+ return index;
+ }
+
+ const char: T = *stringSlice.AtIndex(index);
+ if (!IsWhiteSpaceOrLineTerminator(char)) {
+ return index;
+ }
+ index = index + increment;
+ }
+ unreachable;
+}
- const directString = Cast<DirectString>(receiverString)
- otherwise return runtime::StringTrim(
- receiverString, SmiTag<TrimMode>(variant));
+transitioning macro StringTrimBody<T: type>(implicit context: Context)(
+ string: String, slice: ConstSlice<T>, variant: constexpr TrimMode): String {
+ const stringLength: intptr = string.length_intptr;
let startIndex: intptr = 0;
let endIndex: intptr = stringLength - 1;
-
- // TODO(duongn): It would probably be more efficient to turn StringTrim into a
- // tempalate for the different string types and specialize the loop for them.
if (variant == TrimMode::kTrim || variant == TrimMode::kTrimStart) {
- while (true) {
- if (startIndex == stringLength) {
- return EmptyStringConstant();
- }
- if (!IsWhiteSpaceOrLineTerminator(
- StringCharCodeAt(directString, Unsigned(startIndex)))) {
- break;
- }
- startIndex++;
+ startIndex = StringTrimLoop(slice, startIndex, stringLength, 1);
+ if (startIndex == stringLength) {
+ return kEmptyString;
}
}
if (variant == TrimMode::kTrim || variant == TrimMode::kTrimEnd) {
- while (true) {
- if (endIndex == -1) {
- return EmptyStringConstant();
- }
- if (!IsWhiteSpaceOrLineTerminator(
- StringCharCodeAt(directString, Unsigned(endIndex)))) {
- break;
- }
- endIndex--;
+ endIndex = StringTrimLoop(slice, endIndex, -1, -1);
+ if (endIndex == -1) {
+ return kEmptyString;
}
}
- return SubString(
- receiverString, Unsigned(startIndex), Unsigned(endIndex + 1));
+ return SubString(string, Unsigned(startIndex), Unsigned(endIndex + 1));
+}
+
+transitioning macro StringTrim(implicit context: Context)(
+ receiver: JSAny, _arguments: Arguments, methodName: constexpr string,
+ variant: constexpr TrimMode): String {
+ const receiverString: String = ToThisString(receiver, methodName);
+
+ try {
+ StringToSlice(receiverString) otherwise OneByte, TwoByte;
+ } label OneByte(slice: ConstSlice<char8>) {
+ return StringTrimBody(receiverString, slice, variant);
+ } label TwoByte(slice: ConstSlice<char16>) {
+ return StringTrimBody(receiverString, slice, variant);
+ }
}
// ES6 #sec-string.prototype.trim
@@ -161,8 +171,3 @@ StringPrototypeTrimEnd(
return StringTrim(receiver, arguments, methodName, TrimMode::kTrimEnd);
}
}
-
-namespace runtime {
-extern runtime StringTrim(implicit context: Context)(
- String, SmiTagged<string::TrimMode>): String;
-}