1// Copyright 2019 the V8 project authors. All rights reserved. 2// Use of this source code is governed by a BSD-style license that can be 3// found in the LICENSE file. 4 5namespace string { 6 7// String.prototype.substr ( start, length ) 8// ES6 #sec-string.prototype.substr 9transitioning javascript builtin StringPrototypeSubstr( 10 js-implicit context: NativeContext, receiver: JSAny)(...arguments): String { 11 const methodName: constexpr string = 'String.prototype.substr'; 12 // 1. Let O be ? RequireObjectCoercible(this value). 13 // 2. Let S be ? ToString(O). 14 const string: String = ToThisString(receiver, methodName); 15 16 // 5. Let size be the number of code units in S. 17 const size: uintptr = string.length_uintptr; 18 19 // 3. Let intStart be ? ToInteger(start). 20 // 6. If intStart < 0, set intStart to max(size + intStart, 0). 21 const start = arguments[0]; 22 const initStart: uintptr = 23 start != Undefined ? ConvertToRelativeIndex(start, size) : 0; 24 25 // 4. If length is undefined, 26 // let end be +∞; otherwise let end be ? ToInteger(length). 27 // 7. Let resultLength be min(max(end, 0), size - intStart). 28 const length = arguments[1]; 29 const lengthLimit = size - initStart; 30 dcheck(lengthLimit <= size); 31 const resultLength: uintptr = length != Undefined ? 32 ClampToIndexRange(length, lengthLimit) : 33 lengthLimit; 34 35 // 8. If resultLength ≤ 0, return the empty String "". 36 if (resultLength == 0) return EmptyStringConstant(); 37 38 // 9. Return the String value containing resultLength consecutive code units 39 // from S beginning with the code unit at index intStart. 40 return SubString(string, initStart, initStart + resultLength); 41} 42} 43