Add tests
This commit is contained in:
27
src/utils/str.nim
Normal file
27
src/utils/str.nim
Normal 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)
|
||||
Reference in New Issue
Block a user