fixes more nil string bugs

This commit is contained in:
Araq 2018-08-08 18:22:48 +02:00
commit babd31360a
3 changed files with 10 additions and 8 deletions

View file

@ -312,7 +312,6 @@ proc setIndexTerm*(d: var RstGenerator, id, term: string,
## The index won't be written to disk unless you call `writeIndexFile() ## The index won't be written to disk unless you call `writeIndexFile()
## <#writeIndexFile>`_. The purpose of the index is documented in the `docgen ## <#writeIndexFile>`_. The purpose of the index is documented in the `docgen
## tools guide <docgen.html#index-switch>`_. ## tools guide <docgen.html#index-switch>`_.
assert(not d.theIndex.isNil)
var var
entry = term entry = term
isTitle = false isTitle = false
@ -337,7 +336,7 @@ proc hash(n: PRstNode): int =
result = hash(n.text) result = hash(n.text)
elif n.len > 0: elif n.len > 0:
result = hash(n.sons[0]) result = hash(n.sons[0])
for i in 1 .. <len(n): for i in 1 ..< len(n):
result = result !& hash(n.sons[i]) result = result !& hash(n.sons[i])
result = !$result result = !$result
@ -452,7 +451,7 @@ proc generateSymbolIndex(symbols: seq[IndexEntry]): string =
title="$3" data-doc-search-tag="$2" href="$1">$2</a></li> title="$3" data-doc-search-tag="$2" href="$1">$2</a></li>
""", [url, text, desc]) """, [url, text, desc])
else: else:
result.addf("""<li><a class="reference external" result.addf("""<li><a class="reference external"
data-doc-search-tag="$2" href="$1">$2</a></li> data-doc-search-tag="$2" href="$1">$2</a></li>
""", [url, text]) """, [url, text])
inc j inc j
@ -524,7 +523,7 @@ proc generateDocumentationTOC(entries: seq[IndexEntry]): string =
titleTag = levels[L].text titleTag = levels[L].text
else: else:
result.add(level.indentToLevel(levels[L].level)) result.add(level.indentToLevel(levels[L].level))
result.addf("""<li><a class="reference" data-doc-search-tag="$1" href="$2"> result.addf("""<li><a class="reference" data-doc-search-tag="$1" href="$2">
$3</a></li> $3</a></li>
""", [titleTag & " : " & levels[L].text, link, levels[L].text]) """, [titleTag & " : " & levels[L].text, link, levels[L].text])
inc L inc L

View file

@ -1366,9 +1366,11 @@ proc find*(s: string, sub: char, start: Natural = 0, last: Natural = 0): int {.n
if sub == s[i]: return i if sub == s[i]: return i
else: else:
when hasCStringBuiltin: when hasCStringBuiltin:
let found = c_memchr(s[start].unsafeAddr, sub, last-start+1) let L = last-start+1
if not found.isNil: if L > 0:
return cast[ByteAddress](found) -% cast[ByteAddress](s.cstring) let found = c_memchr(s[start].unsafeAddr, sub, L)
if not found.isNil:
return cast[ByteAddress](found) -% cast[ByteAddress](s.cstring)
else: else:
for i in start..last: for i in start..last:
if sub == s[i]: return i if sub == s[i]: return i
@ -1515,7 +1517,7 @@ proc replace*(s, sub: string, by = ""): string {.noSideEffect,
elif subLen == 1: elif subLen == 1:
# when the pattern is a single char, we use a faster # when the pattern is a single char, we use a faster
# char-based search that doesn't need a skip table: # char-based search that doesn't need a skip table:
var c = sub[0] let c = sub[0]
let last = s.high let last = s.high
var i = 0 var i = 0
while true: while true:

View file

@ -110,6 +110,7 @@ proc nimToCStringConv(s: NimString): cstring {.compilerProc, inline.} =
else: result = cstring(addr s.data) else: result = cstring(addr s.data)
proc copyStr(s: NimString, start: int): NimString {.compilerProc.} = proc copyStr(s: NimString, start: int): NimString {.compilerProc.} =
if s == nil: return nil
result = copyStrLast(s, start, s.len-1) result = copyStrLast(s, start, s.len-1)
proc toNimStr(str: cstring, len: int): NimString {.compilerProc.} = proc toNimStr(str: cstring, len: int): NimString {.compilerProc.} =