fix #19500; remove find optimization [backport: 1.6] (#19714)

* remove find optimization

close #19500

* save find to std

* add simple tests

* Apply suggestions from code review

Co-authored-by: konsumlamm <44230978+konsumlamm@users.noreply.github.com>

Co-authored-by: sandytypical <43030857+xflywind@users.noreply.github.com>
Co-authored-by: konsumlamm <44230978+konsumlamm@users.noreply.github.com>
This commit is contained in:
ringabout 2022-09-29 04:05:41 +08:00 • committed by GitHub
commit 65c2518d5c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 49 additions and 20 deletions

View file

@ -1882,9 +1882,6 @@ func find*(a: SkipTable, s, sub: string, start: Natural = 0, last = -1): int {.
when not (defined(js) or defined(nimdoc) or defined(nimscript)):
func c_memchr(cstr: pointer, c: char, n: csize_t): pointer {.
importc: "memchr", header: "<string.h>".}
func c_strstr(haystack, needle: cstring): cstring {.
importc: "strstr", header: "<string.h>".}
const hasCStringBuiltin = true
else:
const hasCStringBuiltin = false
@ -1954,23 +1951,7 @@ func find*(s, sub: string, start: Natural = 0, last = -1): int {.rtl,
if sub.len > s.len - start: return -1
if sub.len == 1: return find(s, sub[0], start, last)
template useSkipTable =
result = find(initSkipTable(sub), s, sub, start, last)
when nimvm:
useSkipTable()
else:
when hasCStringBuiltin:
if last < 0 and start < s.len:
let found = c_strstr(s[start].unsafeAddr, sub)
result = if not found.isNil:
cast[ByteAddress](found) -% cast[ByteAddress](s.cstring)
else:
-1
else:
useSkipTable()
else:
useSkipTable()
result = find(initSkipTable(sub), s, sub, start, last)
func rfind*(s: string, sub: char, start: Natural = 0, last = -1): int {.rtl,
extern: "nsuRFindChar".} =