wc: basic implementation.
This commit is contained in:
parent
6628b533c3
commit
8db0ba035a
22 changed files with 124 additions and 21 deletions
2
go.mod
2
go.mod
|
@ -1,5 +1,5 @@
|
|||
module github.com/k1574/goblin
|
||||
|
||||
go 1.17
|
||||
go 1.18
|
||||
|
||||
require github.com/k1574/gomtool v0.0.0-20220616060224-023d1559d777 // indirect
|
||||
|
|
|
@ -2,24 +2,25 @@ package main
|
|||
|
||||
import(
|
||||
"github.com/k1574/gomtool/m/multitool"
|
||||
"github.com/k1574/goblin/m/sep/cat"
|
||||
"github.com/k1574/goblin/m/sep/echo"
|
||||
"github.com/k1574/goblin/m/sep/mkdir"
|
||||
"github.com/k1574/goblin/m/sep/gtrue"
|
||||
"github.com/k1574/goblin/m/sep/gfalse"
|
||||
"github.com/k1574/goblin/m/sep/sort"
|
||||
"github.com/k1574/goblin/m/sep/tac"
|
||||
"github.com/k1574/goblin/m/sep/ls"
|
||||
"github.com/k1574/goblin/m/sep/yes"
|
||||
"github.com/k1574/goblin/m/sep/date"
|
||||
"github.com/k1574/goblin/m/sep/uniq"
|
||||
"github.com/k1574/goblin/m/sep/quote"
|
||||
"github.com/k1574/goblin/m/sep/urlprs"
|
||||
"github.com/k1574/goblin/m/sep/noext"
|
||||
"github.com/k1574/goblin/m/sep/mergelbl"
|
||||
"github.com/k1574/goblin/m/sep/basename"
|
||||
"github.com/k1574/goblin/m/sep/ec"
|
||||
"github.com/k1574/goblin/m/sep/read"
|
||||
"github.com/k1574/goblin/m/tool/cat"
|
||||
"github.com/k1574/goblin/m/tool/echo"
|
||||
"github.com/k1574/goblin/m/tool/mkdir"
|
||||
"github.com/k1574/goblin/m/tool/gtrue"
|
||||
"github.com/k1574/goblin/m/tool/gfalse"
|
||||
"github.com/k1574/goblin/m/tool/sort"
|
||||
"github.com/k1574/goblin/m/tool/tac"
|
||||
"github.com/k1574/goblin/m/tool/ls"
|
||||
"github.com/k1574/goblin/m/tool/yes"
|
||||
"github.com/k1574/goblin/m/tool/date"
|
||||
"github.com/k1574/goblin/m/tool/uniq"
|
||||
"github.com/k1574/goblin/m/tool/quote"
|
||||
"github.com/k1574/goblin/m/tool/urlprs"
|
||||
"github.com/k1574/goblin/m/tool/noext"
|
||||
"github.com/k1574/goblin/m/tool/mergelbl"
|
||||
"github.com/k1574/goblin/m/tool/basename"
|
||||
"github.com/k1574/goblin/m/tool/ec"
|
||||
"github.com/k1574/goblin/m/tool/read"
|
||||
"github.com/k1574/goblin/m/tool/wc"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
@ -42,6 +43,7 @@ func main() {
|
|||
"mergelbl" : mergelbl.Run,
|
||||
"ec" : ec.Run,
|
||||
"read" : read.Run,
|
||||
"wc" : wc.Run,
|
||||
}
|
||||
|
||||
multitool.Main("goblin", tools)
|
||||
|
|
|
@ -5,7 +5,7 @@ import (
|
|||
"os"
|
||||
"sort"
|
||||
"flag"
|
||||
"github.com/k1574/goblin/m/gen/input"
|
||||
"github.com/k1574/goblin/m/input"
|
||||
)
|
||||
|
||||
func Run(args []string) {
|
101
m/tool/wc/main.go
Normal file
101
m/tool/wc/main.go
Normal file
|
@ -0,0 +1,101 @@
|
|||
package wc
|
||||
|
||||
import (
|
||||
"os"
|
||||
"io"
|
||||
"bufio"
|
||||
"fmt"
|
||||
"unicode"
|
||||
"flag"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
var (
|
||||
vals [lastFlag]int
|
||||
flags [lastFlag]bool
|
||||
flagList []int
|
||||
)
|
||||
|
||||
const (
|
||||
wordsFlag = iota
|
||||
runesFlag
|
||||
charsFlag
|
||||
linesFlag
|
||||
lastFlag
|
||||
)
|
||||
|
||||
func rdRune(rd *bufio.Reader) (rune, int) {
|
||||
r, siz, err := rd.ReadRune()
|
||||
if err == io.EOF {
|
||||
finish()
|
||||
} else if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return r, siz
|
||||
}
|
||||
|
||||
func finish() {
|
||||
str := ""
|
||||
for i, v := range flagList {
|
||||
str += strconv.Itoa(vals[v])
|
||||
if i != ( len(flagList)-1 ) {
|
||||
str += "\t"
|
||||
}
|
||||
}
|
||||
fmt.Print(str)
|
||||
os.Exit(0)
|
||||
}
|
||||
|
||||
func Run(args []string) {
|
||||
var (
|
||||
r rune
|
||||
siz int
|
||||
)
|
||||
arg0 := args[0]
|
||||
args = args[1:]
|
||||
flagSet := flag.NewFlagSet(arg0, flag.ExitOnError)
|
||||
|
||||
flagSet.BoolVar(&flags[charsFlag], "c", false, "print amount of chars(bytes)")
|
||||
flagSet.BoolVar(&flags[runesFlag], "r", false, "print amount of runes in UTF stream")
|
||||
flagSet.BoolVar(&flags[wordsFlag], "w", false, "print amount of words")
|
||||
flagSet.BoolVar(&flags[linesFlag], "l", false, "print amount of lines")
|
||||
|
||||
flagSet.Parse(args)
|
||||
|
||||
for i, v := range flags {
|
||||
if v {
|
||||
break;
|
||||
} else if i == (len(flags) - 1){
|
||||
flagSet.Usage()
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
for i, v := range flags {
|
||||
if v {
|
||||
flagList = append(flagList, i)
|
||||
}
|
||||
}
|
||||
|
||||
rd := bufio.NewReader(os.Stdin)
|
||||
inWord := false
|
||||
for {
|
||||
r, siz = rdRune(rd)
|
||||
if inWord {
|
||||
if unicode.IsSpace(r) {
|
||||
inWord = false
|
||||
}
|
||||
} else {
|
||||
if !unicode.IsSpace(r) {
|
||||
vals[wordsFlag]++
|
||||
inWord = true
|
||||
}
|
||||
}
|
||||
vals[runesFlag]++
|
||||
vals[charsFlag] += siz
|
||||
if r == rune('\n') {
|
||||
vals[linesFlag]++
|
||||
}
|
||||
}
|
||||
finish()
|
||||
}
|
|
@ -6,7 +6,7 @@ import(
|
|||
"fmt"
|
||||
"flag"
|
||||
"strings"
|
||||
"github.com/k1574/goblin/m/gen/input"
|
||||
"github.com/k1574/goblin/m/input"
|
||||
)
|
||||
var(
|
||||
nArg int
|
Loading…
Reference in a new issue