fix c_memchr, c_strstr definitions (#24587)

One correct definition is enough
This commit is contained in:
Jacek Sieka
2025-01-02 17:28:35 +01:00
committed by GitHub
parent 78835562b1
commit e8bf6af0da
6 changed files with 21 additions and 30 deletions

View File

@@ -12,7 +12,7 @@
when defined(nimPreviewSlimSystem):
import std/assertions
proc c_memcpy(a, b: pointer, size: csize_t): pointer {.importc: "memcpy", header: "<string.h>", discardable.}
from system/ansi_c import c_memcpy
proc addCstringN(result: var string, buf: cstring; buflen: int) =
# no nimvm support needed, so it doesn't need to be fast here either

View File

@@ -75,11 +75,7 @@ template endsWithImpl*[T: string | cstring](s, suffix: T) =
func cmpNimIdentifier*[T: string | cstring](a, b: T): int =
cmpIgnoreStyleImpl(a, b, true)
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>".}
from system/ansi_c import c_memchr, c_strstr
func find*(s: cstring, sub: char, start: Natural = 0, last = 0): int =
## Searches for `sub` in `s` inside the range `start..last` (both ends included).
@@ -91,7 +87,7 @@ func find*(s: cstring, sub: char, start: Natural = 0, last = 0): int =
let last = if last == 0: s.high else: last
let L = last-start+1
if L > 0:
let found = c_memchr(s[start].unsafeAddr, sub, cast[csize_t](L))
let found = c_memchr(s[start].unsafeAddr, cint(sub), cast[csize_t](L))
if not found.isNil:
return cast[int](found) -% cast[int](s)
return -1

View File

@@ -15,6 +15,8 @@ import std/formatfloat
when defined(windows):
import std/widestrs
from system/ansi_c import c_memchr
# ----------------- IO Part ------------------------------------------------
type
CFile {.importc: "FILE", header: "<stdio.h>",
@@ -401,9 +403,6 @@ proc readLine*(f: File, line: var string): bool {.tags: [ReadIOEffect],
## `false` is returned `line` contains no new data.
result = false
proc c_memchr(s: pointer, c: cint, n: csize_t): pointer {.
importc: "memchr", header: "<string.h>".}
when defined(windows):
proc readConsole(hConsoleInput: FileHandle, lpBuffer: pointer,
nNumberOfCharsToRead: int32,
@@ -491,7 +490,7 @@ proc readLine*(f: File, line: var string): bool {.tags: [ReadIOEffect],
checkErr(f)
break
let m = c_memchr(addr line[pos], '\L'.ord, cast[csize_t](sp))
let m = c_memchr(addr line[pos], cint('\L'), cast[csize_t](sp))
if m != nil:
# \l found: Could be our own or the one by fgets, in any case, we're done
var last = cast[int](m) - cast[int](addr line[0])