MCPcopy Create free account
hub / github.com/blues/note / Indent

Function Indent

jsonxt/indent.go:79–141  ·  view source on GitHub ↗

Indent appends to dst an indented form of the JSON-encoded src. Each element in a JSON object or array begins on a new, indented line beginning with prefix followed by one or more copies of indent according to the indentation nesting. The data appended to dst does not begin with the prefix nor any i

(dst *bytes.Buffer, src []byte, prefix, indent string)

Source from the content-addressed store, hash-verified

77// For example, if src has no trailing spaces, neither will dst;
78// if src ends in a trailing newline, so will dst.
79func Indent(dst *bytes.Buffer, src []byte, prefix, indent string) error {
80 origLen := dst.Len()
81 var scan scanner
82 scan.reset()
83 needIndent := false
84 depth := 0
85 for _, c := range src {
86 scan.bytes++
87 v := scan.step(&scan, c)
88 if v == scanSkipSpace {
89 continue
90 }
91 if v == scanError {
92 break
93 }
94 if needIndent && v != scanEndObject && v != scanEndArray {
95 needIndent = false
96 depth++
97 newline(dst, prefix, indent, depth)
98 }
99
100 // Emit semantically uninteresting bytes
101 // (in particular, punctuation in strings) unmodified.
102 if v == scanContinue {
103 dst.WriteByte(c)
104 continue
105 }
106
107 // Add spacing around real punctuation.
108 switch c {
109 case '{', '[':
110 // delay indent so that empty object and array are formatted as {} and [].
111 needIndent = true
112 dst.WriteByte(c)
113
114 case ',':
115 dst.WriteByte(c)
116 newline(dst, prefix, indent, depth)
117
118 case ':':
119 dst.WriteByte(c)
120 dst.WriteByte(' ')
121
122 case '}', ']':
123 if needIndent {
124 // suppress indent in empty object/array
125 needIndent = false
126 } else {
127 depth--
128 newline(dst, prefix, indent, depth)
129 }
130 dst.WriteByte(c)
131
132 default:
133 dst.WriteByte(c)
134 }
135 }
136 if scan.eof() == scanError {

Callers 2

EncodeMethod · 0.85
MarshalIndentFunction · 0.85

Calls 4

resetMethod · 0.95
eofMethod · 0.95
newlineFunction · 0.85
LenMethod · 0.80

Tested by

no test coverage detected