Fix questionable suggestion in strutils docs (#19765)

* Fix questionable suggestion in `stutils` docs

- Removes the recommendation to pass a string slice for getting a relative
index for `find` and `rfind` functions, as this currently makes a string
copy, while a simple subtraction is enough.

- Docstring for `SkipTable` type.

* Doc layout fixup
This commit is contained in:
Zoom 2022-05-06 09:19:27 +00:00 • committed by GitHub
commit 0455d24d55
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -278,7 +278,7 @@ func nimIdentNormalize*(s: string): string =
## except first one. ## except first one.
## ##
## .. Warning:: Backticks (`) are not handled: they remain *as is* and ## .. Warning:: Backticks (`) are not handled: they remain *as is* and
## spaces are preserved. See `nimIdentBackticksNormalize ## spaces are preserved. See `nimIdentBackticksNormalize
## <dochelpers.html#nimIdentBackticksNormalize,string>`_ for ## <dochelpers.html#nimIdentBackticksNormalize,string>`_ for
## an alternative approach. ## an alternative approach.
runnableExamples: runnableExamples:
@ -1808,11 +1808,15 @@ func join*[T: not string](a: openArray[T], sep: string = ""): string =
add(result, $x) add(result, $x)
type type
SkipTable* = array[char, int] SkipTable* = array[char, int] ## Character table for efficient substring search.
func initSkipTable*(a: var SkipTable, sub: string) {.rtl, func initSkipTable*(a: var SkipTable, sub: string) {.rtl,
extern: "nsuInitSkipTable".} = extern: "nsuInitSkipTable".} =
## Preprocess table `a` for `sub`. ## Initializes table `a` for efficient search of substring `sub`.
##
## See also:
## * `find func<#find,SkipTable,string,string,Natural,int>`_
# TODO: this should be the `default()` initializer for the type.
let m = len(sub) let m = len(sub)
fill(a, m) fill(a, m)
@ -1826,6 +1830,9 @@ func find*(a: SkipTable, s, sub: string, start: Natural = 0, last = 0): int {.
## element). ## element).
## ##
## Searching is case-sensitive. If `sub` is not in `s`, -1 is returned. ## Searching is case-sensitive. If `sub` is not in `s`, -1 is returned.
##
## See also:
## * `initSkipTable func<#initSkipTable,SkipTable,string>`_
let let
last = if last == 0: s.high else: last last = if last == 0: s.high else: last
subLast = sub.len - 1 subLast = sub.len - 1
@ -1865,7 +1872,7 @@ func find*(s: string, sub: char, start: Natural = 0, last = 0): int {.rtl,
## ##
## Searching is case-sensitive. If `sub` is not in `s`, -1 is returned. ## Searching is case-sensitive. If `sub` is not in `s`, -1 is returned.
## Otherwise the index returned is relative to `s[0]`, not `start`. ## Otherwise the index returned is relative to `s[0]`, not `start`.
## Use `s[start..last].rfind` for a `start`-origin index. ## Subtract `start` from the result for a `start`-origin index.
## ##
## See also: ## See also:
## * `rfind func<#rfind,string,char,Natural,int>`_ ## * `rfind func<#rfind,string,char,Natural,int>`_
@ -1893,7 +1900,7 @@ func find*(s: string, chars: set[char], start: Natural = 0, last = 0): int {.
## ##
## If `s` contains none of the characters in `chars`, -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`. ## Otherwise the index returned is relative to `s[0]`, not `start`.
## Use `s[start..last].find` for a `start`-origin index. ## Subtract `start` from the result for a `start`-origin index.
## ##
## See also: ## See also:
## * `rfind func<#rfind,string,set[char],Natural,int>`_ ## * `rfind func<#rfind,string,set[char],Natural,int>`_
@ -1910,7 +1917,7 @@ func find*(s, sub: string, start: Natural = 0, last = 0): int {.rtl,
## ##
## Searching is case-sensitive. If `sub` is not in `s`, -1 is returned. ## Searching is case-sensitive. If `sub` is not in `s`, -1 is returned.
## Otherwise the index returned is relative to `s[0]`, not `start`. ## Otherwise the index returned is relative to `s[0]`, not `start`.
## Use `s[start..last].find` for a `start`-origin index. ## Subtract `start` from the result for a `start`-origin index.
## ##
## See also: ## See also:
## * `rfind func<#rfind,string,string,Natural,int>`_ ## * `rfind func<#rfind,string,string,Natural,int>`_
@ -1950,7 +1957,7 @@ func rfind*(s: string, sub: char, start: Natural = 0, last = -1): int {.rtl,
## ##
## Searching is case-sensitive. If `sub` is not in `s`, -1 is returned. ## Searching is case-sensitive. If `sub` is not in `s`, -1 is returned.
## Otherwise the index returned is relative to `s[0]`, not `start`. ## Otherwise the index returned is relative to `s[0]`, not `start`.
## Use `s[start..last].find` for a `start`-origin index. ## Subtract `start` from the result for a `start`-origin index.
## ##
## See also: ## See also:
## * `find func<#find,string,char,Natural,int>`_ ## * `find func<#find,string,char,Natural,int>`_
@ -1968,7 +1975,7 @@ func rfind*(s: string, chars: set[char], start: Natural = 0, last = -1): int {.
## ##
## If `s` contains none of the characters in `chars`, -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`. ## Otherwise the index returned is relative to `s[0]`, not `start`.
## Use `s[start..last].rfind` for a `start`-origin index. ## Subtract `start` from the result for a `start`-origin index.
## ##
## See also: ## See also:
## * `find func<#find,string,set[char],Natural,int>`_ ## * `find func<#find,string,set[char],Natural,int>`_
@ -1986,7 +1993,7 @@ func rfind*(s, sub: string, start: Natural = 0, last = -1): int {.rtl,
## ##
## Searching is case-sensitive. If `sub` is not in `s`, -1 is returned. ## Searching is case-sensitive. If `sub` is not in `s`, -1 is returned.
## Otherwise the index returned is relative to `s[0]`, not `start`. ## Otherwise the index returned is relative to `s[0]`, not `start`.
## Use `s[start..last].rfind` for a `start`-origin index. ## Subtract `start` from the result for a `start`-origin index.
## ##
## See also: ## See also:
## * `find func<#find,string,string,Natural,int>`_ ## * `find func<#find,string,string,Natural,int>`_