From 24b24c17b1822d6931f6a7f05463b174bfc0f623 Mon Sep 17 00:00:00 2001 From: Jacek Sieka Date: Thu, 2 Jan 2025 17:28:35 +0100 Subject: [PATCH] fix c_memchr, c_strstr definitions (#24587) One correct definition is enough (cherry picked from commit e8bf6af0da52ce91dd0b5a7474d386d9a66809b9) --- lib/pure/memfiles.nim | 5 ++--- lib/pure/strutils.nim | 6 +++--- lib/std/formatfloat.nim | 2 +- lib/std/private/strimpl.nim | 8 ++------ lib/std/syncio.nim | 7 +++---- lib/system/ansi_c.nim | 23 ++++++++++------------- 6 files changed, 21 insertions(+), 30 deletions(-) diff --git a/lib/pure/memfiles.nim b/lib/pure/memfiles.nim index bb9b4b02c9..8430dde8b3 100644 --- a/lib/pure/memfiles.nim +++ b/lib/pure/memfiles.nim @@ -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: "".} 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 diff --git a/lib/pure/strutils.nim b/lib/pure/strutils.nim index 9cb6294fe6..687dedd514 100644 --- a/lib/pure/strutils.nim +++ b/lib/pure/strutils.nim @@ -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: "".} + 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: diff --git a/lib/std/formatfloat.nim b/lib/std/formatfloat.nim index 8e9d36ff60..8c63388dc1 100644 --- a/lib/std/formatfloat.nim +++ b/lib/std/formatfloat.nim @@ -12,7 +12,7 @@ when defined(nimPreviewSlimSystem): import std/assertions -proc c_memcpy(a, b: pointer, size: csize_t): pointer {.importc: "memcpy", header: "", 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 diff --git a/lib/std/private/strimpl.nim b/lib/std/private/strimpl.nim index d1732bf365..8ddb39cf0c 100644 --- a/lib/std/private/strimpl.nim +++ b/lib/std/private/strimpl.nim @@ -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: "".} -func c_strstr(haystack, needle: cstring): cstring {. - importc: "strstr", header: "".} - +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 diff --git a/lib/std/syncio.nim b/lib/std/syncio.nim index 5c97712a43..911bff276e 100644 --- a/lib/std/syncio.nim +++ b/lib/std/syncio.nim @@ -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: "", @@ -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: "".} - 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]) diff --git a/lib/system/ansi_c.nim b/lib/system/ansi_c.nim index 3098e17d69..ed1a8aedf1 100644 --- a/lib/system/ansi_c.nim +++ b/lib/system/ansi_c.nim @@ -27,6 +27,8 @@ proc c_strcmp*(a, b: cstring): cint {. importc: "strcmp", header: "", noSideEffect.} proc c_strlen*(a: cstring): csize_t {. importc: "strlen", header: "", noSideEffect.} +proc c_strstr*(haystack, needle: cstring): cstring {. + importc: "strstr", header: "", noSideEffect.} proc c_abort*() {. importc: "abort", header: "", 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)