@@ -37,9 +37,7 @@ func SupportedCPU() bool {
3737 return cpuid .CPU .Supports (cpuid .AVX2 , cpuid .CLMUL )
3838}
3939
40- // Parse a block of data and return the parsed JSON.
41- // An optional block of previously parsed json can be supplied to reduce allocations.
42- func Parse (b []byte , reuse * ParsedJson ) (* ParsedJson , error ) {
40+ func newInternalParsedJson (reuse * ParsedJson , opts []ParserOption ) (* internalParsedJson , error ) {
4341 if ! SupportedCPU () {
4442 return nil , errors .New ("Host CPU does not meet target specs" )
4543 }
@@ -53,7 +51,23 @@ func Parse(b []byte, reuse *ParsedJson) (*ParsedJson, error) {
5351 if pj == nil {
5452 pj = & internalParsedJson {}
5553 }
56- err := pj .parseMessage (b )
54+ pj .copyStrings = true
55+ for _ , opt := range opts {
56+ if err := opt (pj ); err != nil {
57+ return nil , err
58+ }
59+ }
60+ return pj , nil
61+ }
62+
63+ // Parse a block of data and return the parsed JSON.
64+ // An optional block of previously parsed json can be supplied to reduce allocations.
65+ func Parse (b []byte , reuse * ParsedJson , opts ... ParserOption ) (* ParsedJson , error ) {
66+ pj , err := newInternalParsedJson (reuse , opts )
67+ if err != nil {
68+ return nil , err
69+ }
70+ err = pj .parseMessage (b )
5771 if err != nil {
5872 return nil , err
5973 }
@@ -64,17 +78,12 @@ func Parse(b []byte, reuse *ParsedJson) (*ParsedJson, error) {
6478
6579// ParseND will parse newline delimited JSON.
6680// An optional block of previously parsed json can be supplied to reduce allocations.
67- func ParseND (b []byte , reuse * ParsedJson ) (* ParsedJson , error ) {
68- if ! SupportedCPU () {
69- return nil , errors .New ("Host CPU does not meet target specs" )
70- }
71- var pj internalParsedJson
72- if reuse != nil {
73- pj .ParsedJson = * reuse
81+ func ParseND (b []byte , reuse * ParsedJson , opts ... ParserOption ) (* ParsedJson , error ) {
82+ pj , err := newInternalParsedJson (reuse , opts )
83+ if err != nil {
84+ return nil , err
7485 }
75- b = bytes .TrimSpace (b )
76-
77- err := pj .parseMessageNdjson (b )
86+ err = pj .parseMessageNdjson (bytes .TrimSpace (b ))
7887 if err != nil {
7988 return nil , err
8089 }
@@ -166,6 +175,7 @@ func ParseNDStream(r io.Reader, res chan<- Stream, reuse <-chan *ParsedJson) {
166175 queue <- result
167176 go func () {
168177 var pj internalParsedJson
178+ pj .copyStrings = true
169179 select {
170180 case v := <- reuse :
171181 if cap (v .Message ) >= tmpSize + 1024 {
0 commit comments