diff options
author | Charlie Stanton <charlie@shtanton.xyz> | 2023-04-26 10:40:55 +0100 |
---|---|---|
committer | Charlie Stanton <charlie@shtanton.xyz> | 2023-04-26 10:40:55 +0100 |
commit | 43aeb71f2d89ef74227de99a065c4af3a38fc755 (patch) | |
tree | a601f4a88bae1e1eef4f139f5e5ef3fe1faeba82 /walk | |
parent | 8b0a772eee803c490c03f8515114d76eb8d919d8 (diff) | |
download | stred-go-43aeb71f2d89ef74227de99a065c4af3a38fc755.tar |
Refactor out from JSONIn.Read the code that pops the first value from the readBuffer
Diffstat (limited to 'walk')
-rw-r--r-- | walk/read.go | 69 |
1 files changed, 28 insertions, 41 deletions
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()) |