diff options
author | Andreas Rumpf <rumpf_a@web.de> | 2019-08-07 22:40:58 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-08-07 22:40:58 +0200 |
commit | c0d240b8cd3dc08d25c671b0dc7614fbfa980c2e (patch) | |
tree | 82ca82c0cd8d8dc7bd164d352d9b5a1910cd7164 /lib | |
parent | f34ae81971ada93fd4fda871c3da6cba9aab8bff (diff) | |
download | Nim-c0d240b8cd3dc08d25c671b0dc7614fbfa980c2e.tar.gz |
fixes #11807 (#11900)
* fixes #11807 * make tests green again
Diffstat (limited to 'lib')
-rw-r--r-- | lib/pure/includes/osenv.nim | 5 | ||||
-rw-r--r-- | lib/system/widestrs.nim | 57 |
2 files changed, 56 insertions, 6 deletions
diff --git a/lib/pure/includes/osenv.nim b/lib/pure/includes/osenv.nim index 193efd4d4..1ddc51f8f 100644 --- a/lib/pure/includes/osenv.nim +++ b/lib/pure/includes/osenv.nim @@ -19,6 +19,11 @@ var envComputed {.threadvar.}: bool environment {.threadvar.}: seq[string] +when defined(nimV2): + proc unpairedEnvAllocs*(): int = + result = environment.len + if result > 0: inc result + when defined(windows) and not defined(nimscript): # because we support Windows GUI applications, things get really # messy here... diff --git a/lib/system/widestrs.nim b/lib/system/widestrs.nim index 31181e027..cf5e728d7 100644 --- a/lib/system/widestrs.nim +++ b/lib/system/widestrs.nim @@ -15,7 +15,52 @@ type Utf16Char* = distinct int16 - WideCString* = ref UncheckedArray[Utf16Char] + +when defined(nimv2): + + import core / allocators + + type + WideCString* = ptr UncheckedArray[Utf16Char] + + WideCStringObj* = object + bytes: int + data: WideCString + + proc `=destroy`(a: var WideCStringObj) = + if a.data != nil: + let alor = getLocalAllocator() + alor.dealloc(alor, a.data, a.bytes) + a.data = nil + + proc `=`(a: var WideCStringObj; b: WideCStringObj) {.error.} + + proc `=sink`(a: var WideCStringObj; b: WideCStringObj) = + a.bytes = b.bytes + a.data = b.data + + proc createWide(a: var WideCStringObj; bytes: int) = + a.bytes = bytes + let alor = getLocalAllocator() + a.data = cast[typeof(a.data)](alor.alloc(alor, bytes)) + + template `[]`(a: WideCStringObj; idx: int): Utf16Char = a.data[idx] + template `[]=`(a: WideCStringObj; idx: int; val: Utf16Char) = a.data[idx] = val + + template nullWide(): untyped = WideCStringObj(bytes: 0, data: nil) + + converter toWideCString*(x: WideCStringObj): WideCString {.inline.} = + result = x.data + +else: + template nullWide(): untyped = nil + + type + WideCString* = ref UncheckedArray[Utf16Char] + WideCStringObj* = WideCString + + template createWide(a; L) = + unsafeNew(a, L * 4 + 2) proc ord(arg: Utf16Char): int = int(cast[uint16](arg)) @@ -95,8 +140,8 @@ iterator runes(s: cstring, L: int): int = fastRuneAt(s, i, L, result, true) yield result -proc newWideCString*(source: cstring, L: int): WideCString = - unsafeNew(result, L * 4 + 2) +proc newWideCString*(source: cstring, L: int): WideCStringObj = + createWide(result, L * 4 + 2) #result = cast[wideCString](alloc(L * 4 + 2)) var d = 0 for ch in runes(source, L): @@ -116,12 +161,12 @@ proc newWideCString*(source: cstring, L: int): WideCString = inc d result[d] = Utf16Char(0) -proc newWideCString*(s: cstring): WideCString = - if s.isNil: return nil +proc newWideCString*(s: cstring): WideCStringObj = + if s.isNil: return nullWide result = newWideCString(s, s.len) -proc newWideCString*(s: string): WideCString = +proc newWideCString*(s: string): WideCStringObj = result = newWideCString(s, s.len) proc `$`*(w: WideCString, estimate: int, replacement: int = 0xFFFD): string = |