mirror of
https://github.com/nim-lang/Nim.git
synced 2025-12-30 18:02:05 +00:00
Added strutils.rfind.
This commit is contained in:
@@ -654,6 +654,20 @@ proc find*(s: string, chars: set[char], start: int = 0): int {.noSideEffect,
|
||||
for i in start..s.len-1:
|
||||
if s[i] in chars: return i
|
||||
return -1
|
||||
|
||||
proc rfind*(s, sub: string, start: int = -1): int {.noSideEffect.} =
|
||||
## Searches for `sub` in `s` in reverse, starting at `start` and going
|
||||
## backwards to 0. Searching is case-sensitive. If `sub` is not in `s`, -1 is
|
||||
## returned.
|
||||
let realStart = if start == -1: s.len else: start
|
||||
for i in countdown(realStart-sub.len, 0):
|
||||
for j in 0..sub.len-1:
|
||||
result = i
|
||||
if sub[j] != s[i+j]:
|
||||
result = -1
|
||||
break
|
||||
if result != -1: return
|
||||
return -1
|
||||
|
||||
proc quoteIfContainsWhite*(s: string): string =
|
||||
## returns ``'"' & s & '"'`` if `s` contains a space and does not
|
||||
|
||||
Reference in New Issue
Block a user