commit 43aeb71f2d89ef74227de99a065c4af3a38fc755
parent 8b0a772eee803c490c03f8515114d76eb8d919d8
Author: Charlie Stanton <charlie@shtanton.xyz>
Date: Wed, 26 Apr 2023 10:40:55 +0100
Refactor out from JSONIn.Read the code that pops the first value from the readBuffer
Diffstat:
M | walk/read.go | | | 69 | ++++++++++++++++++++++++++++----------------------------------------- |
1 file changed, 28 insertions(+), 41 deletions(-)
diff --git a/walk/read.go b/walk/read.go
@@ -141,8 +141,33 @@ func firstValue(atoms []Atom) ([]Atom, bool) {
}
}
+func (in *JSONIn) readValue() []Atom {
+ try:
+ value, incomplete := firstValue(in.readBuffer[in.readIndex:])
+ if incomplete {
+ if in.readIndex == 0 {
+ newReadBuffer := make([]Atom, len(in.readBuffer), in.readBufferCapacity * 2)
+ in.readBufferCapacity *= 2
+ copy(newReadBuffer, in.readBuffer)
+ in.readBuffer = newReadBuffer
+ in.fillReadBuffer()
+ goto try
+ }
+ copy(in.readBuffer, in.readBuffer[in.readIndex:])
+ in.readBuffer = in.readBuffer[:len(in.readBuffer) - in.readIndex]
+ in.readIndex = 0
+ copy(in.actionBuffer, in.actionBuffer[in.actionIndex:])
+ in.actionBuffer = in.actionBuffer[:len(in.actionBuffer) - in.actionIndex]
+ in.actionIndex = 0
+ in.fillReadBuffer()
+ goto try
+ }
+ in.readIndex += len(value)
+ return value
+}
+
func (in *JSONIn) Read() (WalkItem, error) {
- actionLoop: for {
+ for {
if in.actionIndex == len(in.actionBuffer) {
in.actionIndex = 0
in.readIndex = 0
@@ -156,52 +181,14 @@ func (in *JSONIn) Read() (WalkItem, error) {
action := in.actionBuffer[in.actionIndex]
switch action {
case ActionReadValue:
- value, incomplete := firstValue(in.readBuffer[in.readIndex:])
- if incomplete {
- if in.readIndex == 0 {
- newReadBuffer := make([]Atom, len(in.readBuffer), in.readBufferCapacity * 2)
- in.readBufferCapacity *= 2
- copy(newReadBuffer, in.readBuffer)
- in.readBuffer = newReadBuffer
- in.fillReadBuffer()
- continue actionLoop
- }
- copy(in.readBuffer, in.readBuffer[in.readIndex:])
- in.readBuffer = in.readBuffer[:len(in.readBuffer) - in.readIndex]
- in.readIndex = 0
- copy(in.actionBuffer, in.actionBuffer[in.actionIndex:])
- in.actionBuffer = in.actionBuffer[:len(in.actionBuffer) - in.actionIndex]
- in.actionIndex = 0
- in.fillReadBuffer()
- continue actionLoop
- }
- in.readIndex += len(value)
+ value := in.readValue()
in.actionIndex++
return WalkItem {
Value: value,
Path: in.path,
}, nil
case ActionAppendPath:
- value, incomplete := firstValue(in.readBuffer[in.readIndex:])
- if incomplete {
- if in.readIndex == 0 {
- newReadBuffer := make([]Atom, len(in.readBuffer), in.readBufferCapacity * 2)
- in.readBufferCapacity *= 2
- copy(newReadBuffer, in.readBuffer)
- in.readBuffer = newReadBuffer
- in.fillReadBuffer()
- continue actionLoop
- }
- copy(in.readBuffer, in.readBuffer[in.readIndex:])
- in.readBuffer = in.readBuffer[:len(in.readBuffer) - in.readIndex]
- in.readIndex = 0
- copy(in.actionBuffer, in.actionBuffer[in.actionIndex:])
- in.actionBuffer = in.actionBuffer[:len(in.actionBuffer) - in.actionIndex]
- in.actionIndex = 0
- in.fillReadBuffer()
- continue actionLoop
- }
- in.readIndex += len(value)
+ value := in.readValue()
in.path = append(in.path, value...)
case ActionAppendPathNull:
in.path = append(in.path, NewAtomNull())