Basic range implementation.

This commit is contained in:
k1574 2022-07-14 19:13:58 +05:00
parent c835888064
commit 2ecb2acf6b
2 changed files with 124 additions and 0 deletions

View file

@ -22,6 +22,7 @@ import(
"github.com/k1574/goblin/m/tool/read" "github.com/k1574/goblin/m/tool/read"
"github.com/k1574/goblin/m/tool/wc" "github.com/k1574/goblin/m/tool/wc"
"github.com/k1574/goblin/m/tool/ftest" "github.com/k1574/goblin/m/tool/ftest"
"github.com/k1574/goblin/m/tool/grange"
) )
func main() { func main() {
@ -46,6 +47,7 @@ func main() {
"read" : read.Run, "read" : read.Run,
"wc" : wc.Run, "wc" : wc.Run,
"ftest" : ftest.Run, "ftest" : ftest.Run,
"range" : grange.Run,
} }
multitool.Main("goblin", tools) multitool.Main("goblin", tools)

122
m/tool/grange/main.go Normal file
View file

@ -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]()
}