strutils.rfind via char

This commit is contained in:
dyu
2014-12-19 20:08:42 +08:00
parent bce10ac1d3
commit 12f97a7151
2 changed files with 14 additions and 0 deletions

View File

@@ -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`.

View File

@@ -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