diff options
| author | hopehook <hopehook.com@gmail.com> | 2022-05-11 22:42:00 +0800 |
|---|---|---|
| committer | Gopher Robot <gobot@golang.org> | 2022-05-14 04:25:13 +0000 |
| commit | 3474cd4eee82ac442618391f8bc4a70d7b1cb65a (patch) | |
| tree | d75a58b58e37b4bad2fbeb6f4a9c28ed385dada6 /src/encoding/csv/reader.go | |
| parent | cb458c05a82aefb253034558b34f16dee8713274 (diff) | |
| download | go-3474cd4eee82ac442618391f8bc4a70d7b1cb65a.tar.xz | |
encoding/csv: add Reader.InputOffset method
Fixes #43401.
Change-Id: I2498e77e41d845130d95012bc8623bfb29c0dda1
Reviewed-on: https://go-review.googlesource.com/c/go/+/405675
TryBot-Result: Gopher Robot <gobot@golang.org>
Run-TryBot: Ian Lance Taylor <iant@golang.org>
Run-TryBot: Ian Lance Taylor <iant@google.com>
Auto-Submit: Ian Lance Taylor <iant@google.com>
Reviewed-by: Ian Lance Taylor <iant@google.com>
Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
Diffstat (limited to 'src/encoding/csv/reader.go')
| -rw-r--r-- | src/encoding/csv/reader.go | 18 |
1 files changed, 15 insertions, 3 deletions
diff --git a/src/encoding/csv/reader.go b/src/encoding/csv/reader.go index f860f4f25f..90a37e6074 100644 --- a/src/encoding/csv/reader.go +++ b/src/encoding/csv/reader.go @@ -149,6 +149,9 @@ type Reader struct { // numLine is the current line being read in the CSV file. numLine int + // offset is the input stream byte offset of the current reader position. + offset int64 + // rawBuffer is a line buffer only used by the readLine method. rawBuffer []byte @@ -210,6 +213,13 @@ func (r *Reader) FieldPos(field int) (line, column int) { return p.line, p.col } +// InputOffset returns the input stream byte offset of the current reader +// position. The offset gives the location of the end of the most recently +// read row and the beginning of the next row. +func (r *Reader) InputOffset() int64 { + return r.offset +} + // pos holds the position of a field in the current line. type position struct { line, col int @@ -247,14 +257,16 @@ func (r *Reader) readLine() ([]byte, error) { } line = r.rawBuffer } - if len(line) > 0 && err == io.EOF { + readSize := len(line) + if readSize > 0 && err == io.EOF { err = nil // For backwards compatibility, drop trailing \r before EOF. - if line[len(line)-1] == '\r' { - line = line[:len(line)-1] + if line[readSize-1] == '\r' { + line = line[:readSize-1] } } r.numLine++ + r.offset += int64(readSize) // Normalize \r\n to \n on all input lines. if n := len(line); n >= 2 && line[n-2] == '\r' && line[n-1] == '\n' { line[n-2] = '\n' |
