Skip to content

Commit

Permalink
nimgrep: change 2 iterators to closure (#15962)
Browse files Browse the repository at this point in the history
  • Loading branch information
Araq authored Nov 14, 2020
2 parents 96930b9 + edd84bd commit 784720a
Showing 1 changed file with 14 additions and 12 deletions.
26 changes: 14 additions & 12 deletions tools/nimgrep.nim
Original file line number Diff line number Diff line change
Expand Up @@ -998,7 +998,8 @@ proc hasRightDirectory(path: string, walkOptC: WalkOptComp[Pattern]): bool =
if dirname.contains(pat): return false
result = true

iterator walkDirBasic(dir: string, walkOptC: WalkOptComp[Pattern]): string =
iterator walkDirBasic(dir: string, walkOptC: WalkOptComp[Pattern]): string
{.closure.} =
var dirStack = @[dir] # stack of directories
var timeFiles = newSeq[(times.Time, string)]()
while dirStack.len > 0:
Expand Down Expand Up @@ -1040,7 +1041,8 @@ iterator walkDirBasic(dir: string, walkOptC: WalkOptComp[Pattern]): string =
for (_, file) in timeFiles:
yield file

iterator walkRec(paths: seq[string]): (string, string) =
iterator walkRec(paths: seq[string]): tuple[error: string, filename: string]
{.closure.} =
declareCompiledPatterns(walkOptC, WalkOptComp):
walkOptC.excludeFile.add walkOpt.excludeFile.compileArray()
walkOptC.includeFile.add walkOpt.includeFile.compileArray()
Expand Down Expand Up @@ -1130,13 +1132,13 @@ proc run1Thread() =
processFileResult(searchOptC.pattern, "-",
processFile(searchOptC, "-",
yieldContents=optReplace in options))
for (err, filename) in walkRec(paths):
if err != "":
for entry in walkRec(paths):
if entry.error != "":
inc(gVar.errors)
printError (err & filename)
printError (entry.error & entry.filename)
continue
processFileResult(searchOptC.pattern, filename,
processFile(searchOptC, filename,
processFileResult(searchOptC.pattern, entry.filename,
processFile(searchOptC, entry.filename,
yieldContents=optReplace in options))

# Multi-threaded version: all printing is being done in the Main thread.
Expand Down Expand Up @@ -1182,12 +1184,12 @@ proc pathProducer(arg: (seq[string], WalkOpt)) {.thread.} =
walkOpt = arg[1] # init thread-local copy of opt
var
nextFileN = 0
for (err, filename) in walkRec(paths):
if err == "":
searchRequestsChan.send((nextFileN,filename))
for entry in walkRec(paths):
if entry.error == "":
searchRequestsChan.send((nextFileN, entry.filename))
else:
resultsChan.send((false, nextFileN,
filename, @[Output(kind: openError, msg: err)]))
resultsChan.send((false, nextFileN, entry.filename,
@[Output(kind: openError, msg: entry.error)]))
nextFileN += 1
resultsChan.send((true, nextFileN, "", @[])) # pass total number of files

Expand Down

0 comments on commit 784720a

Please sign in to comment.