MCPcopy Create free account
hub / github.com/lib/pq / appendEscapedText

Function appendEscapedText

encode.go:162–196  ·  view source on GitHub ↗
(buf []byte, text string)

Source from the content-addressed store, hash-verified

160}
161
162func appendEscapedText(buf []byte, text string) []byte {
163 escapeNeeded := false
164 startPos := 0
165
166 // check if we need to escape
167 for i := 0; i < len(text); i++ {
168 c := text[i]
169 if c == '\\' || c == '\n' || c == '\r' || c == '\t' {
170 escapeNeeded = true
171 startPos = i
172 break
173 }
174 }
175 if !escapeNeeded {
176 return append(buf, text...)
177 }
178
179 // copy till first char to escape, iterate the rest
180 result := append(buf, text[:startPos]...)
181 for i := startPos; i < len(text); i++ {
182 switch c := text[i]; c {
183 case '\\':
184 result = append(result, '\\', '\\')
185 case '\n':
186 result = append(result, '\\', 'n')
187 case '\r':
188 result = append(result, '\\', 'r')
189 case '\t':
190 result = append(result, '\\', 't')
191 default:
192 result = append(result, c)
193 }
194 }
195 return result
196}
197
198func parseTime(typ oid.Oid, s []byte) (time.Time, error) {
199 str := string(s)

Callers 3

TestAppendEscapedTextFunction · 0.85
appendEncodedTextFunction · 0.85

Calls

no outgoing calls

Tested by 2

TestAppendEscapedTextFunction · 0.68

Used in the wild real call sites across dependent graphs

searching dependent graphs…