@@ -37,17 +37,24 @@ func (iter *Iterator) SkipAndReturnBytes() []byte {
37
37
return iter .stopCapture ()
38
38
}
39
39
40
- type captureBuffer struct {
41
- startedAt int
42
- captured []byte
40
+ // SkipAndAppendBytes skips next JSON element and appends its content to
41
+ // buffer, returning the result.
42
+ func (iter * Iterator ) SkipAndAppendBytes (buf []byte ) []byte {
43
+ iter .startCaptureTo (buf , iter .head )
44
+ iter .Skip ()
45
+ return iter .stopCapture ()
43
46
}
44
47
45
- func (iter * Iterator ) startCapture ( captureStartedAt int ) {
48
+ func (iter * Iterator ) startCaptureTo ( buf [] byte , captureStartedAt int ) {
46
49
if iter .captured != nil {
47
50
panic ("already in capture mode" )
48
51
}
49
52
iter .captureStartedAt = captureStartedAt
50
- iter .captured = make ([]byte , 0 , 32 )
53
+ iter .captured = buf
54
+ }
55
+
56
+ func (iter * Iterator ) startCapture (captureStartedAt int ) {
57
+ iter .startCaptureTo (make ([]byte , 0 , 32 ), captureStartedAt )
51
58
}
52
59
53
60
func (iter * Iterator ) stopCapture () []byte {
@@ -58,13 +65,7 @@ func (iter *Iterator) stopCapture() []byte {
58
65
remaining := iter .buf [iter .captureStartedAt :iter .head ]
59
66
iter .captureStartedAt = - 1
60
67
iter .captured = nil
61
- if len (captured ) == 0 {
62
- copied := make ([]byte , len (remaining ))
63
- copy (copied , remaining )
64
- return copied
65
- }
66
- captured = append (captured , remaining ... )
67
- return captured
68
+ return append (captured , remaining ... )
68
69
}
69
70
70
71
// Skip skips a json object and positions to relatively the next json object
0 commit comments