File: streamquote.go

package info (click to toggle)
golang-github-nkovacs-streamquote 1.0.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 76 kB
  • sloc: makefile: 2
file content (171 lines) | stat: -rw-r--r-- 4,075 bytes parent folder | download
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
164
165
166
167
168
169
170
171
// Package streamquote implements a streaming version of strconv.Quote.
package streamquote

import (
	"io"
	"strconv"
	"unicode/utf8"
)

// Converter converts data by escaping control characters and
// non-printable characters using Go escape sequences.
type Converter interface {
	// Convert converts the data in "in", writing it to "out".
	// It uses Go escape sequences (\t, \n, \xFF, \u0100) for control characters
	// and non-printable characters as defined by strconv.IsPrint.
	// It is not safe for concurrent use.
	Convert(in io.Reader, out io.Writer) (int, error)
}

const bufSize = 100 * 1024

const lowerhex = "0123456789abcdef"

type converter struct {
	readBuffer  [bufSize]byte
	writeBuffer [10]byte
}

// New returns a new Converter.
func New() Converter {
	return &converter{}
}

// Convert converts the data in "in", writing it to "out".
// It uses Go escape sequences (\t, \n, \xFF, \u0100) for control characters
// and non-printable characters as defined by strconv.IsPrint.
// It is not safe for concurrent use.
func (c *converter) Convert(in io.Reader, out io.Writer) (int, error) {
	var err error
	bufSize := len(c.readBuffer)
	n := 0

	var processed = bufSize
	var dataLen = 0

	for {
		if processed+utf8.UTFMax > bufSize {
			// need to read more
			leftover := bufSize - processed
			if leftover > 0 {
				copy(c.readBuffer[:leftover], c.readBuffer[processed:])
			}
			read, peekErr := in.Read(c.readBuffer[leftover:])
			if peekErr != nil && peekErr != io.EOF {
				err = peekErr
				break
			}
			dataLen = leftover + read
			processed = 0
		}
		if dataLen-processed == 0 {
			break
		}

		maxRune := processed + utf8.UTFMax
		if maxRune > dataLen {
			maxRune = dataLen
		}
		data := c.readBuffer[processed:maxRune]

		var discard, n2 int
		r, width := utf8.DecodeRune(data)
		if width == 1 && r == utf8.RuneError {
			c.writeBuffer[0] = '\\'
			c.writeBuffer[1] = 'x'
			c.writeBuffer[2] = lowerhex[data[0]>>4]
			c.writeBuffer[3] = lowerhex[data[0]&0xF]
			out.Write(c.writeBuffer[0:4])
			n2 = 4
			discard = 1
		} else {
			discard = width
			if r == rune('"') || r == '\\' { // always backslashed
				c.writeBuffer[0] = '\\'
				c.writeBuffer[1] = byte(r)
				out.Write(c.writeBuffer[0:2])
				n2 = 2
			} else if strconv.IsPrint(r) {
				out.Write(data[:width])
				n2 = width
			} else {
				switch r {
				case '\a':
					c.writeBuffer[0] = '\\'
					c.writeBuffer[1] = 'a'
					out.Write(c.writeBuffer[0:2])
					n2 = 2
				case '\b':
					c.writeBuffer[0] = '\\'
					c.writeBuffer[1] = 'b'
					out.Write(c.writeBuffer[0:2])
					n2 = 2
				case '\f':
					c.writeBuffer[0] = '\\'
					c.writeBuffer[1] = 'f'
					out.Write(c.writeBuffer[0:2])
					n2 = 2
				case '\n':
					c.writeBuffer[0] = '\\'
					c.writeBuffer[1] = 'n'
					out.Write(c.writeBuffer[0:2])
					n2 = 2
				case '\r':
					c.writeBuffer[0] = '\\'
					c.writeBuffer[1] = 'r'
					out.Write(c.writeBuffer[0:2])
					n2 = 2
				case '\t':
					c.writeBuffer[0] = '\\'
					c.writeBuffer[1] = 't'
					out.Write(c.writeBuffer[0:2])
					n2 = 2
				case '\v':
					c.writeBuffer[0] = '\\'
					c.writeBuffer[1] = 'v'
					out.Write(c.writeBuffer[0:2])
					n2 = 2
				default:
					switch {
					case r < ' ':
						c.writeBuffer[0] = '\\'
						c.writeBuffer[1] = 'x'
						c.writeBuffer[2] = lowerhex[data[0]>>4]
						c.writeBuffer[3] = lowerhex[data[0]&0xF]
						out.Write(c.writeBuffer[0:4])
						n2 = 4
					case r > utf8.MaxRune:
						r = 0xFFFD
						fallthrough
					case r < 0x10000:
						c.writeBuffer[0] = '\\'
						c.writeBuffer[1] = 'u'
						n2 = 2
						i := 2
						for s := 12; s >= 0; s -= 4 {
							c.writeBuffer[i] = lowerhex[r>>uint(s)&0xF]
							i++
							n2++
						}
						out.Write(c.writeBuffer[0:i])
					default:
						c.writeBuffer[0] = '\\'
						c.writeBuffer[1] = 'U'
						n2 = 2
						i := 2
						for s := 28; s >= 0; s -= 4 {
							c.writeBuffer[i] = lowerhex[r>>uint(s)&0xF]
							i++
							n2++
						}
						out.Write(c.writeBuffer[0:i])
					}
				}
			}
		}
		processed += discard
		n += n2
	}

	return n, err
}