LEFT | RIGHT |
(no file at all) | |
1 // Copyright 2010 The Go Authors. All rights reserved. | 1 // Copyright 2010 The Go Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style | 2 // Use of this source code is governed by a BSD-style |
3 // license that can be found in the LICENSE file. | 3 // license that can be found in the LICENSE file. |
4 | 4 |
5 package json | 5 package json |
6 | 6 |
7 import "bytes" | 7 import "bytes" |
8 | 8 |
9 // Compact appends to dst the JSON-encoded src with | 9 // Compact appends to dst the JSON-encoded src with |
10 // insignificant space characters elided. | 10 // insignificant space characters elided. |
11 func Compact(dst *bytes.Buffer, src []byte) error { | 11 func Compact(dst *bytes.Buffer, src []byte) error { |
| 12 return compact(dst, src, false) |
| 13 } |
| 14 |
| 15 func compact(dst *bytes.Buffer, src []byte, escape bool) error { |
12 origLen := dst.Len() | 16 origLen := dst.Len() |
13 var scan scanner | 17 var scan scanner |
14 scan.reset() | 18 scan.reset() |
15 start := 0 | 19 start := 0 |
16 for i, c := range src { | 20 for i, c := range src { |
| 21 if escape && (c == '<' || c == '>' || c == '&') { |
| 22 if start < i { |
| 23 dst.Write(src[start:i]) |
| 24 } |
| 25 dst.WriteString(`\u00`) |
| 26 dst.WriteByte(hex[c>>4]) |
| 27 dst.WriteByte(hex[c&0xF]) |
| 28 start = i + 1 |
| 29 } |
17 v := scan.step(&scan, int(c)) | 30 v := scan.step(&scan, int(c)) |
18 if v >= scanSkipSpace { | 31 if v >= scanSkipSpace { |
19 if v == scanError { | 32 if v == scanError { |
20 break | 33 break |
21 } | 34 } |
22 if start < i { | 35 if start < i { |
23 dst.Write(src[start:i]) | 36 dst.Write(src[start:i]) |
24 } | 37 } |
25 start = i + 1 | 38 start = i + 1 |
26 } | 39 } |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
105 default: | 118 default: |
106 dst.WriteByte(c) | 119 dst.WriteByte(c) |
107 } | 120 } |
108 } | 121 } |
109 if scan.eof() == scanError { | 122 if scan.eof() == scanError { |
110 dst.Truncate(origLen) | 123 dst.Truncate(origLen) |
111 return scan.err | 124 return scan.err |
112 } | 125 } |
113 return nil | 126 return nil |
114 } | 127 } |
LEFT | RIGHT |