From 601bfa073999306d647a5af70d6aa9814ddb139f Mon Sep 17 00:00:00 2001 From: Jeff Carr Date: Wed, 26 Mar 2025 04:48:36 -0500 Subject: [PATCH] correct usage of 'iterator' and 'scanner' --- Makefile | 5 +++++ generateSort.go | 14 +++++++------- protoReformat.go | 47 +++++++++++++++++++++++++++++++++++++++++------ 3 files changed, 53 insertions(+), 13 deletions(-) diff --git a/Makefile b/Makefile index fc6742a..71d34ba 100644 --- a/Makefile +++ b/Makefile @@ -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 diff --git a/generateSort.go b/generateSort.go index 4692884..7319812 100644 --- a/generateSort.go +++ b/generateSort.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 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, "") diff --git a/protoReformat.go b/protoReformat.go index 98f9c86..6b90347 100644 --- a/protoReformat.go +++ b/protoReformat.go @@ -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