C codegen: preparations for different seq and string implementations
This commit is contained in:
parent
b25044286b
commit
d837362216
8 changed files with 116 additions and 43 deletions
|
|
@ -15,11 +15,11 @@ type
|
|||
len, cap: int
|
||||
data: ptr UncheckedArray[T]
|
||||
|
||||
const nimSeqVersion {.core.} = 2
|
||||
|
||||
template frees(s) = dealloc(s.data, s.cap * sizeof(T))
|
||||
|
||||
# XXX make code memory safe for overflows in '*'
|
||||
proc nimSeqLiteral[T](x: openArray[T]): seq[T] {.core.} =
|
||||
seq[T](len: x.len, cap: x.len, data: x)
|
||||
|
||||
when defined(nimHasTrace):
|
||||
proc `=trace`[T](s: seq[T]; a: Allocator) =
|
||||
|
|
@ -120,7 +120,7 @@ proc `$`*[T](x: seq[T]): string =
|
|||
result = "@["
|
||||
var firstElement = true
|
||||
for i in 0..<x.len:
|
||||
let
|
||||
let
|
||||
value = x.data[i]
|
||||
if firstElement:
|
||||
firstElement = false
|
||||
|
|
|
|||
|
|
@ -12,12 +12,11 @@
|
|||
import allocators
|
||||
|
||||
type
|
||||
string {.core.} = object
|
||||
string {.core, exportc: "NimStringV2".} = object
|
||||
len, cap: int
|
||||
data: ptr UncheckedArray[char]
|
||||
|
||||
proc nimStringLiteral(x: cstring; len: int): string {.core.} =
|
||||
string(len: len, cap: len, data: x)
|
||||
const nimStrVersion {.core.} = 2
|
||||
|
||||
template frees(s) = dealloc(s.data, s.cap + 1)
|
||||
|
||||
|
|
@ -80,7 +79,7 @@ proc newString*(len: int): string =
|
|||
if len > 0:
|
||||
result.data = alloc0(len+1)
|
||||
|
||||
converter toCString(x: string): cstring {.core.} =
|
||||
converter toCString(x: string): cstring {.core, inline.} =
|
||||
if x.len == 0: cstring"" else: cast[cstring](x.data)
|
||||
|
||||
proc newStringOfCap*(cap: int): string =
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue