From f271be415fc4ea946373a996920c8596716afaba Mon Sep 17 00:00:00 2001 From: "i.merzlyakov" Date: Thu, 16 Nov 2023 14:31:25 +0700 Subject: [PATCH] fix(parser): reduce complexity of initializeJSONLine --- pkg/parser/json/json_line.go | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/pkg/parser/json/json_line.go b/pkg/parser/json/json_line.go index 4d468e3d48a..e39b7728f7a 100644 --- a/pkg/parser/json/json_line.go +++ b/pkg/parser/json/json_line.go @@ -46,6 +46,9 @@ func initializeJSONLine(doc []byte) *jsonLine { parent: "", } + line := 1 + prevInputOffset := 0 + // for each token inside JSON for { tok, err := dec.Token() @@ -80,15 +83,14 @@ func initializeJSONLine(doc []byte) *jsonLine { continue } - line := 1 // get the correct line based on byte offset - for i, val := range doc { - if i == int(dec.InputOffset()) { - break - } else if val == byte('\n') { + currentInputOffset := int(dec.InputOffset()) + for i := prevInputOffset; i < currentInputOffset; i++ { + if doc[i] == byte('\n') { line++ } } + prevInputOffset = currentInputOffset // insert into line information map if _, ok := newMap[tokStringRepresentation]; !ok {