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

@@ -30,6 +30,7 @@ import std/oserrors
when defined(nimPreviewSlimSystem):
import std/[syncio, assertions]
from system/ansi_c import c_memchr
proc newEIO(msg: string): ref IOError =
result = (ref IOError)(msg: msg)
@@ -448,14 +449,12 @@ iterator memSlices*(mfile: MemFile, delim = '\l', eat = '\r'): MemSlice {.inline
## echo count
## ```
proc c_memchr(cstr: pointer, c: char, n: csize_t): pointer {.
importc: "memchr", header: "<string.h>".}
proc `-!`(p, q: pointer): int {.inline.} = return cast[int](p) -% cast[int](q)
var ending: pointer
var ms = MemSlice(data: mfile.mem, size: 0)
var remaining = mfile.size
while remaining > 0:
ending = c_memchr(ms.data, delim, csize_t(remaining))
ending = c_memchr(ms.data, cint(delim), csize_t(remaining))
if ending == nil: # unterminated final slice
ms.size = remaining # Weird case..check eat?
yield ms

View File

@@ -1950,8 +1950,8 @@ func find*(a: SkipTable, s, sub: string, start: Natural = 0, last = -1): int {.
inc skip, a[s[skip + subLast]]
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>".}
from system/ansi_c import c_memchr
const hasCStringBuiltin = true
else:
const hasCStringBuiltin = false
@@ -1982,7 +1982,7 @@ func find*(s: string, sub: char, start: Natural = 0, last = -1): int {.rtl,
when hasCStringBuiltin:
let length = last-start+1
if length > 0:
let found = c_memchr(s[start].unsafeAddr, sub, cast[csize_t](length))
let found = c_memchr(s[start].unsafeAddr, cint(sub), cast[csize_t](length))
if not found.isNil:
return cast[int](found) -% cast[int](s.cstring)
else:

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])

View File

@@ -27,6 +27,8 @@ proc c_strcmp*(a, b: cstring): cint {.
importc: "strcmp", header: "<string.h>", noSideEffect.}
proc c_strlen*(a: cstring): csize_t {.
importc: "strlen", header: "<string.h>", noSideEffect.}
proc c_strstr*(haystack, needle: cstring): cstring {.
importc: "strstr", header: "<string.h>", noSideEffect.}
proc c_abort*() {.
importc: "abort", header: "<stdlib.h>", noSideEffect, noreturn.}
@@ -76,19 +78,14 @@ elif defined(haiku):
SIGTERM* = cint(15)
SIGPIPE* = cint(7)
SIG_DFL* = CSighandlerT(nil)
else:
when defined(nimscript):
{.error: "SIGABRT not ported to your platform".}
else:
var
SIGINT* {.importc: "SIGINT", nodecl.}: cint
SIGSEGV* {.importc: "SIGSEGV", nodecl.}: cint
SIGABRT* {.importc: "SIGABRT", nodecl.}: cint
SIGFPE* {.importc: "SIGFPE", nodecl.}: cint
SIGILL* {.importc: "SIGILL", nodecl.}: cint
SIG_DFL* {.importc: "SIG_DFL", nodecl.}: CSighandlerT
when defined(macosx) or defined(linux):
var SIGPIPE* {.importc: "SIGPIPE", nodecl.}: cint
elif not defined(nimscript):
var
SIGINT* {.importc: "SIGINT", nodecl.}: cint
SIGSEGV* {.importc: "SIGSEGV", nodecl.}: cint
SIGABRT* {.importc: "SIGABRT", nodecl.}: cint
SIGFPE* {.importc: "SIGFPE", nodecl.}: cint
SIGILL* {.importc: "SIGILL", nodecl.}: cint
SIG_DFL* {.importc: "SIG_DFL", nodecl.}: CSighandlerT
when defined(macosx):
const SIGBUS* = cint(10)