Fix throw when passing -1 from inital parser
This commit is contained in:
@@ -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 =
|
func findAndDelete*(str: string, chars: set[char], start = 0, last = str.len - 1): string =
|
||||||
## Find the next instance of `chars` from `start`.
|
## Find the next instance of `chars` from `start`.
|
||||||
## When found delete characters until `last`.
|
## When found delete characters until `last`.
|
||||||
let startChar = str.find(chars, start, last)
|
# Prevent passing negative numbers (e.g.: initial parser)
|
||||||
if startChar == -1:
|
if start >= 0 and last >= 0:
|
||||||
str
|
let startChar = str.find(chars, start, last)
|
||||||
else:
|
if startChar == -1:
|
||||||
str.safeDelete(startChar..last)
|
str
|
||||||
|
else:
|
||||||
|
str.safeDelete(startChar..last)
|
||||||
|
else: str
|
||||||
|
|
||||||
func deleteAfterNewline*(str: string, start = 0): string =
|
func deleteAfterNewline*(str: string, start = 0): string =
|
||||||
## Delete string after next Newline from `start`.
|
## Delete string after next Newline from `start`.
|
||||||
|
|||||||
@@ -20,3 +20,4 @@ suite "utils/str":
|
|||||||
let t = "foo\nbar"
|
let t = "foo\nbar"
|
||||||
check(t.deleteAfterNewline() == "foo")
|
check(t.deleteAfterNewline() == "foo")
|
||||||
check(t.deleteAfterNewline(start = 4) == t)
|
check(t.deleteAfterNewline(start = 4) == t)
|
||||||
|
check(t.deleteAfterNewline(start = -1) == t)
|
||||||
|
|||||||
Reference in New Issue
Block a user