-
Notifications
You must be signed in to change notification settings - Fork 3
/
jsonco.go
163 lines (147 loc) · 3.12 KB
/
jsonco.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
// Package jsonco implements an io.Reader wrapper for JSON with C-style comments. It also has a utility method
// which translates byte offset into line and character positions for easier error reporting.
// The package is aware of multibyte characters.
package jsonco
import (
"bytes"
"errors"
"io"
)
type state struct {
// Data source.
src io.Reader
// Content of data source read into memory.
bytes []byte
// Wrapper for bytes.
br *bytes.Reader
}
// ReadOffsetter is the interface that wraps the basic Read method and a LineAndChar method which converts byte
// offset into line and character position.
type ReadOffsetter interface {
io.Reader
LineAndChar(offset int64) (ln int, cn int, err error)
}
// New returns an io.Reader acting as proxy to r.
func New(r io.Reader) ReadOffsetter {
return &state{src: r}
}
// Read reads bytes from the underlying reader replacing C-style comments and trailing commas with spaces.
func (st *state) Read(p []byte) (n int, err error) {
if st.br == nil {
if st.bytes, err = processInput(st.src); err != nil {
return
}
st.br = bytes.NewReader(st.bytes)
}
return st.br.Read(p)
}
// LineAndChar calculates line and character position from the byte offset into underlying stream.
func (st *state) LineAndChar(offset int64) (int, int, error) {
if offset < 0 {
return -1, -1, errors.New("offset value cannot be negative")
}
br := bytes.NewReader(st.bytes)
// Count lines and characters.
lnum := 1
cnum := 0
// Number of bytes consumed.
var count int64
for {
ch, size, err := br.ReadRune()
if err == io.EOF {
return -1, -1, errors.New("offset value too large")
}
count += int64(size)
if ch == '\n' {
lnum++
cnum = 0
} else {
cnum++
}
//log.Println(offset, ch, string(ch), size, count, lnum, cnum)
if count >= offset {
break
}
}
return lnum, cnum, nil
}
func isNL(c byte) bool {
return c == '\n' || c == '\r'
}
func isWS(c byte) bool {
return c == ' ' || c == '\t' || isNL(c)
}
func consumeComment(s []byte, i int) int {
if i < len(s) && s[i] == '/' {
s[i-1] = ' '
for ; i < len(s) && !isNL(s[i]); i += 1 {
s[i] = ' '
}
}
if i < len(s) && s[i] == '*' {
s[i-1] = ' '
s[i] = ' '
for ; i < len(s); i += 1 {
if s[i] != '*' {
// Do not remove new lines inside multiline comments.
if s[i] != '\n' {
s[i] = ' '
}
} else {
s[i] = ' '
i++
if i < len(s) {
if s[i] == '/' {
s[i] = ' '
break
}
}
}
}
}
return i
}
func processInput(r io.Reader) ([]byte, error) {
buff := &bytes.Buffer{}
_, err := io.Copy(buff, r)
if err != nil {
return nil, err
}
s := buff.Bytes()
i := 0
for i < len(s) {
switch s[i] {
case '"':
i += 1
for i < len(s) {
if s[i] == '"' {
i += 1
break
} else if s[i] == '\\' {
i += 1
}
i += 1
}
case '/':
i = consumeComment(s, i+1)
case ',':
j := i
for {
i += 1
if i >= len(s) {
break
} else if s[i] == '}' || s[i] == ']' {
s[j] = ' '
break
} else if s[i] == '/' {
i = consumeComment(s, i+1)
} else if !isWS(s[i]) {
break
}
}
default:
i += 1
}
}
return s, nil
}