BurntSushi / BurntSushi/rust-csv

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

Open
#184 0 comments 2 reactions 0 assignees View on GitHub
Dominant language
Rust
Stars
2k
Forks
257
PR merge metrics
No merged PRs in 30d

Description

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:
```rust
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
```

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.