summaryrefslogtreecommitdiff
path: root/protoReformat.go
diff options
context:
space:
mode:
authorJeff Carr <[email protected]>2025-03-26 04:48:36 -0500
committerJeff Carr <[email protected]>2025-03-26 20:44:06 -0500
commit601bfa073999306d647a5af70d6aa9814ddb139f (patch)
tree3abe0a025b2ce75eae39710f251b7a6b37fbd6c8 /protoReformat.go
parent4ab3a465af1d6fef6afe381b98817028da9f5d1e (diff)
correct usage of 'iterator' and 'scanner'
Diffstat (limited to 'protoReformat.go')
-rw-r--r--protoReformat.go47
1 files changed, 41 insertions, 6 deletions
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