Merge branch 'devel' of github.com:nim-lang/Nim into devel

This commit is contained in:
Araq 2017-06-16 01:48:35 +02:00
commit d947753523
15 changed files with 172 additions and 43 deletions

View file

@ -470,7 +470,12 @@ proc genRecordFieldsAux(m: BModule, n: PNode,
let a = genRecordFieldsAux(m, k, "$1.$2" % [ae, sname], rectype, let a = genRecordFieldsAux(m, k, "$1.$2" % [ae, sname], rectype,
check) check)
if a != nil: if a != nil:
add(unionBody, "struct {") if tfPacked notin rectype.flags:
add(unionBody, "struct {")
else:
addf(unionBody, CC[cCompiler].structStmtFmt,
[rope"struct", nil, rope(CC[cCompiler].packedPragma)])
add(unionBody, "{")
add(unionBody, a) add(unionBody, a)
addf(unionBody, "} $1;$n", [sname]) addf(unionBody, "} $1;$n", [sname])
else: else:

View file

@ -601,8 +601,18 @@ proc generateHeaders(m: BModule) =
addf(m.s[cfsHeaders], "#include \"$1\"$N", [rope(it)]) addf(m.s[cfsHeaders], "#include \"$1\"$N", [rope(it)])
else: else:
addf(m.s[cfsHeaders], "#include $1$N", [rope(it)]) addf(m.s[cfsHeaders], "#include $1$N", [rope(it)])
add(m.s[cfsHeaders], "#undef LANGUAGE_C" & tnl)
add(m.s[cfsHeaders], "#undef MIPSEB" & tnl)
add(m.s[cfsHeaders], "#undef MIPSEL" & tnl)
add(m.s[cfsHeaders], "#undef PPC" & tnl)
add(m.s[cfsHeaders], "#undef R3000" & tnl)
add(m.s[cfsHeaders], "#undef R4000" & tnl)
add(m.s[cfsHeaders], "#undef i386" & tnl)
add(m.s[cfsHeaders], "#undef linux" & tnl) add(m.s[cfsHeaders], "#undef linux" & tnl)
add(m.s[cfsHeaders], "#undef mips" & tnl)
add(m.s[cfsHeaders], "#undef near" & tnl) add(m.s[cfsHeaders], "#undef near" & tnl)
add(m.s[cfsHeaders], "#undef powerpc" & tnl)
add(m.s[cfsHeaders], "#undef unix" & tnl)
proc initFrame(p: BProc, procname, filename: Rope): Rope = proc initFrame(p: BProc, procname, filename: Rope): Rope =
discard cgsym(p.module, "nimFrame") discard cgsym(p.module, "nimFrame")

View file

@ -543,7 +543,7 @@ var
proc toCChar*(c: char): string = proc toCChar*(c: char): string =
case c case c
of '\0'..'\x1F', '\x80'..'\xFF': result = '\\' & toOctal(c) of '\0'..'\x1F', '\x7F'..'\xFF': result = '\\' & toOctal(c)
of '\'', '\"', '\\', '?': result = '\\' & c of '\'', '\"', '\\', '?': result = '\\' & c
else: result = $(c) else: result = $(c)

View file

@ -127,7 +127,7 @@ proc parseProtocol(protocol: string): tuple[orig: string, major, minor: int] =
i.inc protocol.parseInt(result.minor, i) i.inc protocol.parseInt(result.minor, i)
proc sendStatus(client: AsyncSocket, status: string): Future[void] = proc sendStatus(client: AsyncSocket, status: string): Future[void] =
client.send("HTTP/1.1 " & status & "\c\L") client.send("HTTP/1.1 " & status & "\c\L\c\L")
proc processClient(client: AsyncSocket, address: string, proc processClient(client: AsyncSocket, address: string,
callback: proc (request: Request): callback: proc (request: Request):

View file

@ -62,7 +62,8 @@
## let body = %*{ ## let body = %*{
## "data": "some text" ## "data": "some text"
## } ## }
## echo client.request("http://some.api", httpMethod = HttpPost, body = $body) ## let response = client.request("http://some.api", httpMethod = HttpPost, body = $body)
## echo response.status
## ##
## Progress reporting ## Progress reporting
## ================== ## ==================

View file

@ -1001,7 +1001,7 @@ proc escapeJson*(s: string; result: var string) =
result.add("\"") result.add("\"")
for x in runes(s): for x in runes(s):
var r = int(x) var r = int(x)
if r >= 32 and r <= 127: if r >= 32 and r <= 126:
var c = chr(r) var c = chr(r)
case c case c
of '"': result.add("\\\"") of '"': result.add("\\\"")

View file

@ -395,6 +395,12 @@ proc radToDeg*[T: float32|float64](d: T): T {.inline.} =
## Convert from radians to degrees ## Convert from radians to degrees
result = T(d) / RadPerDeg result = T(d) / RadPerDeg
proc sgn*[T: SomeNumber](x: T): int {.inline.} =
## Sign function. Returns -1 for negative numbers and `NegInf`, 1 for
## positive numbers and `Inf`, and 0 for positive zero, negative zero and
## `NaN`.
ord(T(0) < x) - ord(x < T(0))
proc `mod`*[T: float32|float64](x, y: T): T = proc `mod`*[T: float32|float64](x, y: T): T =
## Computes the modulo operation for float operators. Equivalent ## Computes the modulo operation for float operators. Equivalent
## to ``x - y * floor(x/y)``. Note that the remainder will always ## to ``x - y * floor(x/y)``. Note that the remainder will always
@ -447,6 +453,7 @@ when isMainModule and not defined(JS):
assert(lgamma(1.0) == 0.0) # ln(1.0) == 0.0 assert(lgamma(1.0) == 0.0) # ln(1.0) == 0.0
assert(erf(6.0) > erf(5.0)) assert(erf(6.0) > erf(5.0))
assert(erfc(6.0) < erfc(5.0)) assert(erfc(6.0) < erfc(5.0))
when isMainModule: when isMainModule:
# Function for approximate comparison of floats # Function for approximate comparison of floats
proc `==~`(x, y: float): bool = (abs(x-y) < 1e-9) proc `==~`(x, y: float): bool = (abs(x-y) < 1e-9)
@ -509,3 +516,21 @@ when isMainModule:
doAssert(classify(trunc(-1e1000000'f32)) == fcNegInf) doAssert(classify(trunc(-1e1000000'f32)) == fcNegInf)
doAssert(classify(trunc(f_nan.float32)) == fcNan) doAssert(classify(trunc(f_nan.float32)) == fcNan)
doAssert(classify(trunc(0.0'f32)) == fcZero) doAssert(classify(trunc(0.0'f32)) == fcZero)
block: # sgn() tests
assert sgn(1'i8) == 1
assert sgn(1'i16) == 1
assert sgn(1'i32) == 1
assert sgn(1'i64) == 1
assert sgn(1'u8) == 1
assert sgn(1'u16) == 1
assert sgn(1'u32) == 1
assert sgn(1'u64) == 1
assert sgn(-12342.8844'f32) == -1
assert sgn(123.9834'f64) == 1
assert sgn(0'i32) == 0
assert sgn(0'f32) == 0
assert sgn(NegInf) == -1
assert sgn(Inf) == 1
assert sgn(NaN) == 0

View file

@ -149,26 +149,41 @@ proc cmdLineRest*(p: OptParser): TaintedString {.rtl, extern: "npo$1".} =
## retrieves the rest of the command line that has not been parsed yet. ## retrieves the rest of the command line that has not been parsed yet.
result = strip(substr(p.cmd, p.pos, len(p.cmd) - 1)).TaintedString result = strip(substr(p.cmd, p.pos, len(p.cmd) - 1)).TaintedString
iterator getopt*(p: var OptParser): tuple[kind: CmdLineKind, key, val: TaintedString] =
## This is an convenience iterator for iterating over the given OptParser object.
## Example:
##
## .. code-block:: nim
## var p = initOptParser("--left --debug:3 -l=4 -r:2")
## for kind, key, val in p.getopt():
## case kind
## of cmdArgument:
## filename = key
## of cmdLongOption, cmdShortOption:
## case key
## of "help", "h": writeHelp()
## of "version", "v": writeVersion()
## of cmdEnd: assert(false) # cannot happen
## if filename == "":
## # no filename has been given, so we show the help:
## writeHelp()
p.pos = 0
while true:
next(p)
if p.kind == cmdEnd: break
yield (p.kind, p.key, p.val)
when declared(initOptParser): when declared(initOptParser):
iterator getopt*(): tuple[kind: CmdLineKind, key, val: TaintedString] = iterator getopt*(): tuple[kind: CmdLineKind, key, val: TaintedString] =
## This is an convenience iterator for iterating over the command line. ## This is an convenience iterator for iterating over the command line arguments.
## This uses the OptParser object. Example: ## This create a new OptParser object.
## See above for a more detailed example
## ##
## .. code-block:: nim ## .. code-block:: nim
## var
## filename = ""
## for kind, key, val in getopt(): ## for kind, key, val in getopt():
## case kind ## # this will iterate over all arguments passed to the cmdline.
## of cmdArgument: ## continue
## filename = key ##
## of cmdLongOption, cmdShortOption:
## case key
## of "help", "h": writeHelp()
## of "version", "v": writeVersion()
## of cmdEnd: assert(false) # cannot happen
## if filename == "":
## # no filename has been given, so we show the help:
## writeHelp()
var p = initOptParser() var p = initOptParser()
while true: while true:
next(p) next(p)

View file

@ -123,26 +123,41 @@ type
{.deprecated: [TGetoptResult: GetoptResult].} {.deprecated: [TGetoptResult: GetoptResult].}
iterator getopt*(p: var OptParser): GetoptResult =
## This is an convenience iterator for iterating over the given OptParser object.
## Example:
##
## .. code-block:: nim
## var p = initOptParser("--left --debug:3 -l=4 -r:2")
## for kind, key, val in p.getopt():
## case kind
## of cmdArgument:
## filename = key
## of cmdLongOption, cmdShortOption:
## case key
## of "help", "h": writeHelp()
## of "version", "v": writeVersion()
## of cmdEnd: assert(false) # cannot happen
## if filename == "":
## # no filename has been given, so we show the help:
## writeHelp()
p.pos = 0
while true:
next(p)
if p.kind == cmdEnd: break
yield (p.kind, p.key, p.val)
when declared(paramCount): when declared(paramCount):
iterator getopt*(): GetoptResult = iterator getopt*(): GetoptResult =
## This is an convenience iterator for iterating over the command line. ## This is an convenience iterator for iterating over the command line arguments.
## This uses the OptParser object. Example: ## This create a new OptParser object.
## See above for a more detailed example
## ##
## .. code-block:: nim ## .. code-block:: nim
## var
## filename = ""
## for kind, key, val in getopt(): ## for kind, key, val in getopt():
## case kind ## # this will iterate over all arguments passed to the cmdline.
## of cmdArgument: ## continue
## filename = key ##
## of cmdLongOption, cmdShortOption:
## case key
## of "help", "h": writeHelp()
## of "version", "v": writeVersion()
## of cmdEnd: assert(false) # cannot happen
## if filename == "":
## # no filename has been given, so we show the help:
## writeHelp()
var p = initOptParser() var p = initOptParser()
while true: while true:
next(p) next(p)

View file

@ -371,7 +371,7 @@ proc esc(c: char, reserved = {'\0'..'\255'}): string =
of '\a': result = "\\a" of '\a': result = "\\a"
of '\\': result = "\\\\" of '\\': result = "\\\\"
of 'a'..'z', 'A'..'Z', '0'..'9', '_': result = $c of 'a'..'z', 'A'..'Z', '0'..'9', '_': result = $c
elif c < ' ' or c >= '\128': result = '\\' & $ord(c) elif c < ' ' or c >= '\127': result = '\\' & $ord(c)
elif c in reserved: result = '\\' & c elif c in reserved: result = '\\' & c
else: result = $c else: result = $c

View file

@ -1643,7 +1643,7 @@ proc escape*(s: string, prefix = "\"", suffix = "\""): string {.noSideEffect,
## * replaces any ``\`` by ``\\`` ## * replaces any ``\`` by ``\\``
## * replaces any ``'`` by ``\'`` ## * replaces any ``'`` by ``\'``
## * replaces any ``"`` by ``\"`` ## * replaces any ``"`` by ``\"``
## * replaces any other character in the set ``{'\0'..'\31', '\128'..'\255'}`` ## * replaces any other character in the set ``{'\0'..'\31', '\127'..'\255'}``
## by ``\xHH`` where ``HH`` is its hexadecimal value. ## by ``\xHH`` where ``HH`` is its hexadecimal value.
## The procedure has been designed so that its output is usable for many ## The procedure has been designed so that its output is usable for many
## different common syntaxes. The resulting string is prefixed with ## different common syntaxes. The resulting string is prefixed with
@ -1653,7 +1653,7 @@ proc escape*(s: string, prefix = "\"", suffix = "\""): string {.noSideEffect,
result.add(prefix) result.add(prefix)
for c in items(s): for c in items(s):
case c case c
of '\0'..'\31', '\128'..'\255': of '\0'..'\31', '\127'..'\255':
add(result, "\\x") add(result, "\\x")
add(result, toHex(ord(c), 2)) add(result, toHex(ord(c), 2))
of '\\': add(result, "\\\\") of '\\': add(result, "\\\\")

View file

@ -3774,7 +3774,8 @@ proc locals*(): RootObj {.magic: "Plugin", noSideEffect.} =
when hasAlloc and not defined(nimscript) and not defined(JS): when hasAlloc and not defined(nimscript) and not defined(JS):
proc deepCopy*[T](x: var T, y: T) {.noSideEffect, magic: "DeepCopy".} = proc deepCopy*[T](x: var T, y: T) {.noSideEffect, magic: "DeepCopy".} =
## performs a deep copy of `x`. This is also used by the code generator ## performs a deep copy of `y` and copies it into `x`.
## This is also used by the code generator
## for the implementation of ``spawn``. ## for the implementation of ``spawn``.
discard discard

View file

@ -49,7 +49,7 @@ proc reprStrAux(result: var string, s: cstring; len: int) =
of '"': add result, "\\\"" of '"': add result, "\\\""
of '\\': add result, "\\\\" # BUGFIX: forgotten of '\\': add result, "\\\\" # BUGFIX: forgotten
of '\10': add result, "\\10\"\n\"" # " \n " # better readability of '\10': add result, "\\10\"\n\"" # " \n " # better readability
of '\128' .. '\255', '\0'..'\9', '\11'..'\31': of '\127' .. '\255', '\0'..'\9', '\11'..'\31':
add result, "\\" & reprInt(ord(c)) add result, "\\" & reprInt(ord(c))
else: else:
result.add(c) result.add(c)
@ -68,7 +68,7 @@ proc reprChar(x: char): string {.compilerRtl.} =
case x case x
of '"': add result, "\\\"" of '"': add result, "\\\""
of '\\': add result, "\\\\" of '\\': add result, "\\\\"
of '\128' .. '\255', '\0'..'\31': add result, "\\" & reprInt(ord(x)) of '\127' .. '\255', '\0'..'\31': add result, "\\" & reprInt(ord(x))
else: add result, x else: add result, x
add result, "\'" add result, "\'"

View file

@ -44,7 +44,7 @@ proc reprChar(x: char): string {.compilerRtl.} =
case x case x
of '"': add(result, "\\\"") of '"': add(result, "\\\"")
of '\\': add(result, "\\\\") of '\\': add(result, "\\\\")
of '\128'..'\255', '\0'..'\31': add( result, "\\" & reprInt(ord(x)) ) of '\127'..'\255', '\0'..'\31': add( result, "\\" & reprInt(ord(x)) )
else: add(result, x) else: add(result, x)
add(result, "\'") add(result, "\'")
@ -56,7 +56,7 @@ proc reprStrAux(result: var string, s: cstring, len: int) =
of '"': add(result, "\\\"") of '"': add(result, "\\\"")
of '\\': add(result, "\\\\") of '\\': add(result, "\\\\")
of '\10': add(result, "\\10\"\n\"") of '\10': add(result, "\\10\"\n\"")
of '\128'..'\255', '\0'..'\9', '\11'..'\31': of '\127'..'\255', '\0'..'\9', '\11'..'\31':
add( result, "\\" & reprInt(ord(c)) ) add( result, "\\" & reprInt(ord(c)) )
else: else:
add( result, reprInt(ord(c)) ) # Not sure about this. add( result, reprInt(ord(c)) ) # Not sure about this.

57
tests/misc/tparseopt.nim Normal file
View file

@ -0,0 +1,57 @@
discard """
file: "tparseopt.nim"
output: '''
parseopt
first round
kind: cmdLongOption key:val -- left:
second round
kind: cmdLongOption key:val -- left:
kind: cmdLongOption key:val -- debug:3
kind: cmdShortOption key:val -- l:4
kind: cmdShortOption key:val -- r:2
parseopt2
first round
kind: cmdLongOption key:val -- left:
second round
kind: cmdLongOption key:val -- left:
kind: cmdLongOption key:val -- debug:3
kind: cmdShortOption key:val -- l:4
kind: cmdShortOption key:val -- r:2'''
"""
from parseopt import nil
from parseopt2 import nil
block:
echo "parseopt"
for kind, key, val in parseopt.getopt():
echo "kind: ", kind, "\tkey:val -- ", key, ":", val
# pass custom cmdline arguments
echo "first round"
var argv = "--left --debug:3 -l=4 -r:2"
var p = parseopt.initOptParser(argv)
for kind, key, val in parseopt.getopt(p):
echo "kind: ", kind, "\tkey:val -- ", key, ":", val
break
# reset getopt iterator and check arguments are returned correctly.
echo "second round"
for kind, key, val in parseopt.getopt(p):
echo "kind: ", kind, "\tkey:val -- ", key, ":", val
block:
echo "parseopt2"
for kind, key, val in parseopt2.getopt():
echo "kind: ", kind, "\tkey:val -- ", key, ":", val
# pass custom cmdline arguments
echo "first round"
var argv: seq[string] = @["--left", "--debug:3", "-l=4", "-r:2"]
var p = parseopt2.initOptParser(argv)
for kind, key, val in parseopt2.getopt(p):
echo "kind: ", kind, "\tkey:val -- ", key, ":", val
break
# reset getopt iterator and check arguments are returned correctly.
echo "second round"
for kind, key, val in parseopt2.getopt(p):
echo "kind: ", kind, "\tkey:val -- ", key, ":", val