Skip to content

Commit 0fad659

Browse files
committed
make more tests green
1 parent f0eb350 commit 0fad659

File tree

9 files changed

+36
-33
lines changed

9 files changed

+36
-33
lines changed

changelog.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,9 @@
8383

8484
- ``nil`` for strings/seqs is finally gone. Instead the default value for
8585
these is ``"" / @[]``.
86+
- Accessing the binary zero terminator in Nim's native strings
87+
is now invalid. Internally a Nim string still has the trailing zero for
88+
zero-copy interoperability with ``cstring``.
8689

8790
### Tool changes
8891

lib/pure/base64.nim

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ template encodeInternal(s: typed, lineLen: int, newLine: string): untyped =
5252
if numLines > 0: inc(total, (numLines - 1) * newLine.len)
5353

5454
result = newString(total)
55-
var
55+
var
5656
i = 0
5757
r = 0
5858
currLine = 0
@@ -76,7 +76,7 @@ template encodeInternal(s: typed, lineLen: int, newLine: string): untyped =
7676
currLine = 0
7777

7878
if i < s.len-1:
79-
let
79+
let
8080
a = ord(s[i])
8181
b = ord(s[i+1])
8282
result[r] = cb64[a shr 2]
@@ -130,11 +130,11 @@ proc decode*(s: string): string =
130130
# total is an upper bound, as we will skip arbitrary whitespace:
131131
result = newString(total)
132132

133-
var
133+
var
134134
i = 0
135135
r = 0
136136
while true:
137-
while s[i] in Whitespace: inc(i)
137+
while i < s.len and s[i] in Whitespace: inc(i)
138138
if i < s.len-3:
139139
let
140140
a = s[i].decodeByte

lib/pure/cookies.nim

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,16 @@ proc parseCookies*(s: string): StringTableRef =
2525
result = newStringTable(modeCaseInsensitive)
2626
var i = 0
2727
while true:
28-
while s[i] == ' ' or s[i] == '\t': inc(i)
28+
while i < s.len and (s[i] == ' ' or s[i] == '\t'): inc(i)
2929
var keystart = i
30-
while s[i] != '=' and s[i] != '\0': inc(i)
30+
while i < s.len and s[i] != '=': inc(i)
3131
var keyend = i-1
32-
if s[i] == '\0': break
32+
if i >= s.len: break
3333
inc(i) # skip '='
3434
var valstart = i
35-
while s[i] != ';' and s[i] != '\0': inc(i)
35+
while i < s.len and s[i] != ';': inc(i)
3636
result[substr(s, keystart, keyend)] = substr(s, valstart, i-1)
37-
if s[i] == '\0': break
37+
if i >= s.len: break
3838
inc(i) # skip ';'
3939

4040
proc setCookie*(key, value: string, domain = "", path = "",

lib/pure/httpcore.nim

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,11 +190,11 @@ proc len*(headers: HttpHeaders): int = return headers.table.len
190190
proc parseList(line: string, list: var seq[string], start: int): int =
191191
var i = 0
192192
var current = ""
193-
while line[start + i] notin {'\c', '\l', '\0'}:
193+
while start+i < line.len and line[start + i] notin {'\c', '\l'}:
194194
i += line.skipWhitespace(start + i)
195195
i += line.parseUntil(current, {'\c', '\l', ','}, start + i)
196196
list.add(current)
197-
if line[start + i] == ',':
197+
if start+i < line.len and line[start + i] == ',':
198198
i.inc # Skip ,
199199
current.setLen(0)
200200

lib/pure/httpserver.nim

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ when false:
126126
var dataAvail = false
127127
while dataAvail:
128128
dataAvail = recvLine(client, buf) # TODO: This is incorrect.
129-
var L = toLower(buf.string)
129+
var L = toLowerAscii(buf.string)
130130
if L.startsWith("content-length:"):
131131
var i = len("content-length:")
132132
while L[i] in Whitespace: inc(i)
@@ -199,7 +199,7 @@ when false:
199199
notFound(client)
200200
else:
201201
when defined(Windows):
202-
var ext = splitFile(path).ext.toLower
202+
var ext = splitFile(path).ext.toLowerAscii
203203
if ext == ".exe" or ext == ".cgi":
204204
# XXX: extract interpreter information here?
205205
cgi = true
@@ -303,7 +303,7 @@ proc next*(s: var Server) =
303303
if s.reqMethod == "POST":
304304
# Check for Expect header
305305
if s.headers.hasKey("Expect"):
306-
if s.headers["Expect"].toLower == "100-continue":
306+
if s.headers["Expect"].toLowerAscii == "100-continue":
307307
s.client.sendStatus("100 Continue")
308308
else:
309309
s.client.sendStatus("417 Expectation Failed")
@@ -427,7 +427,7 @@ proc nextAsync(s: PAsyncHTTPServer) =
427427
if s.reqMethod == "POST":
428428
# Check for Expect header
429429
if s.headers.hasKey("Expect"):
430-
if s.headers["Expect"].toLower == "100-continue":
430+
if s.headers["Expect"].toLowerAscii == "100-continue":
431431
s.client.sendStatus("100 Continue")
432432
else:
433433
s.client.sendStatus("417 Expectation Failed")

lib/pure/matchers.nim

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,21 +29,21 @@ proc validEmailAddress*(s: string): bool {.noSideEffect,
2929
chars = Letters + Digits + {'!','#','$','%','&',
3030
'\'','*','+','/','=','?','^','_','`','{','}','|','~','-','.'}
3131
var i = 0
32-
if s[i] notin chars or s[i] == '.': return false
33-
while s[i] in chars:
34-
if s[i] == '.' and s[i+1] == '.': return false
32+
if i >= s.len or s[i] notin chars or s[i] == '.': return false
33+
while i < s.len and s[i] in chars:
34+
if i+1 < s.len and s[i] == '.' and s[i+1] == '.': return false
3535
inc(i)
36-
if s[i] != '@': return false
36+
if i >= s.len or s[i] != '@': return false
3737
var j = len(s)-1
38-
if s[j] notin Letters: return false
38+
if j >= 0 and s[j] notin Letters: return false
3939
while j >= i and s[j] in Letters: dec(j)
4040
inc(i) # skip '@'
41-
while s[i] in {'0'..'9', 'a'..'z', '-', '.'}: inc(i)
42-
if s[i] != '\0': return false
41+
while i < s.len and s[i] in {'0'..'9', 'a'..'z', '-', '.'}: inc(i)
42+
if i != s.len: return false
4343

4444
var x = substr(s, j+1)
4545
if len(x) == 2 and x[0] in Letters and x[1] in Letters: return true
46-
case toLower(x)
46+
case toLowerAscii(x)
4747
of "com", "org", "net", "gov", "mil", "biz", "info", "mobi", "name",
4848
"aero", "jobs", "museum": return true
4949
else: return false

lib/pure/strutils.nim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1126,7 +1126,7 @@ proc unindent*(s: string, count: Natural, padding: string = " "): string
11261126
var indentCount = 0
11271127
for j in 0..<count.int:
11281128
indentCount.inc
1129-
if line[j .. j + padding.len-1] != padding:
1129+
if j + padding.len-1 >= line.len or line[j .. j + padding.len-1] != padding:
11301130
indentCount = j
11311131
break
11321132
result.add(line[indentCount*padding.len .. ^1])

lib/pure/xmldom.nim

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -232,10 +232,10 @@ proc createAttributeNS*(doc: PDocument, namespaceURI: string, qualifiedName: str
232232
raise newException(EInvalidCharacterErr, "Invalid character")
233233
# Exceptions
234234
if qualifiedName.contains(':'):
235-
let qfnamespaces = qualifiedName.toLower().split(':')
235+
let qfnamespaces = qualifiedName.toLowerAscii().split(':')
236236
if isNil(namespaceURI):
237237
raise newException(ENamespaceErr, "When qualifiedName contains a prefix namespaceURI cannot be nil")
238-
elif qfnamespaces[0] == "xml" and
238+
elif qfnamespaces[0] == "xml" and
239239
namespaceURI != "http://www.w3.org/XML/1998/namespace" and
240240
qfnamespaces[1] notin stdattrnames:
241241
raise newException(ENamespaceErr,
@@ -311,10 +311,10 @@ proc createElement*(doc: PDocument, tagName: string): PElement =
311311
proc createElementNS*(doc: PDocument, namespaceURI: string, qualifiedName: string): PElement =
312312
## Creates an element of the given qualified name and namespace URI.
313313
if qualifiedName.contains(':'):
314-
let qfnamespaces = qualifiedName.toLower().split(':')
314+
let qfnamespaces = qualifiedName.toLowerAscii().split(':')
315315
if isNil(namespaceURI):
316316
raise newException(ENamespaceErr, "When qualifiedName contains a prefix namespaceURI cannot be nil")
317-
elif qfnamespaces[0] == "xml" and
317+
elif qfnamespaces[0] == "xml" and
318318
namespaceURI != "http://www.w3.org/XML/1998/namespace" and
319319
qfnamespaces[1] notin stdattrnames:
320320
raise newException(ENamespaceErr,
@@ -533,13 +533,13 @@ proc `prefix=`*(n: PNode, value: string) =
533533

534534
if isNil(n.fNamespaceURI):
535535
raise newException(ENamespaceErr, "namespaceURI cannot be nil")
536-
elif value.toLower() == "xml" and n.fNamespaceURI != "http://www.w3.org/XML/1998/namespace":
536+
elif value.toLowerAscii() == "xml" and n.fNamespaceURI != "http://www.w3.org/XML/1998/namespace":
537537
raise newException(ENamespaceErr,
538538
"When the namespace prefix is \"xml\" namespaceURI has to be \"http://www.w3.org/XML/1998/namespace\"")
539-
elif value.toLower() == "xmlns" and n.fNamespaceURI != "http://www.w3.org/2000/xmlns/":
539+
elif value.toLowerAscii() == "xmlns" and n.fNamespaceURI != "http://www.w3.org/2000/xmlns/":
540540
raise newException(ENamespaceErr,
541541
"When the namespace prefix is \"xmlns\" namespaceURI has to be \"http://www.w3.org/2000/xmlns/\"")
542-
elif value.toLower() == "xmlns" and n.fNodeType == AttributeNode:
542+
elif value.toLowerAscii() == "xmlns" and n.fNodeType == AttributeNode:
543543
raise newException(ENamespaceErr, "An AttributeNode cannot have a prefix of \"xmlns\"")
544544

545545
n.fNodeName = value & ":" & n.fLocalName

tests/async/tlambda.nim

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
# bug 2007
33

4-
import asyncdispatch, asyncnet, logging, json, uri, strutils, future
4+
import asyncdispatch, asyncnet, logging, json, uri, strutils, sugar
55

66
type
77
Builder = ref object
@@ -27,7 +27,7 @@ proc newBuild*(onProgress: ProgressCB): Build =
2727
result.onProgress = onProgress
2828

2929
proc start(build: Build, repo, hash: string) {.async.} =
30-
let path = repo.parseUri().path.toLower()
30+
let path = repo.parseUri().path.toLowerAscii()
3131

3232
proc onProgress(builder: Builder, message: string) {.async.} =
3333
debug($message)

0 commit comments

Comments
 (0)