Skip to content
This repository has been archived by the owner on Sep 16, 2023. It is now read-only.

Commit

Permalink
update algorithm.
Browse files Browse the repository at this point in the history
  • Loading branch information
tamada committed Mar 11, 2020
1 parent 495b1f0 commit e16384e
Show file tree
Hide file tree
Showing 8 changed files with 410 additions and 325 deletions.
32 changes: 32 additions & 0 deletions adjacent_uniq.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package uniq2

/*
AdjacentUniqer is an implementation of uniqer for adjacent lines.
*/
type AdjacentUniqer struct {
prev string
firstLine bool
}

/*
NewAdjacentUniqer creates an instance of AdjacentUniqer.
*/
func NewAdjacentUniqer() *AdjacentUniqer {
return &AdjacentUniqer{prev: "", firstLine: true}
}

/*
StreamLine tries to remove the duplicated line.
*/
func (au *AdjacentUniqer) StreamLine(line string) bool {
if au.firstLine {
au.prev = line
au.firstLine = false
return true
}
if au.prev == line {
return false
}
au.prev = line
return true
}
29 changes: 18 additions & 11 deletions cmd/uniq2/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"os"

flag "github.com/spf13/pflag"
"github.com/tamada/uniq2/lib"
"github.com/tamada/uniq2"
)

/*
Expand All @@ -19,6 +19,7 @@ OPTIONS
-a, --adjacent delete only adjacent duplicated lines.
-d, --delete-lines only prints deleted lines.
-i, --ignore-case case sensitive.
-s, --show-counts show counts.
-h, --help print this message.
INPUT gives file name of input. If argument is single dash ('-')
Expand All @@ -27,20 +28,27 @@ OUTPUT represents the destination.
`, appName)
}

func perform(flags *flag.FlagSet, opts *lib.Options) int {
var args, err = lib.NewArguments(opts, flags.Args()[1:])
defer args.Close()
func printError(err error, statusCode int) int {
if err == nil {
err = args.Perform()
return 0
}
fmt.Println(err.Error())
return statusCode
}

func perform(flags *flag.FlagSet, opts *uniq2.Parameters) int {
var args, err = uniq2.NewArguments(flags.Args()[1:])
if err != nil {
fmt.Println(err.Error())
return 1
return printError(err, 1)
}
return 0
defer args.Close()
err = args.Perform(opts)
return printError(err, 2)
}

func goMain() int {
// defer profile.Start(profile.ProfilePath(".")).Stop()

var flags, opts = buildFlagSet()
var err = flags.Parse(os.Args)
if err == nil {
Expand All @@ -51,8 +59,8 @@ func goMain() int {
return 0
}

func buildFlagSet() (*flag.FlagSet, *lib.Options) {
var opts = lib.Options{}
func buildFlagSet() (*flag.FlagSet, *uniq2.Parameters) {
var opts = uniq2.Parameters{}
var flags = flag.NewFlagSet("uniq2", flag.ContinueOnError)
flags.Usage = func() { printHelp("uniq2") }
flags.BoolVarP(&opts.Adjacent, "adjacent", "a", false, "delete only the adjacent duplicate lines")
Expand All @@ -62,7 +70,6 @@ func buildFlagSet() (*flag.FlagSet, *lib.Options) {
}

func main() {
// separates main function in order to run defers before exit.
var exitStatus = goMain()
os.Exit(exitStatus)
}
180 changes: 0 additions & 180 deletions lib/uniq2.go

This file was deleted.

Loading

0 comments on commit e16384e

Please sign in to comment.