diff options
author | Andreas Rumpf <rumpf_a@web.de> | 2017-10-10 13:18:32 +0200 |
---|---|---|
committer | Andreas Rumpf <rumpf_a@web.de> | 2017-10-10 13:18:32 +0200 |
commit | 13f974336eff453fe4384ef2566f069b6fde75a1 (patch) | |
tree | b0849d565dafa26ae4d062e4bd18981c00714241 /lib | |
parent | 8bd9c7a4e6cb789cd0e37d30c4c59080744cea39 (diff) | |
parent | 91981c07bd9335dbd32afb636bd1437a588f39eb (diff) | |
download | Nim-13f974336eff453fe4384ef2566f069b6fde75a1.tar.gz |
Merge branch 'araq-stringify-array' into devel
Diffstat (limited to 'lib')
-rw-r--r-- | lib/impure/ssl.nim | 6 | ||||
-rw-r--r-- | lib/pure/nativesockets.nim | 14 | ||||
-rw-r--r-- | lib/pure/os.nim | 5 | ||||
-rw-r--r-- | lib/pure/strutils.nim | 10 | ||||
-rw-r--r-- | lib/system.nim | 45 | ||||
-rw-r--r-- | lib/system/dyncalls.nim | 5 | ||||
-rw-r--r-- | lib/system/endb.nim | 30 | ||||
-rw-r--r-- | lib/system/excpt.nim | 8 | ||||
-rw-r--r-- | lib/system/repr.nim | 19 | ||||
-rw-r--r-- | lib/system/sysstr.nim | 20 | ||||
-rw-r--r-- | lib/wrappers/openssl.nim | 18 |
11 files changed, 112 insertions, 68 deletions
diff --git a/lib/impure/ssl.nim b/lib/impure/ssl.nim index e3312d792..5b0e899f6 100644 --- a/lib/impure/ssl.nim +++ b/lib/impure/ssl.nim @@ -63,16 +63,16 @@ proc recvLine*(sock: SecureSocket, line: var TaintedString): bool = setLen(line.string, 0) while true: var c: array[0..0, char] - var n = BIO_read(sock.bio, c, c.len.cint) + var n = BIO_read(sock.bio, addr c, c.len.cint) if n <= 0: return false if c[0] == '\r': - n = BIO_read(sock.bio, c, c.len.cint) + n = BIO_read(sock.bio, addr c, c.len.cint) if n > 0 and c[0] == '\L': return true elif n <= 0: return false elif c[0] == '\L': return true - add(line.string, c) + add(line.string, c[0]) proc send*(sock: SecureSocket, data: string) = diff --git a/lib/pure/nativesockets.nim b/lib/pure/nativesockets.nim index 98fc62a3b..6c8701843 100644 --- a/lib/pure/nativesockets.nim +++ b/lib/pure/nativesockets.nim @@ -496,11 +496,12 @@ proc getLocalAddr*(socket: SocketHandle, domain: Domain): (string, Port) = addr(namelen)) == -1'i32: raiseOSError(osLastError()) # Cannot use INET6_ADDRSTRLEN here, because it's a C define. - var buf: array[64, char] + result[0] = newString(64) if inet_ntop(name.sin6_family.cint, - addr name.sin6_addr, buf.cstring, sizeof(buf).int32).isNil: + addr name.sin6_addr, addr result[0][0], (result[0].len+1).int32).isNil: raiseOSError(osLastError()) - result = ($buf, Port(nativesockets.ntohs(name.sin6_port))) + setLen(result[0], result[0].cstring.len) + result[1] = Port(nativesockets.ntohs(name.sin6_port)) else: raiseOSError(OSErrorCode(-1), "invalid socket family in getLocalAddr") @@ -532,11 +533,12 @@ proc getPeerAddr*(socket: SocketHandle, domain: Domain): (string, Port) = addr(namelen)) == -1'i32: raiseOSError(osLastError()) # Cannot use INET6_ADDRSTRLEN here, because it's a C define. - var buf: array[64, char] + result[0] = newString(64) if inet_ntop(name.sin6_family.cint, - addr name.sin6_addr, buf.cstring, sizeof(buf).int32).isNil: + addr name.sin6_addr, addr result[0][0], (result[0].len+1).int32).isNil: raiseOSError(osLastError()) - result = ($buf, Port(nativesockets.ntohs(name.sin6_port))) + setLen(result[0], result[0].cstring.len) + result[1] = Port(nativesockets.ntohs(name.sin6_port)) else: raiseOSError(OSErrorCode(-1), "invalid socket family in getLocalAddr") diff --git a/lib/pure/os.nim b/lib/pure/os.nim index b85181edf..a1ae4e250 100644 --- a/lib/pure/os.nim +++ b/lib/pure/os.nim @@ -790,7 +790,10 @@ iterator walkDir*(dir: string; relative=false): tuple[kind: PathComponent, path: while true: var x = readdir(d) if x == nil: break - var y = $x.d_name + when defined(nimNoArrayToCstringConversion): + var y = $cstring(addr x.d_name) + else: + var y = $x.d_name.cstring if y != "." and y != "..": var s: Stat if not relative: diff --git a/lib/pure/strutils.nim b/lib/pure/strutils.nim index 910d09afd..cc0f474f4 100644 --- a/lib/pure/strutils.nim +++ b/lib/pure/strutils.nim @@ -1890,11 +1890,17 @@ proc formatBiggestFloat*(f: BiggestFloat, format: FloatFormatMode = ffDefault, frmtstr[3] = '*' frmtstr[4] = floatFormatToChar[format] frmtstr[5] = '\0' - L = c_sprintf(buf, frmtstr, precision, f) + when defined(nimNoArrayToCstringConversion): + L = c_sprintf(addr buf, addr frmtstr, precision, f) + else: + L = c_sprintf(buf, frmtstr, precision, f) else: frmtstr[1] = floatFormatToChar[format] frmtstr[2] = '\0' - L = c_sprintf(buf, frmtstr, f) + when defined(nimNoArrayToCstringConversion): + L = c_sprintf(addr buf, addr frmtstr, f) + else: + L = c_sprintf(buf, frmtstr, f) result = newString(L) for i in 0 ..< L: # Depending on the locale either dot or comma is produced, diff --git a/lib/system.nim b/lib/system.nim index 207d616d2..d19a406cb 100644 --- a/lib/system.nim +++ b/lib/system.nim @@ -1875,7 +1875,7 @@ proc `$` *(x: float): string {.magic: "FloatToStr", noSideEffect.} proc `$` *(x: bool): string {.magic: "BoolToStr", noSideEffect.} ## The stringify operator for a boolean argument. Returns `x` ## converted to the string "false" or "true". - +# proc `$` *(x: char): string {.magic: "CharToStr", noSideEffect.} ## The stringify operator for a character argument. Returns `x` ## converted to a string. @@ -2437,20 +2437,28 @@ proc `$`*[T: tuple|object](x: T): string = result.add("...") result.add(")") -proc collectionToString[T: set | seq](x: T, b, e: string): string = - when x is seq: - if x.isNil: return "nil" - result = b +proc collectionToString[T](x: T, prefix, separator, suffix: string): string = + result = prefix var firstElement = true for value in items(x): - if not firstElement: result.add(", ") + if firstElement: + firstElement = false + else: + result.add(separator) + when compiles(value.isNil): - if value.isNil: result.add "nil" - else: result.add($value) + # this branch should not be necessary + if value.isNil: + result.add "nil" + else: + result.add($value) + # prevent temporary string allocation + elif compiles(result.add(value)): + result.add(value) else: result.add($value) - firstElement = false - result.add(e) + + result.add(suffix) proc `$`*[T](x: set[T]): string = ## generic ``$`` operator for sets that is lifted from the components @@ -2458,7 +2466,7 @@ proc `$`*[T](x: set[T]): string = ## ## .. code-block:: nim ## ${23, 45} == "{23, 45}" - collectionToString(x, "{", "}") + collectionToString(x, "{", ", ", "}") proc `$`*[T](x: seq[T]): string = ## generic ``$`` operator for seqs that is lifted from the components @@ -2466,13 +2474,10 @@ proc `$`*[T](x: seq[T]): string = ## ## .. code-block:: nim ## $(@[23, 45]) == "@[23, 45]" - collectionToString(x, "@[", "]") - -when false: - # causes bootstrapping to fail as we use array of chars and cstring should - # match better ... - proc `$`*[T, IDX](x: array[IDX, T]): string = - collectionToString(x, "[", "]") + if x.isNil: + "nil" + else: + collectionToString(x, "@[", ", ", "]") # ----------------- GC interface --------------------------------------------- @@ -3329,6 +3334,10 @@ elif defined(JS): include "system/sysio" +proc `$`*[T, IDX](x: array[IDX, T]): string = + ## generic ``$`` operator for arrays that is lifted from the components + collectionToString(x, "[", ", ", "]") + proc quit*(errormsg: string, errorcode = QuitFailure) {.noReturn.} = ## a shorthand for ``echo(errormsg); quit(errorcode)``. echo(errormsg) diff --git a/lib/system/dyncalls.nim b/lib/system/dyncalls.nim index 2b86ddf25..c8e251d1e 100644 --- a/lib/system/dyncalls.nim +++ b/lib/system/dyncalls.nim @@ -142,7 +142,10 @@ elif defined(windows) or defined(dos): dec(m) k = k div 10 if k == 0: break - result = getProcAddress(cast[THINSTANCE](lib), decorated) + when defined(nimNoArrayToCstringConversion): + result = getProcAddress(cast[THINSTANCE](lib), addr decorated) + else: + result = getProcAddress(cast[THINSTANCE](lib), decorated) if result != nil: return procAddrError(name) diff --git a/lib/system/endb.nim b/lib/system/endb.nim index d4d10a52c..35d8f25c4 100644 --- a/lib/system/endb.nim +++ b/lib/system/endb.nim @@ -76,10 +76,10 @@ proc `==`(a, b: StaticStr): bool = return true proc `==`(a: StaticStr, b: cstring): bool = - result = c_strcmp(a.data, b) == 0 + result = c_strcmp(unsafeAddr a.data, b) == 0 proc write(f: File, s: StaticStr) = - write(f, cstring(s.data)) + write(f, cstring(unsafeAddr s.data)) proc listBreakPoints() = write(stdout, EndbBeg) @@ -260,8 +260,8 @@ proc parseBreakpoint(s: cstring, start: int): Breakpoint = if result.high == 0: result.high = result.low i = scanFilename(s, dbgTemp, i) if dbgTemp.len != 0: - if not hasExt(dbgTemp.data): add(dbgTemp, ".nim") - result.filename = canonFilename(dbgTemp.data.cstring) + if not hasExt(addr dbgTemp.data): add(dbgTemp, ".nim") + result.filename = canonFilename(addr dbgTemp.data) if result.filename.isNil: debugOut("[Warning] no breakpoint could be set; unknown filename ") return @@ -292,12 +292,12 @@ proc dbgEvaluate(stream: File, s: cstring, start: int, f: PFrame) = i = scanAndAppendWord(s, dbgTemp, i) for i in 0 .. getGlobalLen()-1: let v = getGlobal(i) - if c_strcmp(v.name, dbgTemp.data) == 0: + if c_strcmp(v.name, addr dbgTemp.data) == 0: writeVariable(stream, v) else: for i in 0 .. f.len-1: let v = getLocal(f, i) - if c_strcmp(v.name, dbgTemp.data) == 0: + if c_strcmp(v.name, addr dbgTemp.data) == 0: writeVariable(stream, v) proc dbgOut(s: cstring, start: int, currFrame: PFrame) = @@ -306,7 +306,7 @@ proc dbgOut(s: cstring, start: int, currFrame: PFrame) = if dbgTemp.len == 0: invalidCommand() return - var stream = openAppend(dbgTemp.data) + var stream = openAppend(addr dbgTemp.data) if stream == nil: debugOut("[Warning] could not open or create file ") return @@ -320,7 +320,7 @@ proc dbgStackFrame(s: cstring, start: int, currFrame: PFrame) = # just write it to stdout: listFrame(stdout, currFrame) else: - var stream = openAppend(dbgTemp.data) + var stream = openAppend(addr dbgTemp.data) if stream == nil: debugOut("[Warning] could not open or create file ") return @@ -369,7 +369,7 @@ proc commandPrompt() = if not readLine(stdin, dbgUser): break if dbgUser.len == 0: dbgUser.len = oldLen # now look what we have to do: - var i = scanWord(dbgUser.data, dbgTemp, 0) + var i = scanWord(addr dbgUser.data, dbgTemp, 0) template `?`(x: expr): expr = dbgTemp == cstring(x) if ?"s" or ?"step": dbgState = dbStepInto @@ -400,13 +400,13 @@ proc commandPrompt() = prevState = dbgState prevSkipFrame = dbgSkipToFrame dbgState = dbSkipCurrent - dbgEvaluate(stdout, dbgUser.data, i, dbgFramePtr) + dbgEvaluate(stdout, addr dbgUser.data, i, dbgFramePtr) dbgState = prevState dbgSkipToFrame = prevSkipFrame elif ?"o" or ?"out": - dbgOut(dbgUser.data, i, dbgFramePtr) + dbgOut(addr dbgUser.data, i, dbgFramePtr) elif ?"stackframe": - dbgStackFrame(dbgUser.data, i, dbgFramePtr) + dbgStackFrame(addr dbgUser.data, i, dbgFramePtr) elif ?"w" or ?"where": dbgShowExecutionPoint() elif ?"l" or ?"locals": @@ -444,16 +444,16 @@ proc commandPrompt() = elif ?"bt" or ?"backtrace": dbgWriteStackTrace(framePtr) elif ?"b" or ?"break": - createBreakPoint(dbgUser.data, i) + createBreakPoint(addr dbgUser.data, i) elif ?"breakpoints": listBreakPoints() elif ?"toggle": - breakpointToggle(dbgUser.data, i) + breakpointToggle(addr dbgUser.data, i) elif ?"filenames": listFilenames() elif ?"maxdisplay": var parsed: int - i = scanNumber(dbgUser.data, parsed, i) + i = scanNumber(addr dbgUser.data, parsed, i) if dbgUser.data[i-1] in {'0'..'9'}: if parsed == 0: maxDisplayRecDepth = -1 else: maxDisplayRecDepth = parsed diff --git a/lib/system/excpt.nim b/lib/system/excpt.nim index cee4e33a5..950981227 100644 --- a/lib/system/excpt.nim +++ b/lib/system/excpt.nim @@ -289,8 +289,12 @@ proc raiseExceptionAux(e: ref Exception) = add(buf, " [") xadd(buf, e.name, e.name.len) add(buf, "]\n") - unhandled(buf): - showErrorMessage(buf) + when defined(nimNoArrayToCstringConversion): + template tbuf(): untyped = addr buf + else: + template tbuf(): untyped = buf + unhandled(tbuf()): + showErrorMessage(tbuf()) quitOrDebug() proc raiseException(e: ref Exception, ename: cstring) {.compilerRtl.} = diff --git a/lib/system/repr.nim b/lib/system/repr.nim index ab02c58a2..19fa564fb 100644 --- a/lib/system/repr.nim +++ b/lib/system/repr.nim @@ -16,27 +16,32 @@ proc reprInt(x: int64): string {.compilerproc.} = return $x proc reprFloat(x: float): string {.compilerproc.} = return $x proc reprPointer(x: pointer): string {.compilerproc.} = - var buf: array[0..59, char] - discard c_sprintf(buf, "%p", x) - return $buf + when defined(nimNoArrayToCstringConversion): + result = newString(60) + let n = c_sprintf(addr result[0], "%p", x) + setLen(result, n) + else: + var buf: array[0..59, char] + discard c_sprintf(buf, "%p", x) + return $buf proc `$`(x: uint64): string = if x == 0: result = "0" else: - var buf: array[60, char] + result = newString(60) var i = 0 var n = x while n != 0: let nn = n div 10'u64 - buf[i] = char(n - 10'u64 * nn + ord('0')) + result[i] = char(n - 10'u64 * nn + ord('0')) inc i n = nn + result.setLen i let half = i div 2 # Reverse - for t in 0 .. < half: swap(buf[t], buf[i-t-1]) - result = $buf + for t in 0 .. < half: swap(result[t], result[i-t-1]) proc reprStrAux(result: var string, s: cstring; len: int) = if cast[pointer](s) == nil: diff --git a/lib/system/sysstr.nim b/lib/system/sysstr.nim index 90201202c..43b5a0292 100644 --- a/lib/system/sysstr.nim +++ b/lib/system/sysstr.nim @@ -24,7 +24,10 @@ proc cmpStrings(a, b: NimString): int {.inline, compilerProc.} = if a == b: return 0 if a == nil: return -1 if b == nil: return 1 - return c_strcmp(a.data, b.data) + when defined(nimNoArrayToCstringConversion): + return c_strcmp(addr a.data, addr b.data) + else: + return c_strcmp(a.data, b.data) proc eqStrings(a, b: NimString): bool {.inline, compilerProc.} = if a == b: return true @@ -320,7 +323,10 @@ proc nimIntToStr(x: int): string {.compilerRtl.} = proc add*(result: var string; x: float) = var buf: array[0..64, char] - var n: int = c_sprintf(buf, "%.16g", x) + when defined(nimNoArrayToCstringConversion): + var n: int = c_sprintf(addr buf, "%.16g", x) + else: + var n: int = c_sprintf(buf, "%.16g", x) var hasDot = false for i in 0..n-1: if buf[i] == ',': @@ -342,7 +348,10 @@ proc add*(result: var string; x: float) = else: result.add "inf" else: - result.add buf + var i = 0 + while buf[i] != '\0': + result.add buf[i] + inc i proc nimFloatToStr(f: float): string {.compilerproc.} = result = newStringOfCap(8) @@ -507,7 +516,10 @@ proc nimParseBiggestFloat(s: string, number: var BiggestFloat, t[ti-2] = ('0'.ord + abs_exponent mod 10).char; abs_exponent = abs_exponent div 10 t[ti-3] = ('0'.ord + abs_exponent mod 10).char - number = c_strtod(t, nil) + when defined(nimNoArrayToCstringConversion): + number = c_strtod(addr t, nil) + else: + number = c_strtod(t, nil) proc nimInt64ToStr(x: int64): string {.compilerRtl.} = result = newStringOfCap(sizeof(x)*4) diff --git a/lib/wrappers/openssl.nim b/lib/wrappers/openssl.nim index 40b54b08d..5cbe2887c 100644 --- a/lib/wrappers/openssl.nim +++ b/lib/wrappers/openssl.nim @@ -590,13 +590,13 @@ proc md5_Transform*(c: var MD5_CTX; b: ptr cuchar){.importc: "MD5_Transform".} from strutils import toHex, toLowerAscii -proc hexStr (buf:cstring): string = +proc hexStr(buf: cstring): string = # turn md5s output into a nice hex str result = newStringOfCap(32) for i in 0 .. <16: result.add toHex(buf[i].ord, 2).toLowerAscii -proc md5_File* (file: string): string {.raises: [IOError,Exception].} = +proc md5_File*(file: string): string {.raises: [IOError,Exception].} = ## Generate MD5 hash for a file. Result is a 32 character # hex string with lowercase characters (like the output # of `md5sum` @@ -611,14 +611,14 @@ proc md5_File* (file: string): string {.raises: [IOError,Exception].} = while(let bytes = f.readChars(buf, 0, sz); bytes > 0): discard md5_update(ctx, buf[0].addr, bytes) - discard md5_final( buf[0].addr, ctx ) + discard md5_final(buf[0].addr, ctx) f.close - result = hexStr(buf) + result = hexStr(addr buf) -proc md5_Str*(str:string): string = - ##Generate MD5 hash for a string. Result is a 32 character - #hex string with lowercase characters +proc md5_Str*(str: string): string = + ## Generate MD5 hash for a string. Result is a 32 character + ## hex string with lowercase characters var ctx: MD5_CTX res: array[MD5_DIGEST_LENGTH,char] @@ -631,5 +631,5 @@ proc md5_Str*(str:string): string = discard md5_update(ctx, input[i].addr, L) i += L - discard md5_final(res,ctx) - result = hexStr(res) + discard md5_final(addr res, ctx) + result = hexStr(addr res) |