From 2ecb2acf6b73d285bfed00a2e7e4b1e0572c7ad5 Mon Sep 17 00:00:00 2001 From: k1574 Date: Thu, 14 Jul 2022 19:13:58 +0500 Subject: [PATCH] Basic range implementation. --- m/cmd/goblin/main.go | 2 + m/tool/grange/main.go | 122 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 124 insertions(+) create mode 100644 m/tool/grange/main.go diff --git a/m/cmd/goblin/main.go b/m/cmd/goblin/main.go index feeec9c..d165d5e 100644 --- a/m/cmd/goblin/main.go +++ b/m/cmd/goblin/main.go @@ -22,6 +22,7 @@ import( "github.com/k1574/goblin/m/tool/read" "github.com/k1574/goblin/m/tool/wc" "github.com/k1574/goblin/m/tool/ftest" + "github.com/k1574/goblin/m/tool/grange" ) func main() { @@ -46,6 +47,7 @@ func main() { "read" : read.Run, "wc" : wc.Run, "ftest" : ftest.Run, + "range" : grange.Run, } multitool.Main("goblin", tools) diff --git a/m/tool/grange/main.go b/m/tool/grange/main.go new file mode 100644 index 0000000..41c3cc2 --- /dev/null +++ b/m/tool/grange/main.go @@ -0,0 +1,122 @@ +package grange +/* Concatenate files in "stdout". */ +import( + "os" + "flag" + "fmt" + "strconv" +) + +var( + flagSet *flag.FlagSet + args []string + blockSize int + rangeType string + rangeTypeMap map[string] func() = map[string] func() { + "int" : IntRange, + "inteq" : IntRangeEq, + "byte" : ByteRange, + "rune" : RuneRange, + } +) + +func IntRange() { + IntRangeHndl(func(a, b int) bool { + if a < b { + return true + } else { + return false + } + }, func(a, b int) bool { + if a > b { + return true + } else { + return false + } + }) +} + +func IntRangeEq () { + IntRangeHndl(func(a, b int) bool { + if a <= b { + return true + } else { + return false + } + }, func(a, b int) bool { + if a >= b { + return true + } else { + return false + } + }) +} + +func IntRangeHndl(fn1, fn2 func(a, b int) bool) { + var ( + err error + i, a, b int + ) + step := 1 + + if len(args) < 2 { + flagSet.Usage() + } + + if len(args) == 3 { + step, err = strconv.Atoi(args[2]) + if err != nil { + panic(err) + } + if step == 0 { + flagSet.Usage() + } + } + + a, err = strconv.Atoi(args[0]) + if err != nil { + panic(err) + } + + b, err = strconv.Atoi(args[1]) + if err != nil { + panic(err) + } + + if step > 0 { + if a>b { + flagSet.Usage() + } + for i=a ; fn1(i, b) ; i += step { + fmt.Printf("%d%s", i, "\n") + } + } else if a > b { + for i=a ; fn2(i, b) ; i += step { + fmt.Printf("%d%s", i, "\n") + } + } else { + flagSet.Usage() + } + +} + +func ByteRange() { +} + +func RuneRange() { +} + +func Run(arg []string) { + arg0 := arg[0] + args = arg[1:] + flagSet = flag.NewFlagSet(arg0, flag.ExitOnError) + flagSet.StringVar(&rangeType, "t", "int", "range type") + flagSet.Usage = func() { + fmt.Fprintf(os.Stderr, "usage: %s [options] [files]\n", arg0) + flagSet.PrintDefaults() + os.Exit(1) + } + flagSet.Parse(args) + args = flagSet.Args() + rangeTypeMap[rangeType]() +}