diff options
-rw-r--r-- | lib/pure/strutils.nim | 10 | ||||
-rw-r--r-- | tests/stdlib/tstrutil.nim | 4 |
2 files changed, 14 insertions, 0 deletions
diff --git a/lib/pure/strutils.nim b/lib/pure/strutils.nim index 55a204b4c..4f449cb0e 100644 --- a/lib/pure/strutils.nim +++ b/lib/pure/strutils.nim @@ -803,6 +803,16 @@ proc rfind*(s, sub: string, start: int = -1): int {.noSideEffect.} = if result != -1: return return -1 +proc rfind*(s: string, sub: char, start: int = -1): int {.noSideEffect, + rtl.} = + ## Searches for `sub` in `s` in reverse starting at position `start`. + ## + ## Searching is case-sensitive. If `sub` is not in `s`, -1 is returned. + let realStart = if start == -1: s.len-1 else: start + for i in countdown(realStart, 0): + if sub == s[i]: return i + return -1 + proc count*(s: string, sub: string, overlapping: bool = false): int {.noSideEffect, rtl, extern: "nsuCountString".} = ## Count the occurences of a substring `sub` in the string `s`. diff --git a/tests/stdlib/tstrutil.nim b/tests/stdlib/tstrutil.nim index 80c2f3870..da65d1f89 100644 --- a/tests/stdlib/tstrutil.nim +++ b/tests/stdlib/tstrutil.nim @@ -38,6 +38,10 @@ assert(editDistance("prefix__hallo_suffix", "prefix__ha_suffix") == 3) assert(editDistance("prefix__hallo_suffix", "prefix") == 14) assert(editDistance("prefix__hallo_suffix", "suffix") == 14) assert(editDistance("prefix__hallo_suffix", "prefix__hao_suffix") == 2) + +assert "/1/2/3".rfind('/') == 4 +assert "/1/2/3".rfind('/', 1) == 0 +assert "/1/2/3".rfind('0') == -1 main() #OUT ha/home/a1xyz/usr/bin |