From 43aeb71f2d89ef74227de99a065c4af3a38fc755 Mon Sep 17 00:00:00 2001 From: Charlie Stanton Date: Wed, 26 Apr 2023 10:40:55 +0100 Subject: Refactor out from JSONIn.Read the code that pops the first value from the readBuffer --- walk/read.go | 69 ++++++++++++++++++++++++------------------------------------ 1 file changed, 28 insertions(+), 41 deletions(-) (limited to 'walk/read.go') diff --git a/walk/read.go b/walk/read.go index e46ae58..e1b33a4 100644 --- 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()) -- cgit v1.2.3