diff options
author | Charles Blake <charlechaud@gmail.com> | 2019-06-12 07:44:56 -0400 |
---|---|---|
committer | Charles Blake <charlechaud@gmail.com> | 2019-06-12 07:44:56 -0400 |
commit | bde899d4f8f2eee7cd65d4daaacb082a858bcf60 (patch) | |
tree | 466c9b6ae91052d9887d9f82ed3bf7f12ebedb5b /lib | |
parent | da035e9c8385be59449d13d1355aba4f9f97a6b4 (diff) | |
download | Nim-bde899d4f8f2eee7cd65d4daaacb082a858bcf60.tar.gz |
Attempt to close https://github.com/nim-lang/Nim/issues/11430
Diffstat (limited to 'lib')
-rw-r--r-- | lib/pure/strutils.nim | 52 |
1 files changed, 36 insertions, 16 deletions
diff --git a/lib/pure/strutils.nim b/lib/pure/strutils.nim index 114141e45..44ca6d98c 100644 --- a/lib/pure/strutils.nim +++ b/lib/pure/strutils.nim @@ -1850,6 +1850,8 @@ proc find*(s: string, sub: char, start: Natural = 0, last = 0): int {.noSideEffe ## If `last` is unspecified, it defaults to `s.high` (the last element). ## ## Searching is case-sensitive. If `sub` is not in `s`, -1 is returned. + ## Otherwise the index returned is relative to ``s[0]``, not ``start``. + ## Use `s[start..last].rfind` for a ``start``-origin index. ## ## See also: ## * `rfind proc<#rfind,string,char,int>`_ @@ -1876,6 +1878,8 @@ proc find*(s: string, chars: set[char], start: Natural = 0, last = 0): int {.noS ## If `last` is unspecified, it defaults to `s.high` (the last element). ## ## If `s` contains none of the characters in `chars`, -1 is returned. + ## Otherwise the index returned is relative to ``s[0]``, not ``start``. + ## Use `s[start..last].find` for a ``start``-origin index. ## ## See also: ## * `rfind proc<#rfind,string,set[char],int>`_ @@ -1891,6 +1895,8 @@ proc find*(s, sub: string, start: Natural = 0, last = 0): int {.noSideEffect, ## If `last` is unspecified, it defaults to `s.high` (the last element). ## ## Searching is case-sensitive. If `sub` is not in `s`, -1 is returned. + ## Otherwise the index returned is relative to ``s[0]``, not ``start``. + ## Use `s[start..last].find` for a ``start``-origin index. ## ## See also: ## * `rfind proc<#rfind,string,string,int>`_ @@ -1901,45 +1907,59 @@ proc find*(s, sub: string, start: Natural = 0, last = 0): int {.noSideEffect, initSkipTable(a, sub) result = find(a, s, sub, start, last) -proc rfind*(s: string, sub: char, start: int = -1): int {.noSideEffect, +proc rfind*(s: string, sub: char, start: Natural = 0, last = -1): int {.noSideEffect, rtl.} = - ## Searches for characer `sub` in `s` in reverse, starting at position `start` - ## (default: the last character) and going backwards to the first character. + ## Searches for `sub` in `s` inside range ``start..last`` (both ends included) + ## in reverse -- starting at high indexes and moving lower to the first + ## character or ``start``. If `last` is unspecified, it defaults to `s.high` + ## (the last element). ## ## Searching is case-sensitive. If `sub` is not in `s`, -1 is returned. + ## Otherwise the index returned is relative to ``s[0]``, not ``start``. + ## Use `s[start..last].find` for a ``start``-origin index. ## ## See also: ## * `find proc<#find,string,char,int,int>`_ - let realStart = if start == -1: s.len-1 else: start - for i in countdown(realStart, 0): + let last = if last == -1: s.high else: last + for i in countdown(last, start): if sub == s[i]: return i return -1 -proc rfind*(s: string, chars: set[char], start: int = -1): int {.noSideEffect.} = - ## Searches for `chars` in `s` in reverse, starting at position `start` - ## (default: the last character) and going backwards to the first character. +proc rfind*(s: string, chars: set[char], start: Natural = 0, last = -1): int {.noSideEffect, + rtl.} = + ## Searches for `chars` in `s` inside range ``start..last`` (both ends + ## included) in reverse -- starting at high indexes and moving lower to the + ## first character or ``start``. If `last` is unspecified, it defaults to + ## `s.high` (the last element). ## - ## Searching is case-sensitive. If `sub` is not in `s`, -1 is returned. + ## If `s` contains none of the characters in `chars`, -1 is returned. + ## Otherwise the index returned is relative to ``s[0]``, not ``start``. + ## Use `s[start..last].rfind` for a ``start``-origin index. ## ## See also: ## * `find proc<#find,string,set[char],Natural,int>`_ - let realStart = if start == -1: s.len-1 else: start - for i in countdown(realStart, 0): + let last = if last == -1: s.high else: last + for i in countdown(last, start): if s[i] in chars: return i return -1 -proc rfind*(s, sub: string, start: int = -1): int {.noSideEffect.} = - ## Searches for string `sub` in `s` in reverse, starting at position `start` - ## (default: the last character) and going backwards to the first character. +proc rfind*(s, sub: string, start: Natural = 0, last = -1): int {.noSideEffect, + rtl.} = + ## Searches for `sub` in `s` inside range ``start..last`` (both ends included) + ## included) in reverse -- starting at high indexes and moving lower to the + ## first character or ``start``. If `last` is unspecified, it defaults to + ## `s.high` (the last element). ## ## Searching is case-sensitive. If `sub` is not in `s`, -1 is returned. + ## Otherwise the index returned is relative to ``s[0]``, not ``start``. + ## Use `s[start..last].rfind` for a ``start``-origin index. ## ## See also: ## * `find proc<#find,string,string,Natural,int>`_ if sub.len == 0: return -1 - let realStart = if start == -1: s.len else: start - for i in countdown(realStart-sub.len, 0): + let last = if last == -1: s.high else: last + for i in countdown(last - sub.len + 1, start): for j in 0..sub.len-1: result = i if sub[j] != s[i+j]: |