case consistency part 4
This commit is contained in:
parent
706266d8b7
commit
92b8fac94a
122 changed files with 3322 additions and 3322 deletions
|
|
@ -109,7 +109,7 @@ type
|
|||
data: TTrunkBuckets
|
||||
|
||||
type
|
||||
TAlignType = biggestFloat
|
||||
TAlignType = BiggestFloat
|
||||
TFreeCell {.final, pure.} = object
|
||||
next: ptr TFreeCell # next free cell in chunk (overlaid with refcount)
|
||||
zeroField: int # 0 means cell is not used (overlaid with typ field)
|
||||
|
|
@ -260,7 +260,7 @@ proc intSetGet(t: TIntSet, key: int): PTrunk =
|
|||
result = nil
|
||||
|
||||
proc intSetPut(a: var TMemRegion, t: var TIntSet, key: int): PTrunk =
|
||||
result = IntSetGet(t, key)
|
||||
result = intSetGet(t, key)
|
||||
if result == nil:
|
||||
result = cast[PTrunk](llAlloc(a, sizeof(result[])))
|
||||
result.next = t.data[key and high(t.data)]
|
||||
|
|
@ -268,7 +268,7 @@ proc intSetPut(a: var TMemRegion, t: var TIntSet, key: int): PTrunk =
|
|||
result.key = key
|
||||
|
||||
proc contains(s: TIntSet, key: int): bool =
|
||||
var t = IntSetGet(s, key shr TrunkShift)
|
||||
var t = intSetGet(s, key shr TrunkShift)
|
||||
if t != nil:
|
||||
var u = key and TrunkMask
|
||||
result = (t.bits[u shr IntShift] and (1 shl (u and IntMask))) != 0
|
||||
|
|
@ -276,12 +276,12 @@ proc contains(s: TIntSet, key: int): bool =
|
|||
result = false
|
||||
|
||||
proc incl(a: var TMemRegion, s: var TIntSet, key: int) =
|
||||
var t = IntSetPut(a, s, key shr TrunkShift)
|
||||
var t = intSetPut(a, s, key shr TrunkShift)
|
||||
var u = key and TrunkMask
|
||||
t.bits[u shr IntShift] = t.bits[u shr IntShift] or (1 shl (u and IntMask))
|
||||
|
||||
proc excl(s: var TIntSet, key: int) =
|
||||
var t = IntSetGet(s, key shr TrunkShift)
|
||||
var t = intSetGet(s, key shr TrunkShift)
|
||||
if t != nil:
|
||||
var u = key and TrunkMask
|
||||
t.bits[u shr IntShift] = t.bits[u shr IntShift] and not
|
||||
|
|
@ -444,7 +444,7 @@ proc freeBigChunk(a: var TMemRegion, c: PBigChunk) =
|
|||
if isAccessible(a, ri) and chunkUnused(ri):
|
||||
sysAssert(not isSmallChunk(ri), "freeBigChunk 3")
|
||||
if not isSmallChunk(ri):
|
||||
ListRemove(a.freeChunksList, cast[PBigChunk](ri))
|
||||
listRemove(a.freeChunksList, cast[PBigChunk](ri))
|
||||
inc(c.size, ri.size)
|
||||
excl(a.chunkStarts, pageIndex(ri))
|
||||
when coalescLeft:
|
||||
|
|
@ -454,7 +454,7 @@ proc freeBigChunk(a: var TMemRegion, c: PBigChunk) =
|
|||
if isAccessible(a, le) and chunkUnused(le):
|
||||
sysAssert(not isSmallChunk(le), "freeBigChunk 5")
|
||||
if not isSmallChunk(le):
|
||||
ListRemove(a.freeChunksList, cast[PBigChunk](le))
|
||||
listRemove(a.freeChunksList, cast[PBigChunk](le))
|
||||
inc(le.size, c.size)
|
||||
excl(a.chunkStarts, pageIndex(c))
|
||||
c = cast[PBigChunk](le)
|
||||
|
|
@ -462,7 +462,7 @@ proc freeBigChunk(a: var TMemRegion, c: PBigChunk) =
|
|||
if c.size < ChunkOsReturn or weirdUnmap:
|
||||
incl(a, a.chunkStarts, pageIndex(c))
|
||||
updatePrevSize(a, c, c.size)
|
||||
ListAdd(a.freeChunksList, c)
|
||||
listAdd(a.freeChunksList, c)
|
||||
c.used = false
|
||||
else:
|
||||
freeOsChunks(a, c, c.size)
|
||||
|
|
@ -478,7 +478,7 @@ proc splitChunk(a: var TMemRegion, c: PBigChunk, size: int) =
|
|||
updatePrevSize(a, c, rest.size)
|
||||
c.size = size
|
||||
incl(a, a.chunkStarts, pageIndex(rest))
|
||||
ListAdd(a.freeChunksList, rest)
|
||||
listAdd(a.freeChunksList, rest)
|
||||
|
||||
proc getBigChunk(a: var TMemRegion, size: int): PBigChunk =
|
||||
# use first fit for now:
|
||||
|
|
@ -489,10 +489,10 @@ proc getBigChunk(a: var TMemRegion, size: int): PBigChunk =
|
|||
while result != nil:
|
||||
sysAssert chunkUnused(result), "getBigChunk 3"
|
||||
if result.size == size:
|
||||
ListRemove(a.freeChunksList, result)
|
||||
listRemove(a.freeChunksList, result)
|
||||
break search
|
||||
elif result.size > size:
|
||||
ListRemove(a.freeChunksList, result)
|
||||
listRemove(a.freeChunksList, result)
|
||||
splitChunk(a, result, size)
|
||||
break search
|
||||
result = result.next
|
||||
|
|
@ -531,7 +531,7 @@ proc allocInv(a: TMemRegion): bool =
|
|||
while it != nil:
|
||||
if it.zeroField != 0:
|
||||
echo "[SYSASSERT] it.zeroField != 0"
|
||||
cprintf("%ld %p\n", it.zeroField, it)
|
||||
c_printf("%ld %p\n", it.zeroField, it)
|
||||
return false
|
||||
it = it.next
|
||||
c = c.next
|
||||
|
|
@ -557,7 +557,7 @@ proc rawAlloc(a: var TMemRegion, requestedSize: int): pointer =
|
|||
c.free = SmallChunkSize - smallChunkOverhead() - size
|
||||
c.next = nil
|
||||
c.prev = nil
|
||||
ListAdd(a.freeSmallChunks[s], c)
|
||||
listAdd(a.freeSmallChunks[s], c)
|
||||
result = addr(c.data)
|
||||
sysAssert((cast[TAddress](result) and (MemAlign-1)) == 0, "rawAlloc 4")
|
||||
else:
|
||||
|
|
@ -581,7 +581,7 @@ proc rawAlloc(a: var TMemRegion, requestedSize: int): pointer =
|
|||
sysAssert(allocInv(a), "rawAlloc: before c.free < size")
|
||||
if c.free < size:
|
||||
sysAssert(allocInv(a), "rawAlloc: before listRemove test")
|
||||
ListRemove(a.freeSmallChunks[s], c)
|
||||
listRemove(a.freeSmallChunks[s], c)
|
||||
sysAssert(allocInv(a), "rawAlloc: end listRemove test")
|
||||
sysAssert(((cast[TAddress](result) and PageMask) - smallChunkOverhead()) %%
|
||||
size == 0, "rawAlloc 21")
|
||||
|
|
@ -628,12 +628,12 @@ proc rawDealloc(a: var TMemRegion, p: pointer) =
|
|||
# check if it is not in the freeSmallChunks[s] list:
|
||||
if c.free < s:
|
||||
# add it to the freeSmallChunks[s] array:
|
||||
ListAdd(a.freeSmallChunks[s div memAlign], c)
|
||||
listAdd(a.freeSmallChunks[s div memAlign], c)
|
||||
inc(c.free, s)
|
||||
else:
|
||||
inc(c.free, s)
|
||||
if c.free == SmallChunkSize-smallChunkOverhead():
|
||||
ListRemove(a.freeSmallChunks[s div memAlign], c)
|
||||
listRemove(a.freeSmallChunks[s div memAlign], c)
|
||||
c.size = SmallChunkSize
|
||||
freeBigChunk(a, cast[PBigChunk](c))
|
||||
sysAssert(((cast[TAddress](p) and PageMask) - smallChunkOverhead()) %%
|
||||
|
|
@ -802,13 +802,13 @@ template instantiateForRegion(allocator: expr) =
|
|||
when hasThreadSupport:
|
||||
var sharedHeap: TMemRegion
|
||||
var heapLock: TSysLock
|
||||
initSysLock(HeapLock)
|
||||
initSysLock(heapLock)
|
||||
|
||||
proc allocShared(size: int): pointer =
|
||||
when hasThreadSupport:
|
||||
acquireSys(HeapLock)
|
||||
acquireSys(heapLock)
|
||||
result = alloc(sharedHeap, size)
|
||||
releaseSys(HeapLock)
|
||||
releaseSys(heapLock)
|
||||
else:
|
||||
result = alloc(size)
|
||||
|
||||
|
|
@ -818,17 +818,17 @@ template instantiateForRegion(allocator: expr) =
|
|||
|
||||
proc deallocShared(p: pointer) =
|
||||
when hasThreadSupport:
|
||||
acquireSys(HeapLock)
|
||||
acquireSys(heapLock)
|
||||
dealloc(sharedHeap, p)
|
||||
releaseSys(HeapLock)
|
||||
releaseSys(heapLock)
|
||||
else:
|
||||
dealloc(p)
|
||||
|
||||
proc reallocShared(p: pointer, newsize: int): pointer =
|
||||
when hasThreadSupport:
|
||||
acquireSys(HeapLock)
|
||||
acquireSys(heapLock)
|
||||
result = realloc(sharedHeap, p, newsize)
|
||||
releaseSys(HeapLock)
|
||||
releaseSys(heapLock)
|
||||
else:
|
||||
result = realloc(p, newsize)
|
||||
|
||||
|
|
|
|||
|
|
@ -28,8 +28,8 @@ type
|
|||
final, incompleteStruct.} = object
|
||||
C_BinaryFile {.importc: "FILE", header: "<stdio.h>",
|
||||
final, incompleteStruct.} = object
|
||||
C_TextFileStar = ptr CTextFile
|
||||
C_BinaryFileStar = ptr CBinaryFile
|
||||
C_TextFileStar = ptr C_TextFile
|
||||
C_BinaryFileStar = ptr C_BinaryFile
|
||||
|
||||
C_JmpBuf {.importc: "jmp_buf", header: "<setjmp.h>".} = object
|
||||
|
||||
|
|
@ -43,11 +43,11 @@ when not defined(SIGINT):
|
|||
when NoFakeVars:
|
||||
when defined(windows):
|
||||
const
|
||||
SIGABRT = cint(22)
|
||||
SIGFPE = cint(8)
|
||||
SIGILL = cint(4)
|
||||
SIGINT = cint(2)
|
||||
SIGSEGV = cint(11)
|
||||
SIGABRT = cint(22)
|
||||
SIGFPE = cint(8)
|
||||
SIGILL = cint(4)
|
||||
SIGINT = cint(2)
|
||||
SIGSEGV = cint(11)
|
||||
SIGTERM = cint(15)
|
||||
elif defined(macosx) or defined(linux):
|
||||
const
|
||||
|
|
|
|||
|
|
@ -7,10 +7,10 @@
|
|||
# distribution, for details about the copyright.
|
||||
#
|
||||
|
||||
proc genericResetAux(dest: Pointer, n: ptr TNimNode)
|
||||
proc genericResetAux(dest: pointer, n: ptr TNimNode)
|
||||
|
||||
proc genericAssignAux(dest, src: Pointer, mt: PNimType, shallow: bool)
|
||||
proc genericAssignAux(dest, src: Pointer, n: ptr TNimNode, shallow: bool) =
|
||||
proc genericAssignAux(dest, src: pointer, mt: PNimType, shallow: bool)
|
||||
proc genericAssignAux(dest, src: pointer, n: ptr TNimNode, shallow: bool) =
|
||||
var
|
||||
d = cast[TAddress](dest)
|
||||
s = cast[TAddress](src)
|
||||
|
|
@ -44,28 +44,28 @@ proc genericAssignAux(dest, src: Pointer, mt: PNimType, shallow: bool) =
|
|||
sysAssert(mt != nil, "genericAssignAux 2")
|
||||
case mt.Kind
|
||||
of tyString:
|
||||
var x = cast[ppointer](dest)
|
||||
var s2 = cast[ppointer](s)[]
|
||||
var x = cast[PPointer](dest)
|
||||
var s2 = cast[PPointer](s)[]
|
||||
if s2 == nil or shallow or (
|
||||
cast[PGenericSeq](s2).reserved and seqShallowFlag) != 0:
|
||||
unsureAsgnRef(x, s2)
|
||||
else:
|
||||
unsureAsgnRef(x, copyString(cast[NimString](s2)))
|
||||
of tySequence:
|
||||
var s2 = cast[ppointer](src)[]
|
||||
var s2 = cast[PPointer](src)[]
|
||||
var seq = cast[PGenericSeq](s2)
|
||||
var x = cast[ppointer](dest)
|
||||
var x = cast[PPointer](dest)
|
||||
if s2 == nil or shallow or (seq.reserved and seqShallowFlag) != 0:
|
||||
# this can happen! nil sequences are allowed
|
||||
unsureAsgnRef(x, s2)
|
||||
return
|
||||
sysAssert(dest != nil, "genericAssignAux 3")
|
||||
unsureAsgnRef(x, newSeq(mt, seq.len))
|
||||
var dst = cast[taddress](cast[ppointer](dest)[])
|
||||
var dst = cast[TAddress](cast[PPointer](dest)[])
|
||||
for i in 0..seq.len-1:
|
||||
genericAssignAux(
|
||||
cast[pointer](dst +% i*% mt.base.size +% GenericSeqSize),
|
||||
cast[pointer](cast[taddress](s2) +% i *% mt.base.size +%
|
||||
cast[pointer](cast[TAddress](s2) +% i *% mt.base.size +%
|
||||
GenericSeqSize),
|
||||
mt.Base, shallow)
|
||||
of tyObject:
|
||||
|
|
@ -83,16 +83,16 @@ proc genericAssignAux(dest, src: Pointer, mt: PNimType, shallow: bool) =
|
|||
genericAssignAux(cast[pointer](d +% i*% mt.base.size),
|
||||
cast[pointer](s +% i*% mt.base.size), mt.base, shallow)
|
||||
of tyRef:
|
||||
unsureAsgnRef(cast[ppointer](dest), cast[ppointer](s)[])
|
||||
unsureAsgnRef(cast[PPointer](dest), cast[PPointer](s)[])
|
||||
else:
|
||||
copyMem(dest, src, mt.size) # copy raw bits
|
||||
|
||||
proc genericAssign(dest, src: Pointer, mt: PNimType) {.compilerProc.} =
|
||||
proc genericAssign(dest, src: pointer, mt: PNimType) {.compilerProc.} =
|
||||
GC_disable()
|
||||
genericAssignAux(dest, src, mt, false)
|
||||
GC_enable()
|
||||
|
||||
proc genericShallowAssign(dest, src: Pointer, mt: PNimType) {.compilerProc.} =
|
||||
proc genericShallowAssign(dest, src: pointer, mt: PNimType) {.compilerProc.} =
|
||||
GC_disable()
|
||||
genericAssignAux(dest, src, mt, true)
|
||||
GC_enable()
|
||||
|
|
@ -126,7 +126,7 @@ when false:
|
|||
cprintf("%s %ld\n", k, t.size)
|
||||
debugNimType(t.base)
|
||||
|
||||
proc genericSeqAssign(dest, src: Pointer, mt: PNimType) {.compilerProc.} =
|
||||
proc genericSeqAssign(dest, src: pointer, mt: PNimType) {.compilerProc.} =
|
||||
var src = src # ugly, but I like to stress the parser sometimes :-)
|
||||
genericAssign(dest, addr(src), mt)
|
||||
|
||||
|
|
@ -139,8 +139,8 @@ proc genericAssignOpenArray(dest, src: pointer, len: int,
|
|||
genericAssign(cast[pointer](d +% i*% mt.base.size),
|
||||
cast[pointer](s +% i*% mt.base.size), mt.base)
|
||||
|
||||
proc objectInit(dest: Pointer, typ: PNimType) {.compilerProc.}
|
||||
proc objectInitAux(dest: Pointer, n: ptr TNimNode) =
|
||||
proc objectInit(dest: pointer, typ: PNimType) {.compilerProc.}
|
||||
proc objectInitAux(dest: pointer, n: ptr TNimNode) =
|
||||
var d = cast[TAddress](dest)
|
||||
case n.kind
|
||||
of nkNone: sysAssert(false, "objectInitAux")
|
||||
|
|
@ -184,7 +184,7 @@ else:
|
|||
mixin destroy
|
||||
for i in countup(0, r.len - 1): destroy(r[i])
|
||||
|
||||
proc genericReset(dest: Pointer, mt: PNimType) {.compilerProc.}
|
||||
proc genericReset(dest: pointer, mt: PNimType) {.compilerProc.}
|
||||
proc genericResetAux(dest: Pointer, n: ptr TNimNode) =
|
||||
var d = cast[TAddress](dest)
|
||||
case n.kind
|
||||
|
|
@ -202,7 +202,7 @@ proc genericReset(dest: Pointer, mt: PNimType) =
|
|||
sysAssert(mt != nil, "genericReset 2")
|
||||
case mt.Kind
|
||||
of tyString, tyRef, tySequence:
|
||||
unsureAsgnRef(cast[ppointer](dest), nil)
|
||||
unsureAsgnRef(cast[PPointer](dest), nil)
|
||||
of tyObject, tyTuple:
|
||||
# we don't need to reset m_type field for tyObject
|
||||
genericResetAux(dest, mt.node)
|
||||
|
|
|
|||
|
|
@ -115,7 +115,7 @@ proc cellSetEnlarge(t: var TCellSet) =
|
|||
var oldMax = t.max
|
||||
t.max = ((t.max+1)*2)-1
|
||||
var n = cast[PPageDescArray](alloc0((t.max + 1) * sizeof(PPageDesc)))
|
||||
for i in 0 .. oldmax:
|
||||
for i in 0 .. oldMax:
|
||||
if t.data[i] != nil:
|
||||
cellSetRawInsert(t, n, t.data[i])
|
||||
dealloc(t.data)
|
||||
|
|
@ -201,7 +201,7 @@ iterator elements(t: TCellSet): PCell {.inline.} =
|
|||
iterator elementsExcept(t, s: TCellSet): PCell {.inline.} =
|
||||
var r = t.head
|
||||
while r != nil:
|
||||
let ss = CellSetGet(s, r.key)
|
||||
let ss = cellSetGet(s, r.key)
|
||||
var i = 0
|
||||
while i <= high(r.bits):
|
||||
var w = r.bits[i]
|
||||
|
|
|
|||
|
|
@ -9,7 +9,7 @@
|
|||
|
||||
# Implementation of some runtime checks.
|
||||
|
||||
proc raiseRangeError(val: biggestInt) {.compilerproc, noreturn, noinline.} =
|
||||
proc raiseRangeError(val: BiggestInt) {.compilerproc, noreturn, noinline.} =
|
||||
when hostOs == "standalone":
|
||||
sysFatal(EOutOfRange, "value out of range")
|
||||
else:
|
||||
|
|
|
|||
|
|
@ -17,7 +17,7 @@ var
|
|||
## Unstable API.
|
||||
|
||||
when not defined(windows) or not defined(guiapp):
|
||||
proc writeToStdErr(msg: CString) = write(stdmsg, msg)
|
||||
proc writeToStdErr(msg: cstring) = write(stdmsg, msg)
|
||||
|
||||
else:
|
||||
proc MessageBoxA(hWnd: cint, lpText, lpCaption: cstring, uType: int): int32 {.
|
||||
|
|
@ -240,7 +240,7 @@ proc raiseExceptionAux(e: ref E_Base) =
|
|||
showErrorMessage(buf)
|
||||
quitOrDebug()
|
||||
|
||||
proc raiseException(e: ref E_Base, ename: CString) {.compilerRtl.} =
|
||||
proc raiseException(e: ref E_Base, ename: cstring) {.compilerRtl.} =
|
||||
e.name = ename
|
||||
when hasSomeStackTrace:
|
||||
e.trace = ""
|
||||
|
|
|
|||
|
|
@ -154,7 +154,7 @@ proc collectCT(gch: var TGcHeap)
|
|||
proc isOnStack*(p: pointer): bool {.noinline.}
|
||||
proc forAllChildren(cell: PCell, op: TWalkOp)
|
||||
proc doOperation(p: pointer, op: TWalkOp)
|
||||
proc forAllChildrenAux(dest: Pointer, mt: PNimType, op: TWalkOp)
|
||||
proc forAllChildrenAux(dest: pointer, mt: PNimType, op: TWalkOp)
|
||||
# we need the prototype here for debugging purposes
|
||||
|
||||
when hasThreadSupport and hasSharedHeap:
|
||||
|
|
@ -162,9 +162,9 @@ when hasThreadSupport and hasSharedHeap:
|
|||
template `++`(x: expr): stmt = discard atomicInc(x, rcIncrement)
|
||||
else:
|
||||
template `--`(x: expr): expr =
|
||||
Dec(x, rcIncrement)
|
||||
dec(x, rcIncrement)
|
||||
x <% rcIncrement
|
||||
template `++`(x: expr): stmt = Inc(x, rcIncrement)
|
||||
template `++`(x: expr): stmt = inc(x, rcIncrement)
|
||||
|
||||
proc prepareDealloc(cell: PCell) =
|
||||
when useMarkForDebug:
|
||||
|
|
@ -203,7 +203,7 @@ proc decRef(c: PCell) {.inline.} =
|
|||
gcAssert(c.refcount >=% rcIncrement, "decRef")
|
||||
if --c.refcount:
|
||||
rtlAddZCT(c)
|
||||
elif canBeCycleRoot(c):
|
||||
elif canbeCycleRoot(c):
|
||||
# unfortunately this is necessary here too, because a cycle might just
|
||||
# have been broken up and we could recycle it.
|
||||
rtlAddCycleRoot(c)
|
||||
|
|
@ -214,7 +214,7 @@ proc incRef(c: PCell) {.inline.} =
|
|||
c.refcount = c.refCount +% rcIncrement
|
||||
# and not colorMask
|
||||
#writeCell("incRef", c)
|
||||
if canBeCycleRoot(c):
|
||||
if canbeCycleRoot(c):
|
||||
rtlAddCycleRoot(c)
|
||||
|
||||
proc nimGCref(p: pointer) {.compilerProc, inline.} = incRef(usrToCell(p))
|
||||
|
|
@ -235,7 +235,7 @@ proc nimGCunrefNoCycle(p: pointer) {.compilerProc, inline.} =
|
|||
sysAssert(allocInv(gch.region), "end nimGCunrefNoCycle 2")
|
||||
sysAssert(allocInv(gch.region), "end nimGCunrefNoCycle 5")
|
||||
|
||||
proc asgnRef(dest: ppointer, src: pointer) {.compilerProc, inline.} =
|
||||
proc asgnRef(dest: PPointer, src: pointer) {.compilerProc, inline.} =
|
||||
# the code generator calls this proc!
|
||||
gcAssert(not isOnStack(dest), "asgnRef")
|
||||
# BUGFIX: first incRef then decRef!
|
||||
|
|
@ -243,7 +243,7 @@ proc asgnRef(dest: ppointer, src: pointer) {.compilerProc, inline.} =
|
|||
if dest[] != nil: decRef(usrToCell(dest[]))
|
||||
dest[] = src
|
||||
|
||||
proc asgnRefNoCycle(dest: ppointer, src: pointer) {.compilerProc, inline.} =
|
||||
proc asgnRefNoCycle(dest: PPointer, src: pointer) {.compilerProc, inline.} =
|
||||
# the code generator calls this proc if it is known at compile time that no
|
||||
# cycle is possible.
|
||||
if src != nil:
|
||||
|
|
@ -255,7 +255,7 @@ proc asgnRefNoCycle(dest: ppointer, src: pointer) {.compilerProc, inline.} =
|
|||
rtlAddZCT(c)
|
||||
dest[] = src
|
||||
|
||||
proc unsureAsgnRef(dest: ppointer, src: pointer) {.compilerProc.} =
|
||||
proc unsureAsgnRef(dest: PPointer, src: pointer) {.compilerProc.} =
|
||||
# unsureAsgnRef updates the reference counters only if dest is not on the
|
||||
# stack. It is used by the code generator if it cannot decide wether a
|
||||
# reference is in the stack or not (this can happen for var parameters).
|
||||
|
|
@ -318,7 +318,7 @@ proc forAllSlotsAux(dest: pointer, n: ptr TNimNode, op: TWalkOp) =
|
|||
# inlined for speed
|
||||
if n.sons[i].kind == nkSlot:
|
||||
if n.sons[i].typ.kind in {tyRef, tyString, tySequence}:
|
||||
doOperation(cast[ppointer](d +% n.sons[i].offset)[], op)
|
||||
doOperation(cast[PPointer](d +% n.sons[i].offset)[], op)
|
||||
else:
|
||||
forAllChildrenAux(cast[pointer](d +% n.sons[i].offset),
|
||||
n.sons[i].typ, op)
|
||||
|
|
@ -335,7 +335,7 @@ proc forAllChildrenAux(dest: Pointer, mt: PNimType, op: TWalkOp) =
|
|||
if ntfNoRefs notin mt.flags:
|
||||
case mt.Kind
|
||||
of tyRef, tyString, tySequence: # leaf:
|
||||
doOperation(cast[ppointer](d)[], op)
|
||||
doOperation(cast[PPointer](d)[], op)
|
||||
of tyObject, tyTuple:
|
||||
forAllSlotsAux(dest, mt.node, op)
|
||||
of tyArray, tyArrayConstr, tyOpenArray:
|
||||
|
|
@ -519,7 +519,7 @@ proc growObj(old: pointer, newsize: int, gch: var TGcHeap): pointer =
|
|||
d[j] = res
|
||||
break
|
||||
dec(j)
|
||||
if canBeCycleRoot(ol): excl(gch.cycleRoots, ol)
|
||||
if canbeCycleRoot(ol): excl(gch.cycleRoots, ol)
|
||||
when logGC:
|
||||
writeCell("growObj old cell", ol)
|
||||
writeCell("growObj new cell", res)
|
||||
|
|
@ -869,7 +869,7 @@ else:
|
|||
sp = sp +% sizeof(pointer)*8
|
||||
# last few entries:
|
||||
while sp <=% max:
|
||||
gcMark(gch, cast[ppointer](sp)[])
|
||||
gcMark(gch, cast[PPointer](sp)[])
|
||||
sp = sp +% sizeof(pointer)
|
||||
|
||||
proc markStackAndRegisters(gch: var TGcHeap) {.noinline, cdecl.} =
|
||||
|
|
@ -913,7 +913,7 @@ proc collectZCT(gch: var TGcHeap): bool =
|
|||
# In any case, it should be removed from the ZCT. But not
|
||||
# freed. **KEEP THIS IN MIND WHEN MAKING THIS INCREMENTAL!**
|
||||
when cycleGC:
|
||||
if canBeCycleRoot(c): excl(gch.cycleRoots, c)
|
||||
if canbeCycleRoot(c): excl(gch.cycleRoots, c)
|
||||
when logGC: writeCell("zct dealloc cell", c)
|
||||
gcTrace(c, csZctFreed)
|
||||
# We are about to free the object, call the finalizer BEFORE its
|
||||
|
|
|
|||
|
|
@ -69,7 +69,7 @@ type
|
|||
kind: TNimNodeKind
|
||||
offset: int
|
||||
typ: ptr TNimType
|
||||
name: Cstring
|
||||
name: cstring
|
||||
len: int
|
||||
sons: ptr array [0..0x7fff, ptr TNimNode]
|
||||
|
||||
|
|
|
|||
|
|
@ -33,7 +33,7 @@ const
|
|||
|
||||
type
|
||||
PPointer = ptr pointer
|
||||
TByteArray = array[0..1000_0000, byte]
|
||||
TByteArray = array[0..1000_0000, Byte]
|
||||
PByte = ptr TByteArray
|
||||
PString = ptr string
|
||||
|
||||
|
|
|
|||
|
|
@ -96,7 +96,7 @@ proc reprSetAux(result: var string, p: pointer, typ: PNimType) =
|
|||
of 4: u = ze64(cast[ptr int32](p)[])
|
||||
of 8: u = cast[ptr int64](p)[]
|
||||
else:
|
||||
var a = cast[pbyteArray](p)
|
||||
var a = cast[PByteArray](p)
|
||||
for i in 0 .. typ.size*8-1:
|
||||
if (ze(a[i div 8]) and (1 shl (i mod 8))) != 0:
|
||||
if elemCounter > 0: add result, ", "
|
||||
|
|
@ -129,12 +129,12 @@ when not defined(useNimRtl):
|
|||
when hasThreadSupport and hasSharedHeap and defined(heapLock):
|
||||
AcquireSys(HeapLock)
|
||||
when defined(TCellSet):
|
||||
Init(cl.marked)
|
||||
init(cl.marked)
|
||||
cl.recdepth = -1 # default is to display everything!
|
||||
cl.indent = 0
|
||||
|
||||
proc deinitReprClosure(cl: var TReprClosure) =
|
||||
when defined(TCellSet): Deinit(cl.marked)
|
||||
when defined(TCellSet): deinit(cl.marked)
|
||||
when hasThreadSupport and hasSharedHeap and defined(heapLock):
|
||||
ReleaseSys(HeapLock)
|
||||
|
||||
|
|
@ -224,17 +224,17 @@ when not defined(useNimRtl):
|
|||
reprRecord(result, p, t, cl)
|
||||
of tyRef, tyPtr:
|
||||
sysAssert(p != nil, "reprAux")
|
||||
if cast[ppointer](p)[] == nil: add result, "nil"
|
||||
else: reprRef(result, cast[ppointer](p)[], typ, cl)
|
||||
if cast[PPointer](p)[] == nil: add result, "nil"
|
||||
else: reprRef(result, cast[PPointer](p)[], typ, cl)
|
||||
of tySequence:
|
||||
reprSequence(result, cast[ppointer](p)[], typ, cl)
|
||||
reprSequence(result, cast[PPointer](p)[], typ, cl)
|
||||
of tyInt: add result, $(cast[ptr int](p)[])
|
||||
of tyInt8: add result, $int(cast[ptr Int8](p)[])
|
||||
of tyInt16: add result, $int(cast[ptr Int16](p)[])
|
||||
of tyInt32: add result, $int(cast[ptr Int32](p)[])
|
||||
of tyInt64: add result, $(cast[ptr Int64](p)[])
|
||||
of tyUInt8: add result, $ze(cast[ptr Int8](p)[])
|
||||
of tyUInt16: add result, $ze(cast[ptr Int16](p)[])
|
||||
of tyInt8: add result, $int(cast[ptr int8](p)[])
|
||||
of tyInt16: add result, $int(cast[ptr int16](p)[])
|
||||
of tyInt32: add result, $int(cast[ptr int32](p)[])
|
||||
of tyInt64: add result, $(cast[ptr int64](p)[])
|
||||
of tyUInt8: add result, $ze(cast[ptr int8](p)[])
|
||||
of tyUInt16: add result, $ze(cast[ptr int16](p)[])
|
||||
|
||||
of tyFloat: add result, $(cast[ptr float](p)[])
|
||||
of tyFloat32: add result, $(cast[ptr float32](p)[])
|
||||
|
|
@ -246,8 +246,8 @@ when not defined(useNimRtl):
|
|||
of tyCString: reprStrAux(result, $(cast[ptr cstring](p)[]))
|
||||
of tyRange: reprAux(result, p, typ.base, cl)
|
||||
of tyProc, tyPointer:
|
||||
if cast[ppointer](p)[] == nil: add result, "nil"
|
||||
else: add result, reprPointer(cast[ppointer](p)[])
|
||||
if cast[PPointer](p)[] == nil: add result, "nil"
|
||||
else: add result, reprPointer(cast[PPointer](p)[])
|
||||
else:
|
||||
add result, "(invalid data!)"
|
||||
inc(cl.recdepth)
|
||||
|
|
|
|||
|
|
@ -24,16 +24,16 @@ proc fgetc(stream: TFile): cint {.importc: "fgetc", header: "<stdio.h>",
|
|||
tags: [FReadIO].}
|
||||
proc ungetc(c: cint, f: TFile) {.importc: "ungetc", header: "<stdio.h>",
|
||||
tags: [].}
|
||||
proc putc(c: Char, stream: TFile) {.importc: "putc", header: "<stdio.h>",
|
||||
proc putc(c: char, stream: TFile) {.importc: "putc", header: "<stdio.h>",
|
||||
tags: [FWriteIO].}
|
||||
proc fprintf(f: TFile, frmt: CString) {.importc: "fprintf",
|
||||
proc fprintf(f: TFile, frmt: cstring) {.importc: "fprintf",
|
||||
header: "<stdio.h>", varargs, tags: [FWriteIO].}
|
||||
proc strlen(c: cstring): int {.
|
||||
importc: "strlen", header: "<string.h>", tags: [].}
|
||||
|
||||
|
||||
# C routine that is used here:
|
||||
proc fread(buf: Pointer, size, n: int, f: TFile): int {.
|
||||
proc fread(buf: pointer, size, n: int, f: TFile): int {.
|
||||
importc: "fread", header: "<stdio.h>", tags: [FReadIO].}
|
||||
proc fseek(f: TFile, offset: clong, whence: int): int {.
|
||||
importc: "fseek", header: "<stdio.h>", tags: [].}
|
||||
|
|
@ -48,7 +48,7 @@ proc write(f: TFile, c: cstring) = fputs(c, f)
|
|||
when NoFakeVars:
|
||||
when defined(windows):
|
||||
const
|
||||
IOFBF = cint(0)
|
||||
IOFBF = cint(0)
|
||||
IONBF = cint(4)
|
||||
elif defined(macosx) or defined(linux):
|
||||
const
|
||||
|
|
@ -179,9 +179,9 @@ proc rawEchoNL() {.inline, compilerproc.} = write(stdout, "\n")
|
|||
when defined(windows) and not defined(useWinAnsi):
|
||||
include "system/widestrs"
|
||||
|
||||
proc wfopen(filename, mode: widecstring): pointer {.
|
||||
proc wfopen(filename, mode: WideCString): pointer {.
|
||||
importc: "_wfopen", nodecl.}
|
||||
proc wfreopen(filename, mode: widecstring, stream: TFile): TFile {.
|
||||
proc wfreopen(filename, mode: WideCString, stream: TFile): TFile {.
|
||||
importc: "_wfreopen", nodecl.}
|
||||
|
||||
proc fopen(filename, mode: cstring): pointer =
|
||||
|
|
|
|||
|
|
@ -63,14 +63,14 @@ proc copyStrLast(s: NimString, start, last: int): NimString {.compilerProc.} =
|
|||
proc copyStr(s: NimString, start: int): NimString {.compilerProc.} =
|
||||
result = copyStrLast(s, start, s.len-1)
|
||||
|
||||
proc toNimStr(str: CString, len: int): NimString {.compilerProc.} =
|
||||
proc toNimStr(str: cstring, len: int): NimString {.compilerProc.} =
|
||||
result = rawNewString(len)
|
||||
result.len = len
|
||||
c_memcpy(result.data, str, (len+1) * sizeof(Char))
|
||||
#result.data[len] = '\0' # readline relies on this!
|
||||
|
||||
proc cstrToNimstr(str: CString): NimString {.compilerRtl.} =
|
||||
result = toNimstr(str, c_strlen(str))
|
||||
proc cstrToNimstr(str: cstring): NimString {.compilerRtl.} =
|
||||
result = toNimStr(str, c_strlen(str))
|
||||
|
||||
proc copyString(src: NimString): NimString {.compilerRtl.} =
|
||||
if src != nil:
|
||||
|
|
@ -98,8 +98,8 @@ proc hashString(s: string): int {.compilerproc.} =
|
|||
# the compiler needs exactly the same hash function!
|
||||
# this used to be used for efficient generation of string case statements
|
||||
var h = 0
|
||||
for i in 0..Len(s)-1:
|
||||
h = h +% Ord(s[i])
|
||||
for i in 0..len(s)-1:
|
||||
h = h +% ord(s[i])
|
||||
h = h +% h shl 10
|
||||
h = h xor (h shr 6)
|
||||
h = h +% h shl 3
|
||||
|
|
@ -150,10 +150,10 @@ proc addChar(s: NimString, c: char): NimString =
|
|||
# s = rawNewString(0);
|
||||
|
||||
proc resizeString(dest: NimString, addlen: int): NimString {.compilerRtl.} =
|
||||
if dest.len + addLen <= dest.space:
|
||||
if dest.len + addlen <= dest.space:
|
||||
result = dest
|
||||
else: # slow path:
|
||||
var sp = max(resize(dest.space), dest.len + addLen)
|
||||
var sp = max(resize(dest.space), dest.len + addlen)
|
||||
result = cast[NimString](growObj(dest, sizeof(TGenericSeq) + sp + 1))
|
||||
result.reserved = sp
|
||||
#result = rawNewString(sp)
|
||||
|
|
@ -280,7 +280,7 @@ proc nimCharToStr(x: char): string {.compilerRtl.} =
|
|||
result = newString(1)
|
||||
result[0] = x
|
||||
|
||||
proc binaryStrSearch(x: openarray[string], y: string): int {.compilerproc.} =
|
||||
proc binaryStrSearch(x: openArray[string], y: string): int {.compilerproc.} =
|
||||
var
|
||||
a = 0
|
||||
b = len(x)
|
||||
|
|
|
|||
|
|
@ -1,153 +1,153 @@
|
|||
#
|
||||
#
|
||||
# Nimrod's Runtime Library
|
||||
# (c) Copyright 2012 Andreas Rumpf
|
||||
#
|
||||
# See the file "copying.txt", included in this
|
||||
# distribution, for details about the copyright.
|
||||
#
|
||||
|
||||
## Nimrod support for C/C++'s `wide strings`:idx:. This is part of the system
|
||||
## module! Do not import it directly!
|
||||
#
|
||||
#
|
||||
# Nimrod's Runtime Library
|
||||
# (c) Copyright 2012 Andreas Rumpf
|
||||
#
|
||||
# See the file "copying.txt", included in this
|
||||
# distribution, for details about the copyright.
|
||||
#
|
||||
|
||||
## Nimrod support for C/C++'s `wide strings`:idx:. This is part of the system
|
||||
## module! Do not import it directly!
|
||||
|
||||
when not defined(NimString):
|
||||
{.error: "You must not import this module explicitly".}
|
||||
|
||||
type
|
||||
TUtf16Char* = distinct int16
|
||||
WideCString* = ref array[0.. 1_000_000, TUtf16Char]
|
||||
|
||||
proc len*(w: WideCString): int =
|
||||
## returns the length of a widestring. This traverses the whole string to
|
||||
## find the binary zero end marker!
|
||||
while int16(w[result]) != 0'i16: inc result
|
||||
|
||||
const
|
||||
UNI_REPLACEMENT_CHAR = TUtf16Char(0xFFFD'i16)
|
||||
UNI_MAX_BMP = 0x0000FFFF
|
||||
UNI_MAX_UTF16 = 0x0010FFFF
|
||||
UNI_MAX_UTF32 = 0x7FFFFFFF
|
||||
UNI_MAX_LEGAL_UTF32 = 0x0010FFFF
|
||||
|
||||
halfShift = 10
|
||||
halfBase = 0x0010000
|
||||
halfMask = 0x3FF
|
||||
|
||||
UNI_SUR_HIGH_START = 0xD800
|
||||
UNI_SUR_HIGH_END = 0xDBFF
|
||||
UNI_SUR_LOW_START = 0xDC00
|
||||
UNI_SUR_LOW_END = 0xDFFF
|
||||
|
||||
template ones(n: expr): expr = ((1 shl n)-1)
|
||||
|
||||
template fastRuneAt(s: cstring, i: int, result: expr, doInc = true) =
|
||||
## Returns the unicode character ``s[i]`` in `result`. If ``doInc == true``
|
||||
## `i` is incremented by the number of bytes that have been processed.
|
||||
bind ones
|
||||
|
||||
if ord(s[i]) <=% 127:
|
||||
result = ord(s[i])
|
||||
when doInc: inc(i)
|
||||
elif ord(s[i]) shr 5 == 0b110:
|
||||
#assert(ord(s[i+1]) shr 6 == 0b10)
|
||||
result = (ord(s[i]) and (ones(5))) shl 6 or (ord(s[i+1]) and ones(6))
|
||||
when doInc: inc(i, 2)
|
||||
elif ord(s[i]) shr 4 == 0b1110:
|
||||
#assert(ord(s[i+1]) shr 6 == 0b10)
|
||||
#assert(ord(s[i+2]) shr 6 == 0b10)
|
||||
result = (ord(s[i]) and ones(4)) shl 12 or
|
||||
(ord(s[i+1]) and ones(6)) shl 6 or
|
||||
(ord(s[i+2]) and ones(6))
|
||||
when doInc: inc(i, 3)
|
||||
elif ord(s[i]) shr 3 == 0b11110:
|
||||
#assert(ord(s[i+1]) shr 6 == 0b10)
|
||||
#assert(ord(s[i+2]) shr 6 == 0b10)
|
||||
#assert(ord(s[i+3]) shr 6 == 0b10)
|
||||
result = (ord(s[i]) and ones(3)) shl 18 or
|
||||
(ord(s[i+1]) and ones(6)) shl 12 or
|
||||
(ord(s[i+2]) and ones(6)) shl 6 or
|
||||
(ord(s[i+3]) and ones(6))
|
||||
when doInc: inc(i, 4)
|
||||
else:
|
||||
result = 0xFFFD
|
||||
when doInc: inc(i)
|
||||
|
||||
iterator runes(s: cstring): int =
|
||||
var
|
||||
i = 0
|
||||
result: int
|
||||
while s[i] != '\0':
|
||||
fastRuneAt(s, i, result, true)
|
||||
yield result
|
||||
|
||||
proc newWideCString*(source: cstring, L: int): WideCString =
|
||||
|
||||
type
|
||||
TUtf16Char* = distinct int16
|
||||
WideCString* = ref array[0.. 1_000_000, TUtf16Char]
|
||||
|
||||
proc len*(w: WideCString): int =
|
||||
## returns the length of a widestring. This traverses the whole string to
|
||||
## find the binary zero end marker!
|
||||
while int16(w[result]) != 0'i16: inc result
|
||||
|
||||
const
|
||||
UNI_REPLACEMENT_CHAR = TUtf16Char(0xFFFD'i16)
|
||||
UNI_MAX_BMP = 0x0000FFFF
|
||||
UNI_MAX_UTF16 = 0x0010FFFF
|
||||
UNI_MAX_UTF32 = 0x7FFFFFFF
|
||||
UNI_MAX_LEGAL_UTF32 = 0x0010FFFF
|
||||
|
||||
halfShift = 10
|
||||
halfBase = 0x0010000
|
||||
halfMask = 0x3FF
|
||||
|
||||
UNI_SUR_HIGH_START = 0xD800
|
||||
UNI_SUR_HIGH_END = 0xDBFF
|
||||
UNI_SUR_LOW_START = 0xDC00
|
||||
UNI_SUR_LOW_END = 0xDFFF
|
||||
|
||||
template ones(n: expr): expr = ((1 shl n)-1)
|
||||
|
||||
template fastRuneAt(s: cstring, i: int, result: expr, doInc = true) =
|
||||
## Returns the unicode character ``s[i]`` in `result`. If ``doInc == true``
|
||||
## `i` is incremented by the number of bytes that have been processed.
|
||||
bind ones
|
||||
|
||||
if ord(s[i]) <=% 127:
|
||||
result = ord(s[i])
|
||||
when doInc: inc(i)
|
||||
elif ord(s[i]) shr 5 == 0b110:
|
||||
#assert(ord(s[i+1]) shr 6 == 0b10)
|
||||
result = (ord(s[i]) and (ones(5))) shl 6 or (ord(s[i+1]) and ones(6))
|
||||
when doInc: inc(i, 2)
|
||||
elif ord(s[i]) shr 4 == 0b1110:
|
||||
#assert(ord(s[i+1]) shr 6 == 0b10)
|
||||
#assert(ord(s[i+2]) shr 6 == 0b10)
|
||||
result = (ord(s[i]) and ones(4)) shl 12 or
|
||||
(ord(s[i+1]) and ones(6)) shl 6 or
|
||||
(ord(s[i+2]) and ones(6))
|
||||
when doInc: inc(i, 3)
|
||||
elif ord(s[i]) shr 3 == 0b11110:
|
||||
#assert(ord(s[i+1]) shr 6 == 0b10)
|
||||
#assert(ord(s[i+2]) shr 6 == 0b10)
|
||||
#assert(ord(s[i+3]) shr 6 == 0b10)
|
||||
result = (ord(s[i]) and ones(3)) shl 18 or
|
||||
(ord(s[i+1]) and ones(6)) shl 12 or
|
||||
(ord(s[i+2]) and ones(6)) shl 6 or
|
||||
(ord(s[i+3]) and ones(6))
|
||||
when doInc: inc(i, 4)
|
||||
else:
|
||||
result = 0xFFFD
|
||||
when doInc: inc(i)
|
||||
|
||||
iterator runes(s: cstring): int =
|
||||
var
|
||||
i = 0
|
||||
result: int
|
||||
while s[i] != '\0':
|
||||
fastRuneAt(s, i, result, true)
|
||||
yield result
|
||||
|
||||
proc newWideCString*(source: cstring, L: int): WideCString =
|
||||
unsafeNew(result, L * 4 + 2)
|
||||
#result = cast[wideCString](alloc(L * 4 + 2))
|
||||
var d = 0
|
||||
for ch in runes(source):
|
||||
if ch <=% UNI_MAX_BMP:
|
||||
if ch >=% UNI_SUR_HIGH_START and ch <=% UNI_SUR_LOW_END:
|
||||
result[d] = UNI_REPLACEMENT_CHAR
|
||||
else:
|
||||
result[d] = TUtf16Char(toU16(ch))
|
||||
elif ch >% UNI_MAX_UTF16:
|
||||
result[d] = UNI_REPLACEMENT_CHAR
|
||||
else:
|
||||
let ch = ch -% halfBase
|
||||
result[d] = TUtf16Char(toU16((ch shr halfShift) +% UNI_SUR_HIGH_START))
|
||||
inc d
|
||||
result[d] = TUtf16Char(toU16((ch and halfMask) +% UNI_SUR_LOW_START))
|
||||
inc d
|
||||
result[d] = TUtf16Char(0'i16)
|
||||
|
||||
proc newWideCString*(s: cstring): WideCString =
|
||||
if s.isNil: return nil
|
||||
|
||||
when not defined(c_strlen):
|
||||
#result = cast[wideCString](alloc(L * 4 + 2))
|
||||
var d = 0
|
||||
for ch in runes(source):
|
||||
if ch <=% UNI_MAX_BMP:
|
||||
if ch >=% UNI_SUR_HIGH_START and ch <=% UNI_SUR_LOW_END:
|
||||
result[d] = UNI_REPLACEMENT_CHAR
|
||||
else:
|
||||
result[d] = TUtf16Char(toU16(ch))
|
||||
elif ch >% UNI_MAX_UTF16:
|
||||
result[d] = UNI_REPLACEMENT_CHAR
|
||||
else:
|
||||
let ch = ch -% halfBase
|
||||
result[d] = TUtf16Char(toU16((ch shr halfShift) +% UNI_SUR_HIGH_START))
|
||||
inc d
|
||||
result[d] = TUtf16Char(toU16((ch and halfMask) +% UNI_SUR_LOW_START))
|
||||
inc d
|
||||
result[d] = TUtf16Char(0'i16)
|
||||
|
||||
proc newWideCString*(s: cstring): WideCString =
|
||||
if s.isNil: return nil
|
||||
|
||||
when not defined(c_strlen):
|
||||
proc c_strlen(a: cstring): int {.
|
||||
header: "<string.h>", noSideEffect, importc: "strlen".}
|
||||
|
||||
let L = cstrlen(s)
|
||||
result = newWideCString(s, L)
|
||||
|
||||
proc newWideCString*(s: string): WideCString =
|
||||
result = newWideCString(s, s.len)
|
||||
|
||||
proc `$`*(w: wideCString, estimate: int): string =
|
||||
result = newStringOfCap(estimate + estimate shr 2)
|
||||
|
||||
var i = 0
|
||||
while w[i].int16 != 0'i16:
|
||||
var ch = w[i].int
|
||||
inc i
|
||||
if ch >=% UNI_SUR_HIGH_START and ch <=% UNI_SUR_HIGH_END:
|
||||
# If the 16 bits following the high surrogate are in the source buffer...
|
||||
let ch2 = w[i].int
|
||||
# If it's a low surrogate, convert to UTF32:
|
||||
if ch2 >=% UNI_SUR_LOW_START and ch2 <=% UNI_SUR_LOW_END:
|
||||
ch = ((ch -% UNI_SUR_HIGH_START) shr halfShift) +%
|
||||
(ch2 -% UNI_SUR_LOW_START) +% halfBase
|
||||
inc i
|
||||
|
||||
if ch <=% 127:
|
||||
result.add chr(ch)
|
||||
elif ch <=% 0x07FF:
|
||||
result.add chr((ch shr 6) or 0b110_00000)
|
||||
result.add chr((ch and ones(6)) or 0b10_000000)
|
||||
elif ch <=% 0xFFFF:
|
||||
result.add chr(ch shr 12 or 0b1110_0000)
|
||||
result.add chr(ch shr 6 and ones(6) or 0b10_0000_00)
|
||||
result.add chr(ch and ones(6) or 0b10_0000_00)
|
||||
elif ch <=% 0x0010FFFF:
|
||||
result.add chr(ch shr 18 or 0b1111_0000)
|
||||
result.add chr(ch shr 12 and ones(6) or 0b10_0000_00)
|
||||
result.add chr(ch shr 6 and ones(6) or 0b10_0000_00)
|
||||
result.add chr(ch and ones(6) or 0b10_0000_00)
|
||||
else:
|
||||
# replacement char:
|
||||
result.add chr(0xFFFD shr 12 or 0b1110_0000)
|
||||
result.add chr(0xFFFD shr 6 and ones(6) or 0b10_0000_00)
|
||||
result.add chr(0xFFFD and ones(6) or 0b10_0000_00)
|
||||
|
||||
proc `$`*(s: WideCString): string =
|
||||
result = s $ 80
|
||||
header: "<string.h>", noSideEffect, importc: "strlen".}
|
||||
|
||||
let L = c_strlen(s)
|
||||
result = newWideCString(s, L)
|
||||
|
||||
proc newWideCString*(s: string): WideCString =
|
||||
result = newWideCString(s, s.len)
|
||||
|
||||
proc `$`*(w: WideCString, estimate: int): string =
|
||||
result = newStringOfCap(estimate + estimate shr 2)
|
||||
|
||||
var i = 0
|
||||
while w[i].int16 != 0'i16:
|
||||
var ch = w[i].int
|
||||
inc i
|
||||
if ch >=% UNI_SUR_HIGH_START and ch <=% UNI_SUR_HIGH_END:
|
||||
# If the 16 bits following the high surrogate are in the source buffer...
|
||||
let ch2 = w[i].int
|
||||
# If it's a low surrogate, convert to UTF32:
|
||||
if ch2 >=% UNI_SUR_LOW_START and ch2 <=% UNI_SUR_LOW_END:
|
||||
ch = ((ch -% UNI_SUR_HIGH_START) shr halfShift) +%
|
||||
(ch2 -% UNI_SUR_LOW_START) +% halfBase
|
||||
inc i
|
||||
|
||||
if ch <=% 127:
|
||||
result.add chr(ch)
|
||||
elif ch <=% 0x07FF:
|
||||
result.add chr((ch shr 6) or 0b110_00000)
|
||||
result.add chr((ch and ones(6)) or 0b10_000000)
|
||||
elif ch <=% 0xFFFF:
|
||||
result.add chr(ch shr 12 or 0b1110_0000)
|
||||
result.add chr(ch shr 6 and ones(6) or 0b10_0000_00)
|
||||
result.add chr(ch and ones(6) or 0b10_0000_00)
|
||||
elif ch <=% 0x0010FFFF:
|
||||
result.add chr(ch shr 18 or 0b1111_0000)
|
||||
result.add chr(ch shr 12 and ones(6) or 0b10_0000_00)
|
||||
result.add chr(ch shr 6 and ones(6) or 0b10_0000_00)
|
||||
result.add chr(ch and ones(6) or 0b10_0000_00)
|
||||
else:
|
||||
# replacement char:
|
||||
result.add chr(0xFFFD shr 12 or 0b1110_0000)
|
||||
result.add chr(0xFFFD shr 6 and ones(6) or 0b10_0000_00)
|
||||
result.add chr(0xFFFD and ones(6) or 0b10_0000_00)
|
||||
|
||||
proc `$`*(s: WideCString): string =
|
||||
result = s $ 80
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue