correct usage of 'iterator' and 'scanner'
This commit is contained in:
parent
4ab3a465af
commit
601bfa0739
5
Makefile
5
Makefile
|
@ -59,6 +59,11 @@ proto:
|
||||||
# rm -f auto.pb.go
|
# rm -f auto.pb.go
|
||||||
autogenpb --proto file.proto --package main
|
autogenpb --proto file.proto --package main
|
||||||
# rm -f auto.sort.pb.go auto.newsort.pb.go # auto.marshal.pb.go
|
# rm -f auto.sort.pb.go auto.newsort.pb.go # auto.marshal.pb.go
|
||||||
|
#
|
||||||
|
|
||||||
|
# use the current autogenpb
|
||||||
|
proto-local: bak clean
|
||||||
|
./autogenpb.last --proto file.proto --package main
|
||||||
|
|
||||||
junk:
|
junk:
|
||||||
cd example; rm -f go.* *.pb.go
|
cd example; rm -f go.* *.pb.go
|
||||||
|
|
|
@ -44,8 +44,8 @@ func newIter(w io.Writer, msg *MsgName) string {
|
||||||
// in this file where it is "new or "New". I changed it to lower case 2025.01.12
|
// in this file where it is "new or "New". I changed it to lower case 2025.01.12
|
||||||
funcdef := "func new" + APPLE + "Iterator(things []*" + APPLE + ") *" + APPLE + "Iterator"
|
funcdef := "func new" + APPLE + "Iterator(things []*" + APPLE + ") *" + APPLE + "Iterator"
|
||||||
|
|
||||||
fmt.Fprintln(w, "// DEFINE THE", APPLE, "ITERATOR.")
|
fmt.Fprintln(w, "// DEFINE THE", APPLE, "SCANNER.")
|
||||||
fmt.Fprintln(w, "// itializes a new iterator.")
|
fmt.Fprintln(w, "// itializes a new scanner.")
|
||||||
fmt.Fprintln(w, funcdef, "{")
|
fmt.Fprintln(w, funcdef, "{")
|
||||||
fmt.Fprintln(w, " return &"+APPLE+"Iterator{things: things}")
|
fmt.Fprintln(w, " return &"+APPLE+"Iterator{things: things}")
|
||||||
fmt.Fprintln(w, "}")
|
fmt.Fprintln(w, "}")
|
||||||
|
@ -75,7 +75,7 @@ func newIter(w io.Writer, msg *MsgName) string {
|
||||||
fmt.Fprintln(w, " return it.things[it.index-1]")
|
fmt.Fprintln(w, " return it.things[it.index-1]")
|
||||||
fmt.Fprintln(w, "}")
|
fmt.Fprintln(w, "}")
|
||||||
fmt.Fprintln(w, "")
|
fmt.Fprintln(w, "")
|
||||||
fmt.Fprintln(w, "// END DEFINE THE ITERATOR")
|
fmt.Fprintln(w, "// END DEFINE THE SCANNER")
|
||||||
fmt.Fprintln(w, "")
|
fmt.Fprintln(w, "")
|
||||||
|
|
||||||
return funcdef
|
return funcdef
|
||||||
|
@ -127,8 +127,8 @@ func (msg *MsgName) addIterAllFunc(w io.Writer, FRUIT, APPLE, APPLES string) str
|
||||||
fmt.Fprintln(w, "")
|
fmt.Fprintln(w, "")
|
||||||
// should this be 'new' or 'New' ? Does it matter? I think it's totally internal here. I think there are only 3 places
|
// should this be 'new' or 'New' ? Does it matter? I think it's totally internal here. I think there are only 3 places
|
||||||
// in this file where it is "new or "New". I changed it to lower case 2025.01.12
|
// in this file where it is "new or "New". I changed it to lower case 2025.01.12
|
||||||
fmt.Fprintln(w, " iterator := new"+APPLE+"Iterator("+APPLE+"Pointers)")
|
fmt.Fprintln(w, " scanner := new"+APPLE+"Iterator("+APPLE+"Pointers)")
|
||||||
fmt.Fprintln(w, " return iterator")
|
fmt.Fprintln(w, " return scanner")
|
||||||
fmt.Fprintln(w, "}")
|
fmt.Fprintln(w, "}")
|
||||||
fmt.Fprintln(w, "")
|
fmt.Fprintln(w, "")
|
||||||
|
|
||||||
|
@ -179,8 +179,8 @@ func (msg *MsgName) addAllFunc(w io.Writer, FRUIT, APPLE, APPLES string) string
|
||||||
fmt.Fprintln(w, "")
|
fmt.Fprintln(w, "")
|
||||||
// should this be 'new' or 'New' ? Does it matter? I think it's totally internal here. I think there are only 3 places
|
// should this be 'new' or 'New' ? Does it matter? I think it's totally internal here. I think there are only 3 places
|
||||||
// in this file where it is "new or "New". I changed it to lower case 2025.01.12
|
// in this file where it is "new or "New". I changed it to lower case 2025.01.12
|
||||||
fmt.Fprintln(w, " iterator := new"+APPLE+"Iterator("+APPLE+"Pointers)")
|
fmt.Fprintln(w, " scanneriterator := new"+APPLE+"Iterator("+APPLE+"Pointers)")
|
||||||
fmt.Fprintln(w, " return iterator")
|
fmt.Fprintln(w, " return scanneriterator")
|
||||||
fmt.Fprintln(w, "}")
|
fmt.Fprintln(w, "}")
|
||||||
fmt.Fprintln(w, "")
|
fmt.Fprintln(w, "")
|
||||||
|
|
||||||
|
|
|
@ -8,6 +8,7 @@ import (
|
||||||
"iter"
|
"iter"
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
|
sync "sync"
|
||||||
|
|
||||||
"go.wit.com/log"
|
"go.wit.com/log"
|
||||||
)
|
)
|
||||||
|
@ -16,7 +17,6 @@ import (
|
||||||
|
|
||||||
// var maxVarname int
|
// var maxVarname int
|
||||||
// var maxVartype int
|
// var maxVartype int
|
||||||
var linesIter iter.Seq[string]
|
|
||||||
|
|
||||||
func protoReformat(filename string) error {
|
func protoReformat(filename string) error {
|
||||||
// read in the .proto file
|
// read in the .proto file
|
||||||
|
@ -31,13 +31,13 @@ func protoReformat(filename string) error {
|
||||||
var fmtmsg *FormatMsg
|
var fmtmsg *FormatMsg
|
||||||
fmtmsg = new(FormatMsg)
|
fmtmsg = new(FormatMsg)
|
||||||
|
|
||||||
linesIter = makeLineIter(data)
|
|
||||||
|
|
||||||
var bigName int64
|
var bigName int64
|
||||||
var bigType int64
|
var bigType int64
|
||||||
|
|
||||||
|
var allLinesIter iter.Seq[string]
|
||||||
|
allLinesIter = makeLineIter(data)
|
||||||
// gets the max vartype and varname
|
// gets the max vartype and varname
|
||||||
for line := range linesIter {
|
for line := range allLinesIter {
|
||||||
if strings.HasPrefix(line, "message ") {
|
if strings.HasPrefix(line, "message ") {
|
||||||
inMessage = true
|
inMessage = true
|
||||||
continue
|
continue
|
||||||
|
@ -69,7 +69,9 @@ func protoReformat(filename string) error {
|
||||||
fmtmsg.MaxVartype = bigType
|
fmtmsg.MaxVartype = bigType
|
||||||
|
|
||||||
// write out the messages
|
// write out the messages
|
||||||
for line := range linesIter {
|
all := newLinesScanner(strings.Split(string(data), "\n"))
|
||||||
|
for all.Scan() {
|
||||||
|
line := all.Next()
|
||||||
if strings.HasPrefix(line, "message ") {
|
if strings.HasPrefix(line, "message ") {
|
||||||
if inMessage {
|
if inMessage {
|
||||||
// message inception. search for the architect. don't forget your totem
|
// message inception. search for the architect. don't forget your totem
|
||||||
|
@ -207,7 +209,7 @@ func slicesPop(parts []string) ([]string, string) {
|
||||||
return parts[0 : x-1], end
|
return parts[0 : x-1], end
|
||||||
}
|
}
|
||||||
|
|
||||||
// 'for x := range' syntax using the awesome golang 1.24 'iter'
|
// 'for x := range' syntax using the smartly done golang 1.24 'iter'
|
||||||
func makeLineIter(data []byte) iter.Seq[string] {
|
func makeLineIter(data []byte) iter.Seq[string] {
|
||||||
items := strings.Split(string(data), "\n")
|
items := strings.Split(string(data), "\n")
|
||||||
// log.Println("Made All() Iter.Seq[] with length", len(items))
|
// log.Println("Made All() Iter.Seq[] with length", len(items))
|
||||||
|
@ -268,3 +270,36 @@ func formatMessage2(curmsg *FormatMsg) []string {
|
||||||
}
|
}
|
||||||
return newmsg
|
return newmsg
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// DEFINE THE Lines ITERATOR.
|
||||||
|
// itializes a new iterator.
|
||||||
|
func newLinesScanner(things []string) *LinesScanner {
|
||||||
|
return &LinesScanner{things: things}
|
||||||
|
}
|
||||||
|
|
||||||
|
type LinesScanner struct {
|
||||||
|
sync.Mutex
|
||||||
|
|
||||||
|
things []string
|
||||||
|
index int
|
||||||
|
}
|
||||||
|
|
||||||
|
func (it *LinesScanner) Scan() bool {
|
||||||
|
if it.index >= len(it.things) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
it.Lock()
|
||||||
|
it.index++
|
||||||
|
it.Unlock()
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
// Next() returns the next thing in the array
|
||||||
|
func (it *LinesScanner) Next() string {
|
||||||
|
if it.index-1 == len(it.things) {
|
||||||
|
fmt.Println("Next() error in LinesScanner", it.index)
|
||||||
|
}
|
||||||
|
return it.things[it.index-1]
|
||||||
|
}
|
||||||
|
|
||||||
|
// END DEFINE THE ITERATOR
|
||||||
|
|
Loading…
Reference in New Issue