rust-csv icon indicating copy to clipboard operation
rust-csv copied to clipboard

`Position::line` reports inaccurate record lines when `has_headers(false)`

Open KiloJuliett opened this issue 5 years ago • 0 comments

When reading CSV files without a special header row , the line number of each record is inaccurate.

(As a sidenote, the documentation for Position::line is a bit unclear whether line numbers are counted relative to the first line in the file/input stream or relative to the first valid record or to some other reference frame. Regardless of the interpretation, the reported line number is still inaccurate.)

What version of the csv crate are you using?

1.1.2; however, this problem occurs in version 1.0.0.

Rust: Stable - 1.40.0 OS: Windows 10 1803 (if that information is useful)

Briefly describe the question, bug or feature request.

When reading a CSV file without a special header row (i.e. using ReaderBuilder::has_headers set to false), the line number reported by Position::line of each record after the first one is off by one, as if the first record failed to get counted.

Include a complete program demonstrating a problem.

main.rs:

fn main() {
    let mut rb = csv::ReaderBuilder::new()
                 .has_headers(false)
                 .from_path("csv.csv")
                 .unwrap();
    
    for entry in rb.records() {
        let record = entry.unwrap();
        println!("{}", record.position().unwrap().line());
    }
}

csv.csv:

expect,line,1
expect,line,2
expect,line,3
expect,line,4
expect,line,5

What is the observed behavior of the code above?

Output:

1
1
2
3
4

What is the expected or desired behavior of the code above?

Expected output:

1
2
3
4
5

KiloJuliett avatar Jan 12 '20 02:01 KiloJuliett