bugfix: len openarray
This commit is contained in:
parent
2e280eafa8
commit
96b828a386
8 changed files with 74 additions and 13 deletions
|
|
@ -748,6 +748,24 @@ proc toBin*(x: BiggestInt, len: int): string =
|
||||||
shift = shift + 1
|
shift = shift + 1
|
||||||
mask = mask shl 1
|
mask = mask shl 1
|
||||||
|
|
||||||
|
proc insertSep*(s: string, sep = '_', digits = 3): string =
|
||||||
|
## inserts the separator `sep` after `digits` digits from right to left.
|
||||||
|
## Even though the algorithm works with any string `s`, it is only useful
|
||||||
|
## if `s` contains a number.
|
||||||
|
## Example: ``insertSep("1000000") == "1_000_000"``
|
||||||
|
var L = (s.len-1) div digits + s.len
|
||||||
|
result = newString(L)
|
||||||
|
var j = 0
|
||||||
|
dec(L)
|
||||||
|
for i in countdown(len(s)-1, 0):
|
||||||
|
if j == digits:
|
||||||
|
result[L] = sep
|
||||||
|
dec(L)
|
||||||
|
j = 0
|
||||||
|
result[L] = s[i]
|
||||||
|
inc(j)
|
||||||
|
dec(L)
|
||||||
|
|
||||||
proc escape*(s: string, prefix = "\"", suffix = "\""): string =
|
proc escape*(s: string, prefix = "\"", suffix = "\""): string =
|
||||||
## Escapes a string `s`. This does these operations (at the same time):
|
## Escapes a string `s`. This does these operations (at the same time):
|
||||||
## * replaces any ``\`` by ``\\``
|
## * replaces any ``\`` by ``\\``
|
||||||
|
|
|
||||||
|
|
@ -85,7 +85,10 @@ proc semAndEvalConstExpr(c: PContext, n: PNode): PNode =
|
||||||
proc semAfterMacroCall(c: PContext, n: PNode, s: PSym): PNode =
|
proc semAfterMacroCall(c: PContext, n: PNode, s: PSym): PNode =
|
||||||
result = n
|
result = n
|
||||||
case s.typ.sons[0].kind
|
case s.typ.sons[0].kind
|
||||||
of tyExpr: result = semExprWithType(c, result)
|
of tyExpr:
|
||||||
|
# BUGFIX: we cannot expect a type here, because module aliases would not
|
||||||
|
# work then (see the ``tmodulealias`` test)
|
||||||
|
result = semExpr(c, result) # semExprWithType(c, result)
|
||||||
of tyStmt: result = semStmt(c, result)
|
of tyStmt: result = semStmt(c, result)
|
||||||
of tyTypeDesc: result.typ = semTypeNode(c, result, nil)
|
of tyTypeDesc: result.typ = semTypeNode(c, result, nil)
|
||||||
else: liMessage(s.info, errInvalidParamKindX, typeToString(s.typ.sons[0]))
|
else: liMessage(s.info, errInvalidParamKindX, typeToString(s.typ.sons[0]))
|
||||||
|
|
|
||||||
|
|
@ -256,6 +256,21 @@ proc leValueConv(a, b: PNode): bool =
|
||||||
else: InternalError(a.info, "leValueConv")
|
else: InternalError(a.info, "leValueConv")
|
||||||
else: InternalError(a.info, "leValueConv")
|
else: InternalError(a.info, "leValueConv")
|
||||||
|
|
||||||
|
proc magicCall(m: PSym, n: PNode): PNode =
|
||||||
|
var s = n.sons[0].sym
|
||||||
|
var a = getConstExpr(m, n.sons[1])
|
||||||
|
var b, c: PNode
|
||||||
|
if a == nil: return
|
||||||
|
if sonsLen(n) > 2:
|
||||||
|
b = getConstExpr(m, n.sons[2])
|
||||||
|
if b == nil: return
|
||||||
|
if sonsLen(n) > 3:
|
||||||
|
c = getConstExpr(m, n.sons[3])
|
||||||
|
if c == nil: return
|
||||||
|
else:
|
||||||
|
b = nil
|
||||||
|
result = evalOp(s.magic, n, a, b, c)
|
||||||
|
|
||||||
proc getConstExpr(m: PSym, n: PNode): PNode =
|
proc getConstExpr(m: PSym, n: PNode): PNode =
|
||||||
result = nil
|
result = nil
|
||||||
case n.kind
|
case n.kind
|
||||||
|
|
@ -308,19 +323,16 @@ proc getConstExpr(m: PSym, n: PNode): PNode =
|
||||||
if not (skipTypes(n.sons[1].typ, abstractVar).kind in
|
if not (skipTypes(n.sons[1].typ, abstractVar).kind in
|
||||||
{tyOpenArray, tySequence, tyString}):
|
{tyOpenArray, tySequence, tyString}):
|
||||||
result = newIntNodeT(lastOrd(skipTypes(n.sons[1].typ, abstractVar)), n)
|
result = newIntNodeT(lastOrd(skipTypes(n.sons[1].typ, abstractVar)), n)
|
||||||
else:
|
of mLengthOpenArray:
|
||||||
var a = getConstExpr(m, n.sons[1])
|
var a = n.sons[1]
|
||||||
var b, c: PNode
|
if a.kind == nkPassAsOpenArray: a = a.sons[0]
|
||||||
if a == nil: return
|
if a.kind == nkBracket:
|
||||||
if sonsLen(n) > 2:
|
# we can optimize it away! This fixes the bug ``len(134)``.
|
||||||
b = getConstExpr(m, n.sons[2])
|
result = newIntNodeT(sonsLen(a), n)
|
||||||
if b == nil: return
|
|
||||||
if sonsLen(n) > 3:
|
|
||||||
c = getConstExpr(m, n.sons[3])
|
|
||||||
if c == nil: return
|
|
||||||
else:
|
else:
|
||||||
b = nil
|
result = magicCall(m, n)
|
||||||
result = evalOp(s.magic, n, a, b, c)
|
else:
|
||||||
|
result = magicCall(m, n)
|
||||||
except EOverflow:
|
except EOverflow:
|
||||||
liMessage(n.info, errOverOrUnderflow)
|
liMessage(n.info, errOverOrUnderflow)
|
||||||
except EDivByZero:
|
except EDivByZero:
|
||||||
|
|
|
||||||
17
tests/accept/compile/tmodulealias.nim
Executable file
17
tests/accept/compile/tmodulealias.nim
Executable file
|
|
@ -0,0 +1,17 @@
|
||||||
|
|
||||||
|
|
||||||
|
when defined(windows):
|
||||||
|
import winlean
|
||||||
|
else:
|
||||||
|
import posix
|
||||||
|
|
||||||
|
when defined(Windows):
|
||||||
|
template orig: expr =
|
||||||
|
winlean
|
||||||
|
else:
|
||||||
|
template orig: expr =
|
||||||
|
posix
|
||||||
|
|
||||||
|
proc socket(domain, typ, protocol: int): int =
|
||||||
|
result = orig.socket(ord(domain), ord(typ), ord(protocol)))
|
||||||
|
|
||||||
|
|
@ -27,6 +27,7 @@ tisopr.nim;falsetrue
|
||||||
titer2.nim;123
|
titer2.nim;123
|
||||||
titer3.nim;1231
|
titer3.nim;1231
|
||||||
titer5.nim;abcxyz
|
titer5.nim;abcxyz
|
||||||
|
tlenopenarray.nim;1
|
||||||
tlowhigh.nim;10
|
tlowhigh.nim;10
|
||||||
tmatrix.nim;111
|
tmatrix.nim;111
|
||||||
tmultim1.nim;7
|
tmultim1.nim;7
|
||||||
|
|
|
||||||
|
5
tests/accept/run/tlenopenarray.nim
Executable file
5
tests/accept/run/tlenopenarray.nim
Executable file
|
|
@ -0,0 +1,5 @@
|
||||||
|
|
||||||
|
# len(x) --> len([x]) --> match!
|
||||||
|
echo len(1_000_000) #OUT 1
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -11,6 +11,10 @@ proc main() =
|
||||||
for p in split("/home/a1:xyz:/usr/bin", {':'}):
|
for p in split("/home/a1:xyz:/usr/bin", {':'}):
|
||||||
write(stdout, p)
|
write(stdout, p)
|
||||||
|
|
||||||
|
assert(insertSep($1000_000) == "1_000_000")
|
||||||
|
assert(insertSep($232) == "232")
|
||||||
|
assert(insertSep($12345, ',') == "12,345")
|
||||||
|
assert(insertSep($0) == "0")
|
||||||
|
|
||||||
assert(editDistance("prefix__hallo_suffix", "prefix__hallo_suffix") == 0)
|
assert(editDistance("prefix__hallo_suffix", "prefix__hallo_suffix") == 0)
|
||||||
assert(editDistance("prefix__hallo_suffix", "prefix__hallo_suffi1") == 1)
|
assert(editDistance("prefix__hallo_suffix", "prefix__hallo_suffi1") == 1)
|
||||||
|
|
|
||||||
|
|
@ -35,6 +35,7 @@ Additions
|
||||||
- Added ``system./`` for int.
|
- Added ``system./`` for int.
|
||||||
- Exported ``system.newException`` template.
|
- Exported ``system.newException`` template.
|
||||||
- Added ``cgi.decodeData(data: string): tuple[key, value: string]``.
|
- Added ``cgi.decodeData(data: string): tuple[key, value: string]``.
|
||||||
|
- Added ``strutils.insertSep``.
|
||||||
- Added ``math.trunc``.
|
- Added ``math.trunc``.
|
||||||
- Added ``ropes`` module.
|
- Added ``ropes`` module.
|
||||||
- Added ``sockets`` module.
|
- Added ``sockets`` module.
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue