Skip to content

Commit

Permalink
refactor, code review, version up
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisVilches committed Oct 27, 2024
1 parent ca74a32 commit 11e3ef3
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 52 deletions.
2 changes: 1 addition & 1 deletion .metadata/version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.0.12
2.0.13
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,9 @@ docker run --rm $IMG
```

Clean up manually afterwards (i.e. remove images).

Generate screenshots by first installing [termshot](https://github.com/homeport/termshot) and then executing the following command:

```sh
bash ./screenshots/gen.sh path_to_termshot_executable path_to_cpdiff_executable
```
41 changes: 22 additions & 19 deletions cli/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,22 +116,28 @@ func showComparisonEntry(
return true, nil
}

func readLinesToChannel(buf *bufio.Scanner, ch chan string, opts options) {
for buf.Scan() {
line := buf.Text()
func readLinesToChannel(buf *bufio.Scanner, opts options) <-chan string {
ch := make(chan string, chSize)

if opts.trim {
line = strings.TrimSpace(line)
}
go func() {
for buf.Scan() {
line := buf.Text()

if opts.trim {
line = strings.TrimSpace(line)
}

if shouldSkipLine(line, opts) {
continue
if shouldSkipLine(line, opts) {
continue
}

ch <- line
}

ch <- line
}
close(ch)
}()

close(ch)
return ch
}

func getFile(path string) (*os.File, error) {
Expand Down Expand Up @@ -171,7 +177,7 @@ func getBothFiles(args []string) ([]*os.File, error) {
}

func listenEntries(
entries chan cmp.ComparisonEntry,
entries <-chan cmp.ComparisonEntry,
opts options,
) (*fullResult, error) {
res := newFullResult()
Expand Down Expand Up @@ -213,19 +219,16 @@ func mainCommand(opts options, args []string) error {
lhs := bufio.NewScanner(files[0])
rhs := bufio.NewScanner(files[1])

entries := make(chan cmp.ComparisonEntry, chSize)
lhsCh := make(chan string, chSize)
rhsCh := make(chan string, chSize)
lhsCh := readLinesToChannel(lhs, opts)
rhsCh := readLinesToChannel(rhs, opts)

go readLinesToChannel(lhs, lhsCh, opts)
go readLinesToChannel(rhs, rhsCh, opts)
go cmp.Process(
entries := cmp.Process(
lhsCh,
rhsCh,
opts.error,
opts.useRelativeError,
opts.numbers,
entries,
chSize,
)

fullResult, err := listenEntries(entries, opts)
Expand Down
14 changes: 7 additions & 7 deletions cmp/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,39 +5,39 @@ import (
"strings"
)

func toNumArray(s string) ([]big.Decimal, bool) {
func toNumArray(s string) []big.Decimal {
parts := strings.Fields(s)
res := []big.Decimal{}

if len(parts) == 0 {
return nil, false
return nil
}

for _, part := range parts {
if len(part) > 1 && part[0] == '0' && part[1] != '.' {
return nil, false
return nil
}

val, ok := big.NewFromString(part)

if !ok {
return nil, false
return nil
}

res = append(res, val)
}

return res, true
return res
}

func newComparable(line string) Comparable {
if len(line) == 0 {
return Empty{}
}

nums, ok := toNumArray(line)
nums := toNumArray(line)

if ok {
if nums != nil {
return NumArray{nums: nums, rawData: line}
}

Expand Down
52 changes: 29 additions & 23 deletions cmp/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,36 +76,42 @@ func applyStringFallbackHeuristic(lhs, rhs *Comparable, line1, line2 string) {

func Process(
lhsCh,
rhsCh chan string,
rhsCh <-chan string,
allowedError big.Decimal,
useRelativeErr bool,
useNumbers bool,
entriesCh chan ComparisonEntry,
) {
for {
line1, ok1 := <-lhsCh
line2, ok2 := <-rhsCh

if !ok1 && !ok2 {
break
}
chSize int,
) <-chan ComparisonEntry {
entries := make(chan ComparisonEntry, chSize)

var lhs, rhs Comparable
go func() {
for {
line1, ok1 := <-lhsCh
line2, ok2 := <-rhsCh

if useNumbers {
lhs = newComparable(line1)
rhs = newComparable(line2)
if !ok1 && !ok2 {
break
}

applyStringFallbackHeuristic(&lhs, &rhs, line1, line2)
} else {
lhs = newComparableNonNumeric(line1)
rhs = newComparableNonNumeric(line2)
}
var lhs, rhs Comparable

ranges, maxErr := lhs.compare(rhs, useRelativeErr, allowedError)
if useNumbers {
lhs = newComparable(line1)
rhs = newComparable(line2)

entriesCh <- newComparisonEntry(lhs, rhs, ranges, maxErr)
}
applyStringFallbackHeuristic(&lhs, &rhs, line1, line2)
} else {
lhs = newComparableNonNumeric(line1)
rhs = newComparableNonNumeric(line2)
}

ranges, maxErr := lhs.compare(rhs, useRelativeErr, allowedError)

entries <- newComparisonEntry(lhs, rhs, ranges, maxErr)
}

close(entries)
}()

close(entriesCh)
return entries
}
2 changes: 0 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,3 @@ func main() {
os.Exit(1)
}
}

// TODO: This project needs a massive code review.

0 comments on commit 11e3ef3

Please sign in to comment.