Add tests

This commit is contained in:
Florian Schroedl
2022-01-20 17:00:00 +01:00
parent d24e596ce3
commit 49ebf7329a
5 changed files with 59 additions and 3 deletions

27
src/utils/str.nim Normal file
View File

@@ -0,0 +1,27 @@
import std/strutils
import std/math
proc safeDelete*(str: string, slice: Slice[int]): string =
## Deletes the items `str[slice]`, ignoring elements out of range.
if slice.a > str.len - 1:
str
else:
let fromIndex = clamp(slice.a, 0..str.len)
let toIndex = clamp(slice.b, 0..str.len - 1)
var strDup = str
strDup.delete(fromIndex..toIndex)
strDup
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)
func deleteAfterNewline*(str: string, start = 0): string =
## Delete string after next Newline from `start`.
findAndDelete(str, Newlines, start)