-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add option to split files by line count
- Loading branch information
1 parent
7ff9693
commit 5a22ffe
Showing
2 changed files
with
63 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package main | ||
|
||
import ( | ||
"bytes" | ||
"io" | ||
"os" | ||
) | ||
|
||
func estimateFileTimesByLineCount(currentFileSet map[string]bool, fileTimes map[string]float64) { | ||
for fileName := range currentFileSet { | ||
file, err := os.Open(fileName) | ||
if err != nil { | ||
printMsg("failed to count lines in file %s: %v\n", file, err) | ||
continue | ||
} | ||
defer file.Close() | ||
lineCount, err := lineCounter(file) | ||
if err != nil { | ||
printMsg("failed to count lines in file %s: %v\n", file, err) | ||
continue | ||
} | ||
fileTimes[fileName] = float64(lineCount) | ||
} | ||
} | ||
|
||
// Credit to http://stackoverflow.com/a/24563853/6678 | ||
func lineCounter(r io.Reader) (int, error) { | ||
buf := make([]byte, 32*1024) | ||
count := 0 | ||
lineSep := []byte{'\n'} | ||
|
||
for { | ||
c, err := r.Read(buf) | ||
count += bytes.Count(buf[:c], lineSep) | ||
|
||
switch { | ||
case err == io.EOF: | ||
return count, nil | ||
|
||
case err != nil: | ||
return count, err | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters