Skip to content

Commit 8bd9fe9

Browse files
authored
Merge pull request #20 from fpatron/zero-allocation-mode
add initial pass with zero allocation
2 parents e22bec6 + d856804 commit 8bd9fe9

3 files changed

Lines changed: 55 additions & 3 deletions

File tree

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
# jsonc - https://pkg.go.dev/github.com/fpatron/jsonc
1+
# jsonc
22

33
[![Go CI](https://github.com/fpatron/jsonc/actions/workflows/go.yml/badge.svg)](https://github.com/fpatron/jsonc/actions/workflows/go.yml)
4+
[![Go Doc](https://img.shields.io/badge/godoc-reference-blue.svg)](http://godoc.org/github.com/fpatron/jsonc)
45

5-
`jsonc` is an extension of the JSON data format that allows single and multi line comments and provides accurate parsing errors.
6+
`jsonc` is an extension of the JSON data format that allows single and multi line comments.
67

78
## Instalation
89
To install `jsonc`, use `go get`:
@@ -13,7 +14,6 @@ To install `jsonc`, use `go get`:
1314
## Features
1415

1516
- **Comment Support**: Easily parse JSON files with single-line and multi-line comments.
16-
- **Accurate Errors**: Errors while parsing will match original commented JSON.
1717
- **Simple API**: Uses a familiar API similar to the standard `encoding/json` package.
1818

1919
## Usage

jsonc.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,32 @@ func stripComments(data []byte) []byte {
1818
)
1919

2020
state := OUTSIDE
21+
hasComment := false
22+
scanLoop:
23+
for i := 0; i < len(data); i++ {
24+
switch state {
25+
case OUTSIDE:
26+
if data[i] == '/' && i+1 < len(data) && (data[i+1] == '/' || data[i+1] == '*') {
27+
hasComment = true
28+
break scanLoop
29+
}
30+
if data[i] == '"' {
31+
state = IN_STRING
32+
}
33+
case IN_STRING:
34+
if data[i] == '\\' && i+1 < len(data) {
35+
i++
36+
} else if data[i] == '"' {
37+
state = OUTSIDE
38+
}
39+
}
40+
}
41+
42+
if !hasComment {
43+
return data
44+
}
45+
46+
state = OUTSIDE
2147
result := make([]byte, 0, len(data))
2248

2349
for i := 0; i < len(data); i++ {

v2/jsonc.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,32 @@ func stripComments(data []byte) []byte {
1616
)
1717

1818
state := OUTSIDE
19+
hasComment := false
20+
scanLoop:
21+
for i := 0; i < len(data); i++ {
22+
switch state {
23+
case OUTSIDE:
24+
if data[i] == '/' && i+1 < len(data) && (data[i+1] == '/' || data[i+1] == '*') {
25+
hasComment = true
26+
break scanLoop
27+
}
28+
if data[i] == '"' {
29+
state = IN_STRING
30+
}
31+
case IN_STRING:
32+
if data[i] == '\\' && i+1 < len(data) {
33+
i++
34+
} else if data[i] == '"' {
35+
state = OUTSIDE
36+
}
37+
}
38+
}
39+
40+
if !hasComment {
41+
return data
42+
}
43+
44+
state = OUTSIDE
1945
result := make([]byte, 0, len(data))
2046

2147
for i := 0; i < len(data); i++ {

0 commit comments

Comments
 (0)