Skip to content

Commit

Permalink
Bring --lines in line with wc
Browse files Browse the repository at this point in the history
  • Loading branch information
timdp committed Apr 27, 2018
1 parent 8d1e278 commit 9fe1688
Show file tree
Hide file tree
Showing 6 changed files with 9 additions and 17 deletions.
10 changes: 0 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,16 +61,6 @@ Run a slow command and count the number of bytes logged:
slow-command | lwc --bytes
```

## Caveats

- The `--lines` option is currently implemented differently from `wc`'s. Where
`wc` will count the number of newline characters, `lwc` will count the actual
number of lines. Hence, if there is no newline at the end of its input, `lwc`
will still count the line, while `wc` won't.

- While `lwc` is pretty fast, you'll still get better performance out of `wc`.
Benchmarks will be added at some point, but it's currently not a priority.

## JavaScript Version

This utility briefly existed as a
Expand Down
2 changes: 1 addition & 1 deletion internal/app/lwc/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func (config *Config) Processors() []Processor {
var temp [5]Processor
i := 0
if config.Lines {
temp[i] = Processor{bufio.ScanLines, ScanCount}
temp[i] = Processor{lwcutil.SplitOnByte(LINE_FEED, true), ScanCount}
i++
}
if config.Words {
Expand Down
3 changes: 0 additions & 3 deletions internal/app/lwc/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@ import (
)

const COUNT_FORMAT string = "%8d"
const CARRIAGE_RETURN byte = 13
const LINE_FEED byte = 10
const SPACE byte = 32

func FormatCounts(counts *[]uint64, name string, cr bool, lf bool) *bytes.Buffer {
buf := new(bytes.Buffer)
Expand Down
5 changes: 5 additions & 0 deletions internal/app/lwc/runes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package lwc

const CARRIAGE_RETURN byte = 13
const LINE_FEED byte = 10
const SPACE byte = 32
2 changes: 1 addition & 1 deletion internal/pkg/lwcutil/chan.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func NewFilesChanFromSlice(values []string) *chan string {
func NewFilesChanFromReader(reader io.Reader, separator byte) *chan string {
c := make(chan string)
scanner := bufio.NewScanner(reader)
scanner.Split(SplitOnByte(0))
scanner.Split(SplitOnByte(0, false))
go func() {
for scanner.Scan() {
name := scanner.Text()
Expand Down
4 changes: 2 additions & 2 deletions internal/pkg/lwcutil/scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ import (
"bytes"
)

func SplitOnByte(b byte) bufio.SplitFunc {
func SplitOnByte(b byte, requireEnd bool) bufio.SplitFunc {
return func(data []byte, atEOF bool) (advance int, token []byte, err error) {
if atEOF && len(data) == 0 {
return 0, nil, nil
}
if i := bytes.IndexByte(data, b); i >= 0 {
return i + 1, data[0:i], nil
}
if atEOF {
if atEOF && !requireEnd {
return len(data), data, nil
}
return 0, nil, nil
Expand Down

0 comments on commit 9fe1688

Please sign in to comment.