bugfix: len openarray

This commit is contained in:
Andreas Rumpf
2010-03-10 16:33:55 +01:00
parent 2e280eafa8
commit 96b828a386
8 changed files with 74 additions and 13 deletions

View File

@@ -748,6 +748,24 @@ proc toBin*(x: BiggestInt, len: int): string =
shift = shift + 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 =
## Escapes a string `s`. This does these operations (at the same time):
## * replaces any ``\`` by ``\\``

View File

@@ -85,7 +85,10 @@ proc semAndEvalConstExpr(c: PContext, n: PNode): PNode =
proc semAfterMacroCall(c: PContext, n: PNode, s: PSym): PNode =
result = n
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 tyTypeDesc: result.typ = semTypeNode(c, result, nil)
else: liMessage(s.info, errInvalidParamKindX, typeToString(s.typ.sons[0]))

View File

@@ -256,6 +256,21 @@ proc leValueConv(a, b: PNode): bool =
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 =
result = nil
case n.kind
@@ -308,19 +323,16 @@ proc getConstExpr(m: PSym, n: PNode): PNode =
if not (skipTypes(n.sons[1].typ, abstractVar).kind in
{tyOpenArray, tySequence, tyString}):
result = newIntNodeT(lastOrd(skipTypes(n.sons[1].typ, abstractVar)), n)
of mLengthOpenArray:
var a = n.sons[1]
if a.kind == nkPassAsOpenArray: a = a.sons[0]
if a.kind == nkBracket:
# we can optimize it away! This fixes the bug ``len(134)``.
result = newIntNodeT(sonsLen(a), n)
else:
result = magicCall(m, n)
else:
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)
result = magicCall(m, n)
except EOverflow:
liMessage(n.info, errOverOrUnderflow)
except EDivByZero:

View 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)))

View File

@@ -27,6 +27,7 @@ tisopr.nim;falsetrue
titer2.nim;123
titer3.nim;1231
titer5.nim;abcxyz
tlenopenarray.nim;1
tlowhigh.nim;10
tmatrix.nim;111
tmultim1.nim;7
1 tack.nim 125
27 titer2.nim 123
28 titer3.nim 1231
29 titer5.nim abcxyz
30 tlenopenarray.nim 1
31 tlowhigh.nim 10
32 tmatrix.nim 111
33 tmultim1.nim 7

View File

@@ -0,0 +1,5 @@
# len(x) --> len([x]) --> match!
echo len(1_000_000) #OUT 1

View File

@@ -11,6 +11,10 @@ proc main() =
for p in split("/home/a1:xyz:/usr/bin", {':'}):
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_suffi1") == 1)

View File

@@ -35,6 +35,7 @@ Additions
- Added ``system./`` for int.
- Exported ``system.newException`` template.
- Added ``cgi.decodeData(data: string): tuple[key, value: string]``.
- Added ``strutils.insertSep``.
- Added ``math.trunc``.
- Added ``ropes`` module.
- Added ``sockets`` module.