From a4dfffdacf09426a035a3e2d7d75eb5805d77535 Mon Sep 17 00:00:00 2001 From: Florian Schroedl Date: Thu, 20 Jan 2022 17:00:00 +0100 Subject: [PATCH] Fix throw when passing -1 from inital parser --- src/utils/str.nim | 13 ++++++++----- tests/utils/test_str.nim | 1 + 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/src/utils/str.nim b/src/utils/str.nim index 794778e..67db94d 100644 --- a/src/utils/str.nim +++ b/src/utils/str.nim @@ -19,11 +19,14 @@ proc safeDelete*(str: string, slice: Slice[int]): string = func findAndDelete*(str: string, chars: set[char], start = 0, last = str.len - 1): string = ## Find the next instance of `chars` from `start`. ## When found delete characters until `last`. - let startChar = str.find(chars, start, last) - if startChar == -1: - str - else: - str.safeDelete(startChar..last) + # Prevent passing negative numbers (e.g.: initial parser) + if start >= 0 and last >= 0: + let startChar = str.find(chars, start, last) + if startChar == -1: + str + else: + str.safeDelete(startChar..last) + else: str func deleteAfterNewline*(str: string, start = 0): string = ## Delete string after next Newline from `start`. diff --git a/tests/utils/test_str.nim b/tests/utils/test_str.nim index b3d66ed..3b13a43 100644 --- a/tests/utils/test_str.nim +++ b/tests/utils/test_str.nim @@ -20,3 +20,4 @@ suite "utils/str": let t = "foo\nbar" check(t.deleteAfterNewline() == "foo") check(t.deleteAfterNewline(start = 4) == t) + check(t.deleteAfterNewline(start = -1) == t)