Skip to content

Commit

Permalink
Fix deprecated code
Browse files Browse the repository at this point in the history
  • Loading branch information
def- committed Nov 24, 2021
1 parent 235737b commit 36134a7
Show file tree
Hide file tree
Showing 21 changed files with 48 additions and 47 deletions.
10 changes: 5 additions & 5 deletions avg.nim
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ proc getc_unlocked(stream: File): cint {.importc: "getc_unlocked", header: "<std
proc ungetc(c: cint, f: File) {.importc: "ungetc", header: "<stdio.h>",
tags: [].}

proc myReadLine2(f: File, line: var TaintedString): bool =
proc myReadLine2(f: File, line: var string): bool =
# of course this could be optimized a bit; but IO is slow anyway...
# and it was difficult to get this CORRECT with Ansi C's methods
setLen(line.string, 0) # reuse the buffer!
setLen(line, 0) # reuse the buffer!
while true:
var c = getc_unlocked(f)
if c < 0'i32:
Expand All @@ -28,12 +28,12 @@ proc myReadLine2(f: File, line: var TaintedString): bool =
c = getc_unlocked(f) # is the next char LF?
if c != 10'i32: ungetc(c, f) # no, put the character back
break
add line.string, chr(int(c))
add line, chr(int(c))
result = true

proc myReadLine(f: File, line: var TaintedString): bool =
proc myReadLine(f: File, line: var string): bool =
var buf {.noinit.}: array[8192, char]
setLen(line.string, 0)
setLen(line, 0)
result = true
while true:
if fgets(cstring(addr buf), 8192, f) == nil:
Expand Down
36 changes: 18 additions & 18 deletions bufferedfile.nim
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,32 @@ const

type
BufferedFile* = object
file*: TFile
file*: File
buffer*: array[bufferSize, char] # TODO: Make this noinit
curPos*: int ## current index in buffer
bufLen*: int ## current length of buffer

proc fread(buf: pointer, size, n: int, f: TFile): int {.
importc: "fread", header: "<stdio.h>", tags: [FReadIO].}
proc fread(buf: pointer, size, n: int, f: File): int {.
importc: "fread", header: "<stdio.h>".}

# TODO: What do we do with the C internal bufSize, always set to 0?
proc open*(bf: var BufferedFile; filename: string; mode: TFileMode = fmRead;
proc open*(bf: var BufferedFile; filename: string; mode: FileMode = fmRead;
bufSize: int = - 1): bool {.tags: [], gcsafe.} =
result = bf.file.open(filename, mode, bufSize)
bf.curPos = 0
bf.bufLen = 0

proc open*(bf: var BufferedFile; filehandle: TFileHandle;
mode: TFileMode = fmRead): bool {.tags: [], gcsafe.} =
proc open*(bf: var BufferedFile; filehandle: FileHandle;
mode: FileMode = fmRead): bool {.tags: [], gcsafe.} =
result = bf.file.open(filehandle, mode)
bf.curPos = 0
bf.bufLen = 0

proc open*(filename: string, mode: TFileMode = fmRead, bufSize: int = -1):
proc open*(filename: string, mode: FileMode = fmRead, bufSize: int = -1):
BufferedFile {.tags: [], gcsafe.} =
result.file = system.open(filename, mode, bufSize)

proc buffered*(file: TFile): BufferedFile {.tags: [], gcsafe.} =
proc buffered*(file: File): BufferedFile {.tags: [], gcsafe.} =
result.file = file

proc clearBuffer(bf: var BufferedFile) =
Expand All @@ -47,21 +47,21 @@ proc readChar*(bf: var BufferedFile): char =
inc bf.curPos

proc raiseEIO(msg: string) {.noinline, noreturn.} =
raise newException(EIO, msg)
raise newException(IOError, msg)

template addUntil(i): stmt {.immediate.} =
template addUntil(i): typed =
## Helper for readLine; Adds part of a char-array to a string efficiently
let nll = ll + i - bf.curPos
line.string.setLen(nll)
copyMem(addr line.string[ll], addr bf.buffer[bf.curPos], i - bf.curPos)
line.setLen(nll)
copyMem(addr line[ll], addr bf.buffer[bf.curPos], i - bf.curPos)
ll = nll

proc readLine*(bf: var BufferedFile, line: var TaintedString): bool
{.tags: [FReadIO], gcsafe, raises: [].} =
proc readLine*(bf: var BufferedFile, line: var string): bool
{.gcsafe.} =
var
i = bf.curPos
ll = 0
line.string.setLen(ll)
line.setLen(ll)

if bf.bufLen == 0:
bf.refillBuffer
Expand All @@ -87,8 +87,8 @@ proc readLine*(bf: var BufferedFile, line: var TaintedString): bool
if bf.bufLen == 0:
return ll > 0

proc readLine*(bf: var BufferedFile): TaintedString {.tags: [FReadIO], gcsafe.} =
result = TaintedString(newStringOfCap(80))
proc readLine*(bf: var BufferedFile): string {.gcsafe.} =
result = newStringOfCap(80)
if not readLine(bf, result): raiseEIO("EOF reached")

proc readBuffer*(bf: var BufferedFile, buffer: pointer, len: int): int =
Expand All @@ -108,7 +108,7 @@ proc readChars*(bf: var BufferedFile, a: var openArray[char], start,
result = readBuffer(bf, addr(a[start]), len)

iterator lines*(bf: var BufferedFile): string =
var line = TaintedString(newStringOfCap(80))
var line = newStringOfCap(80)
while bf.readLine(line):
yield line

Expand Down
4 changes: 2 additions & 2 deletions christmas.nim
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ Twelve drummers drumming""".splitLines()
days = "first second third fourth fifth sixth seventh eighth ninth tenth eleventh twelfth".split(' ')

for n, day in days:
var g = (gifts[..n])
var g = (gifts[0..n])
reverse(g)
echo "\nOn the ", day, " day of Christmas\nMy true love gave to me:\n" &
g[.. ^2].join("\n") &
g[0 .. ^2].join("\n") &
(if n > 0: " and\n" & g[g.high] else: capitalizeAscii(g[g.high]))
2 changes: 1 addition & 1 deletion collections.nim
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
var a = [1,2,3,4,5,6,7,8,9]
var b: array[128, int]
b[9] = 10
b[..8] = a
b[0..8] = a
var c: array['a'..'d', float] = [1.0, 1.1, 1.2, 1.3]
c['b'] = 10000

Expand Down
2 changes: 1 addition & 1 deletion commondirpath.nim
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ proc commonprefix(paths: openarray[string], sep = "/"): string =

block outer:
for i in 0..paths[0].len:
result = paths[0][..i]
result = paths[0][0..i]
for path in paths:
if not path.startsWith(result):
break outer
Expand Down
2 changes: 1 addition & 1 deletion concarray.nim
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ var
b = [7,8,9,10,11]
c: array[11, int]

c[..5] = a
c[0..5] = a
c[6..10] = b
2 changes: 1 addition & 1 deletion crc32.nim
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ proc crc32FromFile*(filename: string): TCrc32 =
var buf {.noinit.}: array[bufSize, char]

while true:
var readBytes = bin.readChars(buf, 0, bufSize)
var readBytes = bin.readChars(toOpenArray(buf, 0, bufSize-1))
for i in countup(0, readBytes - 1):
updateCrc32(buf[i], result)
if readBytes != bufSize:
Expand Down
4 changes: 2 additions & 2 deletions fasta.nim
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
proc fgets(c: cstring, n: int, f: File): cstring {.
importc: "fgets", header: "<stdio.h>", tags: [ReadIOEffect].}

proc myReadLine(f: File, line: var TaintedString): bool =
proc myReadLine(f: File, line: var string): bool =
var buf {.noinit.}: array[8192, char]
setLen(line.string, 0)
setLen(line, 0)
result = true
while true:
if fgets(cstring(addr buf), 8192, f) == nil:
Expand Down
4 changes: 2 additions & 2 deletions fasta3.nim
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
proc fgets(c: cstring, n: int, f: File): cstring {.
importc: "fgets", header: "<stdio.h>", tags: [ReadIOEffect].}

proc myReadLine(f: File, line: var TaintedString): bool =
proc myReadLine(f: File, line: var string): bool =
var buf {.noinit.}: array[8192, char]
setLen(line.string, 0)
setLen(line, 0)
result = true
while true:
if fgets(cstring(addr buf), 8192, f) == nil:
Expand Down
2 changes: 1 addition & 1 deletion fourbitadder.nim
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ proc fa4(a, b: array[4, bool]): array[5, bool] =
let r = fa(a[i], b[i], if i > 0: co[i-1] else: false)
s[i] = r[0]
co[i] = r[1]
result[..3] = s
result[0..3] = s
result[4] = co[3]

proc int2bus(n: int): array[4, bool] =
Expand Down
2 changes: 1 addition & 1 deletion hailstone.nim
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ proc hailstone*(n: int): seq[int] =

when isMainModule:
let h = hailstone 27
assert h.len == 112 and h[..3] == @[27,82,41,124] and h[h.high-3..h.high] == @[8,4,2,1]
assert h.len == 112 and h[0..3] == @[27,82,41,124] and h[h.high-3..h.high] == @[8,4,2,1]
var m, mi = 0
for i in 1 ..< 100_000:
let n = hailstone(i).len
Expand Down
2 changes: 1 addition & 1 deletion hofstadterconway.nim
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import strutils
const last = 1 shl 20

var aList: array[last + 1, int]
aList[..2] = [-50_000, 1, 1]
aList[0..2] = [-50_000, 1, 1]
var
v = aList[2]
k1 = 2
Expand Down
4 changes: 2 additions & 2 deletions hofstadterq.nim
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
var q = @[1, 1]
for n in 2 ..< 100_000: q.add q[n-q[n-1]] + q[n-q[n-2]]

echo q[..9]
assert q[..9] == @[1, 1, 2, 3, 3, 4, 5, 5, 6, 6]
echo q[0..9]
assert q[0..9] == @[1, 1, 2, 3, 3, 4, 5, 5, 6, 6]

echo q[999]
assert q[999] == 502
Expand Down
4 changes: 2 additions & 2 deletions iban.nim
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ proc validIban(iban: string): bool =
return false

# Validate country code against expected length
if iban.len != countryLen[iban[..1]]:
if iban.len != countryLen[iban[0..1]]:
return false

# Shift and convert
iban = iban[4..iban.high] & iban[..3]
iban = iban[4..iban.high] & iban[0..3]
var digits = ""
for ch in iban:
case ch
Expand Down
3 changes: 2 additions & 1 deletion lastfriday.nim
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import times, os, strutils
let year = paramStr(1).parseInt
for month in mJan .. mDec:
for day in countdown(getDaysInMonth(month, year), 1):
let date = initDateTime(day, month, year, 0, 0, 0, utc())

let date = dateTime(year, month, day, 0, 0, 0, 0, utc())
if date.weekday == dFri:
echo date.format "yyyy-MM-dd"
break
2 changes: 1 addition & 1 deletion longmultiplication.nim
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,6 @@ proc longmulti(a, b: string): string =
result[k] = chr(ord(result[k]) + carry)

if result[0] == '0':
result[.. ^2] = result[1.. ^1]
result[0.. ^2] = result[1.. ^1]

echo longmulti("-18446744073709551616", "-18446744073709551616")
2 changes: 1 addition & 1 deletion multiplicativeroot.nim
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ for n in 0..int.high:
table[mdroot(n).mdr].add n

for mp, val in table:
echo mp,": ",val[..4]
echo mp,": ",val[0..4]
2 changes: 1 addition & 1 deletion noncontsub.nim
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ proc ncsub[T](se: seq[T], s = 0): seq[seq[T]] =
result = @[]
if se.len > 0:
let
x = se[..0]
x = se[0..0]
xs = se[1 .. ^1]
p2 = s mod 2
p1 = (s + 1) mod 2
Expand Down
2 changes: 1 addition & 1 deletion slice.nim
Original file line number Diff line number Diff line change
@@ -1 +1 @@
echo "foo"[..1]
echo "foo"[0..1]
2 changes: 1 addition & 1 deletion trabb_pardo_knuth.nim
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import math, rdstdin, strutils, sequtils, algorithm
proc f(x: float): float = x.abs.pow(0.5) + 5 * x.pow(3)

proc ask: seq[float] =
readLineFromStdin("\n11 numbers: ").strip.split[..10].map(parseFloat)
readLineFromStdin("\n11 numbers: ").strip.split[0..10].map(parseFloat)

var s = ask()
reverse s
Expand Down
2 changes: 1 addition & 1 deletion truncatableprimes.nim
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ proc truncatablePrime(n: int64): tuple[left: int64, right: int64] =
for n in primelist:
var alltruncs = initHashSet[string]()
for i in 0..n.high:
alltruncs.incl n[..i]
alltruncs.incl n[0..i]
if alltruncs <= primeset:
result.right = parseInt(n)
break
Expand Down

0 comments on commit 36134a7

Please sign in to comment.