-
Notifications
You must be signed in to change notification settings - Fork 0
/
reader.go
98 lines (90 loc) · 1.97 KB
/
reader.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
// Copyright 2015 Giulio Iotti. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package jsoncomments
import (
"bufio"
"bytes"
"io"
)
func isQuote(c rune) bool {
if c == '"' || c == '\'' {
return true
}
return false
}
// Reader implements a reader that discards comments.
//
// Comments are all contents after a # in non-quoted string context.
type Reader struct {
scanner *bufio.Scanner
buf *bytes.Buffer
eof bool
ooc bool // Out-of-Context: cannot remove comments here.
}
// NewReader creates a new Reader from underlying reader r.
func NewReader(r io.Reader) *Reader {
return &Reader{
buf: bytes.NewBufferString(""),
scanner: bufio.NewScanner(r),
}
}
// findRune finds rune r in a non-quoted context in s.
func (r *Reader) findRune(s string, ru rune) int {
var esc bool // Next rune has been escaped
p := -1
for i, b := range s {
// Skip the rune after an escape
if esc {
esc = false
continue
}
// \ is used to escape the next rune in ooc mode
if b == '\\' && r.ooc {
esc = true
continue
}
if isQuote(b) {
r.ooc = !r.ooc
continue
}
// Found a valid comment marker rune
if b == ru && !r.ooc {
p = i
break
}
}
return p
}
// load loads into an internal buffer to satisfy the next read.
// Comments are stripped before the resulting data is written to the buffer.
func (r *Reader) load(n int) error {
var err error
for {
if !r.scanner.Scan() {
if err := r.scanner.Err(); err != nil {
return err
}
return io.EOF
}
line := r.scanner.Text()
p := r.findRune(line, '#')
if p >= 0 {
line = line[:p]
}
r.buf.WriteString(line + "\n")
if r.buf.Len() >= n {
return err
}
}
}
// Read implements a io.Reader interface.
func (r *Reader) Read(p []byte) (n int, err error) {
if r.buf.Len() < len(p) {
if err := r.load(len(p) - r.buf.Len()); err != nil {
n, _ = r.buf.Read(p)
return n, err
}
}
return r.buf.Read(p)
}