string-endswith.tq 2.76 KB
Newer Older
1 2 3 4 5
// Copyright 2019 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

namespace string {
6 7 8 9 10
macro TryFastStringCompareSequence(
    string: String, searchStr: String, start: uintptr,
    searchLength: uintptr): Boolean labels Slow {
  const directString = Cast<DirectString>(string) otherwise Slow;
  const directSearchStr = Cast<DirectString>(searchStr) otherwise Slow;
11

12 13
  let searchIndex: uintptr = 0;
  let stringIndex: uintptr = start;
14

15 16 17 18
  while (searchIndex < searchLength) {
    if (StringCharCodeAt(directSearchStr, searchIndex) !=
        StringCharCodeAt(directString, stringIndex)) {
      return False;
19
    }
20 21 22

    searchIndex++;
    stringIndex++;
23
  }
24 25
  return True;
}
26

27 28 29 30 31 32 33
// https://tc39.github.io/ecma262/#sec-string.prototype.endswith
transitioning javascript builtin StringPrototypeEndsWith(
    js-implicit context: NativeContext,
    receiver: JSAny)(...arguments): Boolean {
  const searchString: JSAny = arguments[0];
  const endPosition: JSAny = arguments[1];
  const kBuiltinName: constexpr string = 'String.prototype.endsWith';
34

35 36 37
  // 1. Let O be ? RequireObjectCoercible(this value).
  // 2. Let S be ? ToString(O).
  const string: String = ToThisString(receiver, kBuiltinName);
38

39 40 41 42 43
  // 3. Let isRegExp be ? IsRegExp(searchString).
  // 4. If isRegExp is true, throw a TypeError exception.
  if (regexp::IsRegExp(searchString)) {
    ThrowTypeError(MessageTemplate::kFirstArgumentNotRegExp, kBuiltinName);
  }
44

45 46
  // 5. Let searchStr be ? ToString(searchString).
  const searchStr: String = ToString_Inline(searchString);
47

48 49
  // 6. Let len be the length of S.
  const len: uintptr = string.length_uintptr;
50

51 52 53 54 55
  // 7. If endPosition is undefined, let pos be len,
  // else let pos be ? ToInteger(endPosition).
  // 8. Let end be min(max(pos, 0), len).
  const end: uintptr =
      (endPosition != Undefined) ? ClampToIndexRange(endPosition, len) : len;
56

57 58
  // 9. Let searchLength be the length of searchStr.
  const searchLength: uintptr = searchStr.length_uintptr;
59

60 61
  // 10. Let start be end - searchLength.
  const start: uintptr = end - searchLength;
62

63 64
  // 11. If start is less than 0, return false.
  if (Signed(start) < 0) return False;
65

66 67 68 69 70 71 72 73 74 75 76 77
  // 12. If the sequence of code units of S starting at start of length
  // searchLength is the same as the full code unit sequence of searchStr,
  // return true.
  // 13. Otherwise, return false.
  try {
    // Fast Path: If both strings are direct and relevant indices are Smis.
    return TryFastStringCompareSequence(string, searchStr, start, searchLength)
        otherwise Slow;
  } label Slow {
    // Slow Path: If either of the string is indirect, bail into runtime.
    return StringCompareSequence(
        context, string, searchStr, Convert<Number>(start));
78 79
  }
}
80
}