MCPcopy Create free account
hub / github.com/auxten/postgresql-parser / readRecord

Method readRecord

pkg/util/encoding/csv/reader.go:267–411  ·  view source on GitHub ↗
(dst []string)

Source from the content-addressed store, hash-verified

265}
266
267func (r *Reader) readRecord(dst []string) ([]string, error) {
268 if r.Comma == r.Comment || !validDelim(r.Comma) || (r.Comment != 0 && !validDelim(r.Comment)) {
269 return nil, errInvalidDelim
270 }
271
272 // Read line (automatically skipping past empty lines and any comments).
273 var line, fullLine []byte
274 var errRead error
275 for errRead == nil {
276 line, errRead = r.readLine()
277 if r.Comment != 0 && nextRune(line) == r.Comment {
278 line = nil
279 continue // Skip comment lines
280 }
281 if errRead == nil && len(line) == lengthCRLF(line) {
282 line = nil
283 continue // Skip empty lines
284 }
285 fullLine = line
286 break
287 }
288 if errRead == io.EOF {
289 return nil, errRead
290 }
291
292 // Parse each field in the record.
293 var err error
294 const quoteLen = len(`"`)
295 commaLen := utf8.RuneLen(r.Comma)
296 recLine := r.numLine // Starting line for record
297 r.recordBuffer = r.recordBuffer[:0]
298 r.fieldIndexes = r.fieldIndexes[:0]
299parseField:
300 for {
301 if r.TrimLeadingSpace {
302 line = bytes.TrimLeftFunc(line, unicode.IsSpace)
303 }
304 if len(line) == 0 || line[0] != '"' {
305 // Non-quoted string field
306 i := bytes.IndexRune(line, r.Comma)
307 field := line
308 if i >= 0 {
309 field = field[:i]
310 } else {
311 field = field[:len(field)-lengthCRLF(field)]
312 }
313 // Check to make sure a quote does not appear in field.
314 if !r.LazyQuotes {
315 if j := bytes.IndexByte(field, '"'); j >= 0 {
316 col := utf8.RuneCount(fullLine[:len(fullLine)-len(line[j:])])
317 err = &ParseError{StartLine: recLine, Line: r.numLine, Column: col, Err: ErrBareQuote}
318 break parseField
319 }
320 }
321 r.recordBuffer = append(r.recordBuffer, field...)
322 r.fieldIndexes = append(r.fieldIndexes, len(r.recordBuffer))
323 if i >= 0 {
324 line = line[i+commaLen:]

Callers 2

ReadMethod · 0.95
ReadAllMethod · 0.95

Calls 4

readLineMethod · 0.95
validDelimFunction · 0.85
nextRuneFunction · 0.85
lengthCRLFFunction · 0.85

Tested by

no test coverage detected