Skip to content

Commit

Permalink
fix: substring() can handle exceeding length
Browse files Browse the repository at this point in the history
If substring() is called with a given length that is greater than the string then it returns the remaining characters from the start index.
  • Loading branch information
saig0 committed Aug 22, 2024
1 parent 9af6832 commit 1f7478b
Showing 1 changed file with 4 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,11 @@ object StringBuiltinFunctions {
private def substringFunction3 = builtinFunction(
params = List("string", "start position", "length"),
invoke = { case List(ValString(string), ValNumber(start), ValNumber(length)) =>
val startIndex = stringIndex(string, start.intValue)
val endIndex = Math.min(startIndex + length.intValue, string.length)

ValString(
string.substring(
stringIndex(string, start.intValue),
stringIndex(string, start.intValue) + length.intValue
)
string.substring(startIndex, endIndex)
)
}
)
Expand Down

0 comments on commit 1f7478b

Please sign in to comment.