diff options
Diffstat (limited to 'lib/system')
-rw-r--r-- | lib/system/io.nim | 9 | ||||
-rw-r--r-- | lib/system/nimscript.nim | 12 | ||||
-rw-r--r-- | lib/system/repr_v2.nim | 38 | ||||
-rw-r--r-- | lib/system/seqs_v2.nim | 4 | ||||
-rw-r--r-- | lib/system/strmantle.nim | 8 | ||||
-rw-r--r-- | lib/system/strs_v2.nim | 15 | ||||
-rw-r--r-- | lib/system/widestrs.nim | 1 |
7 files changed, 52 insertions, 35 deletions
diff --git a/lib/system/io.nim b/lib/system/io.nim index 9f12a1767..3d5cf981a 100644 --- a/lib/system/io.nim +++ b/lib/system/io.nim @@ -282,7 +282,11 @@ elif defined(windows): proc getOsfhandle(fd: cint): int {. importc: "_get_osfhandle", header: "<io.h>".} - proc setHandleInformation(handle: int, mask, flags: culong): cint {. + type + IoHandle = distinct pointer + ## Windows' HANDLE type. Defined as an untyped pointer but is **not** + ## one. Named like this to avoid collision with other `system` modules. + proc setHandleInformation(handle: IoHandle, mask, flags: culong): cint {. importc: "SetHandleInformation", header: "<handleapi.h>".} const @@ -339,7 +343,8 @@ when defined(nimdoc) or (defined(posix) and not defined(nimscript)) or defined(w flags = if inheritable: flags and not FD_CLOEXEC else: flags or FD_CLOEXEC result = c_fcntl(f, F_SETFD, flags) != -1 else: - result = setHandleInformation(f.int, HANDLE_FLAG_INHERIT, culong inheritable) != 0 + result = setHandleInformation(cast[IoHandle](f), HANDLE_FLAG_INHERIT, + culong inheritable) != 0 proc readLine*(f: File, line: var TaintedString): bool {.tags: [ReadIOEffect], benign.} = diff --git a/lib/system/nimscript.nim b/lib/system/nimscript.nim index ceaaef013..9e19fcfc4 100644 --- a/lib/system/nimscript.nim +++ b/lib/system/nimscript.nim @@ -136,13 +136,13 @@ proc dirExists*(dir: string): bool {. ## Checks if the directory `dir` exists. builtin -proc existsFile*(filename: string): bool = - ## An alias for ``fileExists``. - fileExists(filename) +template existsFile*(args: varargs[untyped]): untyped {.deprecated: "use fileExists".} = + # xxx: warning won't be shown for nimsscript because of current logic handling + # `foreignPackageNotes` + fileExists(args) -proc existsDir*(dir: string): bool = - ## An alias for ``dirExists``. - dirExists(dir) +template existsDir*(args: varargs[untyped]): untyped {.deprecated: "use dirExists".} = + dirExists(args) proc selfExe*(): string = ## Returns the currently running nim or nimble executable. diff --git a/lib/system/repr_v2.nim b/lib/system/repr_v2.nim index fcc187a42..d456f4454 100644 --- a/lib/system/repr_v2.nim +++ b/lib/system/repr_v2.nim @@ -29,21 +29,35 @@ proc repr*(x: bool): string {.magic: "BoolToStr", noSideEffect.} proc repr*(x: char): string {.noSideEffect.} = ## repr for a character argument. Returns `x` - ## converted to a string. + ## converted to an escaped string. ## ## .. code-block:: Nim ## assert repr('c') == "'c'" - '\'' & $x & '\'' - -proc repr*(x: cstring): string {.noSideEffect.} = - ## repr for a CString argument. Returns `x` - ## converted to a quoted string. - '"' & $x & '"' + result.add '\'' + # Elides string creations if not needed + if x in {'\\', '\0'..'\31', '\127'..'\255'}: + result.add '\\' + if x in {'\0'..'\31', '\127'..'\255'}: + result.add $x.uint8 + else: + result.add x + result.add '\'' -proc repr*(x: string): string {.noSideEffect.} = +proc repr*(x: string | cstring): string {.noSideEffect.} = ## repr for a string argument. Returns `x` - ## but quoted. - '"' & x & '"' + ## converted to a quoted and escaped string. + result.add '\"' + for i in 0..<x.len: + if x[i] in {'"', '\\', '\0'..'\31', '\127'..'\255'}: + result.add '\\' + case x[i]: + of '\n': + result.add "n\n" + of '\0'..'\9', '\11'..'\31', '\127'..'\255': + result.add $x[i].uint8 + else: + result.add x[i] + result.add '\"' proc repr*[Enum: enum](x: Enum): string {.magic: "EnumToStr", noSideEffect.} ## repr for an enumeration argument. This works for @@ -68,6 +82,10 @@ proc repr*(p: pointer): string = result[j] = HexChars[n and 0xF] n = n shr 4 +proc repr*(p: proc): string = + ## repr of a proc as its address + repr(cast[pointer](p)) + template repr*(x: distinct): string = repr(distinctBase(typeof(x))(x)) diff --git a/lib/system/seqs_v2.nim b/lib/system/seqs_v2.nim index 1b40c00ab..3c94a03f9 100644 --- a/lib/system/seqs_v2.nim +++ b/lib/system/seqs_v2.nim @@ -73,16 +73,16 @@ proc shrink*[T](x: var seq[T]; newLen: Natural) = when nimvm: setLen(x, newLen) else: - mixin `=destroy` #sysAssert newLen <= x.len, "invalid newLen parameter for 'shrink'" when not supportsCopyMem(T): for i in countdown(x.len - 1, newLen): - `=destroy`(x[i]) + reset x[i] # XXX This is wrong for const seqs that were moved into 'x'! cast[ptr NimSeqV2[T]](addr x).len = newLen proc grow*[T](x: var seq[T]; newLen: Natural; value: T) = let oldLen = x.len + #sysAssert newLen >= x.len, "invalid newLen parameter for 'grow'" if newLen <= oldLen: return var xu = cast[ptr NimSeqV2[T]](addr x) if xu.p == nil or xu.p.cap < newLen: diff --git a/lib/system/strmantle.nim b/lib/system/strmantle.nim index cb26833ac..43a769b5f 100644 --- a/lib/system/strmantle.nim +++ b/lib/system/strmantle.nim @@ -66,10 +66,6 @@ proc addInt*(result: var string; x: int64) = for j in 0..i div 2 - 1: swap(result[base+j], result[base+i-j-1]) -proc add*(result: var string; x: int64) {.deprecated: - "Deprecated since v0.20, use 'addInt'".} = - addInt(result, x) - proc nimIntToStr(x: int): string {.compilerRtl.} = result = newStringOfCap(sizeof(x)*4) result.addInt x @@ -98,10 +94,6 @@ proc addFloat*(result: var string; x: float) = let n = writeFloatToBuffer(buffer, x) result.addCstringN(cstring(buffer[0].addr), n) -proc add*(result: var string; x: float) {.deprecated: - "Deprecated since v0.20, use 'addFloat'".} = - addFloat(result, x) - proc nimFloatToStr(f: float): string {.compilerproc.} = result = newStringOfCap(8) result.addFloat f diff --git a/lib/system/strs_v2.nim b/lib/system/strs_v2.nim index aa644522f..85c0af190 100644 --- a/lib/system/strs_v2.nim +++ b/lib/system/strs_v2.nim @@ -132,10 +132,13 @@ proc nimAsgnStrV2(a: var NimStringV2, b: NimStringV2) {.compilerRtl.} = a.len = b.len copyMem(unsafeAddr a.p.data[0], unsafeAddr b.p.data[0], b.len+1) -proc nimPrepareStrMutationV2(s: var NimStringV2) {.compilerRtl.} = +proc nimPrepareStrMutationImpl(s: var NimStringV2) = + let oldP = s.p + # can't mutate a literal, so we need a fresh copy here: + s.p = cast[ptr NimStrPayload](allocShared0(contentSize(s.len))) + s.p.cap = s.len + copyMem(unsafeAddr s.p.data[0], unsafeAddr oldP.data[0], s.len+1) + +proc nimPrepareStrMutationV2(s: var NimStringV2) {.compilerRtl, inline.} = if s.p != nil and (s.p.cap and strlitFlag) == strlitFlag: - let oldP = s.p - # can't mutate a literal, so we need a fresh copy here: - s.p = cast[ptr NimStrPayload](allocShared0(contentSize(s.len))) - s.p.cap = s.len - copyMem(unsafeAddr s.p.data[0], unsafeAddr oldP.data[0], s.len+1) + nimPrepareStrMutationImpl(s) diff --git a/lib/system/widestrs.nim b/lib/system/widestrs.nim index aabcbdc90..83c11eb79 100644 --- a/lib/system/widestrs.nim +++ b/lib/system/widestrs.nim @@ -28,7 +28,6 @@ when defined(nimv2): proc `=destroy`(a: var WideCStringObj) = if a.data != nil: deallocShared(a.data) - a.data = nil proc `=`(a: var WideCStringObj; b: WideCStringObj) {.error.} |