From 612e603180dd16a928b78d819f9a53cdaf0513dc Mon Sep 17 00:00:00 2001 From: Timothee Cour Date: Wed, 18 Nov 2020 20:53:03 -0800 Subject: [PATCH 1/3] add simpler to use readChars overload --- changelog.md | 2 ++ lib/system/io.nim | 12 ++++++++---- tests/stdlib/tio.nim | 37 +++++++++++++++++++++++++++++++++++++ 3 files changed, 47 insertions(+), 4 deletions(-) create mode 100644 tests/stdlib/tio.nim diff --git a/changelog.md b/changelog.md index d53f5b0381cc7..43d34338af93c 100644 --- a/changelog.md +++ b/changelog.md @@ -45,6 +45,8 @@ - `os.FileInfo` (returned by `getFileInfo`) now contains `blockSize`, determining preferred I/O block size for this file object. +- Added a simpler to use `io.readChars` overload. + ## Language changes - `nimscript` now handles `except Exception as e` diff --git a/lib/system/io.nim b/lib/system/io.nim index c3b2976fbd3a8..4aa5a52eb286a 100644 --- a/lib/system/io.nim +++ b/lib/system/io.nim @@ -174,14 +174,18 @@ proc readBytes*(f: File, a: var openArray[int8|uint8], start, len: Natural): int ## `len` (if not as many bytes are remaining), but not greater. result = readBuffer(f, addr(a[start]), len) +proc readChars*(f: File, a: var openArray[char]): int {.tags: [ReadIOEffect], benign.} = + ## reads up to `a.len` bytes into the buffer `a`. Returns + ## the actual number of bytes that have been read which may be less than + ## `a.len` (if not as many bytes are remaining), but not greater. + result = readBuffer(f, addr(a[0]), a.len) + proc readChars*(f: File, a: var openArray[char], start, len: Natural): int {. - tags: [ReadIOEffect], benign.} = + tags: [ReadIOEffect], benign, deprecated: + "use other `readChars` overload, possibly via: readChars(toOpenArray(buf, start, len-1))".} = ## reads `len` bytes into the buffer `a` starting at ``a[start]``. Returns ## the actual number of bytes that have been read which may be less than ## `len` (if not as many bytes are remaining), but not greater. - ## - ## **Warning:** The buffer `a` must be pre-allocated. This can be done - ## using, for example, ``newString``. if (start + len) > len(a): raiseEIO("buffer overflow: (start+len) > length of openarray buffer") result = readBuffer(f, addr(a[start]), len) diff --git a/tests/stdlib/tio.nim b/tests/stdlib/tio.nim new file mode 100644 index 0000000000000..50be625314885 --- /dev/null +++ b/tests/stdlib/tio.nim @@ -0,0 +1,37 @@ +# xxx move to here other tests that belong here; io is a proper module + +import std/os + +block: # readChars + let dir = getTempDir() + let file = dir / "D20201118T205105.txt" + let s = "he\0l\0lo" + writeFile(file, s) + defer: removeFile(file) + let f = open(file) + defer: close(f) + let n = f.getFileInfo.blockSize + var buf = newString(n) + template fn = + let n2 = f.readChars(buf) + doAssert n2 == s.len + doAssert buf[0.. Date: Wed, 18 Nov 2020 20:57:36 -0800 Subject: [PATCH 2/3] use new readChars overload --- lib/std/sha1.nim | 2 +- lib/wrappers/openssl.nim | 2 +- tests/stdlib/tio.nim | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/std/sha1.nim b/lib/std/sha1.nim index 9763b286e5f66..31a3f4b9650a4 100644 --- a/lib/std/sha1.nim +++ b/lib/std/sha1.nim @@ -219,7 +219,7 @@ proc secureHashFile*(filename: string): SecureHash = var state = newSha1State() var buffer = newString(BufferLength) while true: - let length = readChars(f, buffer, 0, BufferLength) + let length = readChars(f, buffer) if length == 0: break buffer.setLen(length) diff --git a/lib/wrappers/openssl.nim b/lib/wrappers/openssl.nim index 694152db1a0de..3917753f58479 100644 --- a/lib/wrappers/openssl.nim +++ b/lib/wrappers/openssl.nim @@ -760,7 +760,7 @@ proc md5_File*(file: string): string {.raises: [IOError,Exception].} = ctx: MD5_CTX discard md5_Init(ctx) - while(let bytes = f.readChars(buf, 0, sz); bytes > 0): + while(let bytes = f.readChars(buf); bytes > 0): discard md5_Update(ctx, buf[0].addr, cast[csize_t](bytes)) discard md5_Final(buf[0].addr, ctx) diff --git a/tests/stdlib/tio.nim b/tests/stdlib/tio.nim index 50be625314885..0da64f9c26de8 100644 --- a/tests/stdlib/tio.nim +++ b/tests/stdlib/tio.nim @@ -1,10 +1,10 @@ # xxx move to here other tests that belong here; io is a proper module import std/os +from stdtest/specialpaths import buildDir block: # readChars - let dir = getTempDir() - let file = dir / "D20201118T205105.txt" + let file = buildDir / "D20201118T205105.txt" let s = "he\0l\0lo" writeFile(file, s) defer: removeFile(file) From c9acde74da89b67a7dd8549b9a1b3e2b0b26cb2f Mon Sep 17 00:00:00 2001 From: Andreas Rumpf Date: Thu, 19 Nov 2020 15:02:49 +0100 Subject: [PATCH 3/3] Update lib/wrappers/openssl.nim --- lib/wrappers/openssl.nim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/wrappers/openssl.nim b/lib/wrappers/openssl.nim index 3917753f58479..a2453dd400f41 100644 --- a/lib/wrappers/openssl.nim +++ b/lib/wrappers/openssl.nim @@ -760,7 +760,7 @@ proc md5_File*(file: string): string {.raises: [IOError,Exception].} = ctx: MD5_CTX discard md5_Init(ctx) - while(let bytes = f.readChars(buf); bytes > 0): + while (let bytes = f.readChars(buf); bytes > 0): discard md5_Update(ctx, buf[0].addr, cast[csize_t](bytes)) discard md5_Final(buf[0].addr, ctx)