correct usage of 'iterator' and 'scanner'

This commit is contained in:
Jeff Carr 2025-03-26 04:48:36 -05:00
parent 4ab3a465af
commit 601bfa0739
3 changed files with 53 additions and 13 deletions

View File

@ -59,6 +59,11 @@ proto:
# rm -f auto.pb.go
autogenpb --proto file.proto --package main
# 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:
cd example; rm -f go.* *.pb.go

View File

@ -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
funcdef := "func new" + APPLE + "Iterator(things []*" + APPLE + ") *" + APPLE + "Iterator"
fmt.Fprintln(w, "// DEFINE THE", APPLE, "ITERATOR.")
fmt.Fprintln(w, "// itializes a new iterator.")
fmt.Fprintln(w, "// DEFINE THE", APPLE, "SCANNER.")
fmt.Fprintln(w, "// itializes a new scanner.")
fmt.Fprintln(w, funcdef, "{")
fmt.Fprintln(w, " return &"+APPLE+"Iterator{things: things}")
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, "}")
fmt.Fprintln(w, "")
fmt.Fprintln(w, "// END DEFINE THE ITERATOR")
fmt.Fprintln(w, "// END DEFINE THE SCANNER")
fmt.Fprintln(w, "")
return funcdef
@ -127,8 +127,8 @@ func (msg *MsgName) addIterAllFunc(w io.Writer, FRUIT, APPLE, APPLES string) str
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
// 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, " return iterator")
fmt.Fprintln(w, " scanner := new"+APPLE+"Iterator("+APPLE+"Pointers)")
fmt.Fprintln(w, " return scanner")
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, "")
// 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
fmt.Fprintln(w, " iterator := new"+APPLE+"Iterator("+APPLE+"Pointers)")
fmt.Fprintln(w, " return iterator")
fmt.Fprintln(w, " scanneriterator := new"+APPLE+"Iterator("+APPLE+"Pointers)")
fmt.Fprintln(w, " return scanneriterator")
fmt.Fprintln(w, "}")
fmt.Fprintln(w, "")

View File

@ -8,6 +8,7 @@ import (
"iter"
"os"
"strings"
sync "sync"
"go.wit.com/log"
)
@ -16,7 +17,6 @@ import (
// var maxVarname int
// var maxVartype int
var linesIter iter.Seq[string]
func protoReformat(filename string) error {
// read in the .proto file
@ -31,13 +31,13 @@ func protoReformat(filename string) error {
var fmtmsg *FormatMsg
fmtmsg = new(FormatMsg)
linesIter = makeLineIter(data)
var bigName int64
var bigType int64
var allLinesIter iter.Seq[string]
allLinesIter = makeLineIter(data)
// gets the max vartype and varname
for line := range linesIter {
for line := range allLinesIter {
if strings.HasPrefix(line, "message ") {
inMessage = true
continue
@ -69,7 +69,9 @@ func protoReformat(filename string) error {
fmtmsg.MaxVartype = bigType
// 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 inMessage {
// 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
}
// '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] {
items := strings.Split(string(data), "\n")
// log.Println("Made All() Iter.Seq[] with length", len(items))
@ -268,3 +270,36 @@ func formatMessage2(curmsg *FormatMsg) []string {
}
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