encoding.csv: fix bug in RandomAccessReader, spotted on windows with mingw32 (#20571)

This commit is contained in:
penguindark 2024-01-17 21:10:28 +01:00 committed by GitHub
parent 4c47bb5288
commit ef3b0ec775
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 51 additions and 3 deletions

View File

@ -252,7 +252,7 @@ pub fn (mut cr RandomAccessReader) map_csv() ! {
// println("${i:-12d} of ${cr.f_len:-12d} readed: ${read_bytes_count}") // println("${i:-12d} of ${cr.f_len:-12d} readed: ${read_bytes_count}")
mut p1 := p mut p1 := p
mut i1 := i64(0) mut i1 := i64(0)
for i1 <= read_bytes_count { for i1 < read_bytes_count {
// println("loop char: ${*&u8(p1):c}") // println("loop char: ${*&u8(p1):c}")
// manage quote char // manage quote char
if *p1 == cr.quote { if *p1 == cr.quote {
@ -529,7 +529,7 @@ pub fn (mut cr RandomAccessReader) rows_count() !i64 {
// println("${i:-12d} of ${cr.f_len:-12d} readed: ${read_bytes_count}") // println("${i:-12d} of ${cr.f_len:-12d} readed: ${read_bytes_count}")
mut p1 := p mut p1 := p
mut i1 := 0 mut i1 := 0
for i1 <= read_bytes_count { for i1 < read_bytes_count {
if *p1 == cr.end_line { if *p1 == cr.end_line {
count++ count++
} }

View File

@ -10,6 +10,7 @@ This file contains tests
Known limitations: Known limitations:
*/ */
import encoding.csv import encoding.csv
import strings
import os import os
/****************************************************************************** /******************************************************************************
@ -267,7 +268,7 @@ fn test_csv_string() {
// parse the temp file // parse the temp file
csvr = csv.csv_reader( csvr = csv.csv_reader(
file_path: file_path_str file_path: file_path_str
mem_buf_size: 64 mem_buf_size: 32
end_line_len: csv.endline_crlf_len end_line_len: csv.endline_crlf_len
)! )!
perform_test(mut csvr)! perform_test(mut csvr)!
@ -295,6 +296,53 @@ fn test_csv_string() {
csvr.dispose_csv_reader() csvr.dispose_csv_reader()
} }
fn test_coherence() {
file_path_str := os.join_path(os.temp_dir(), 'test_csv.csv')
mut f := os.open_file(file_path_str, 'w')!
mut b := strings.new_builder(64536)
mut i := u64(0)
mut sum := u64(0)
for rows in 0 .. 1000 {
for col in 0 .. 1000 {
if col > 0 {
b.write_u8(`,`)
}
b.write_string(i.str())
i++
sum += i
}
b.write_string('\n')
}
f.write_string(b.str())!
f.close()
sum -= i
// println('sum: ${sum}')
// parse the temp file
mut csvr := csv.csv_reader(
file_path: file_path_str
mem_buf_size: 32
end_line_len: csv.endline_cr_len
)!
mut sum1 := u64(0)
for row_index in 0 .. csvr.csv_map.len {
row := csvr.get_row(row_index)!
for x in row {
sum1 += u64(x.int())
}
}
// println('sum: ${sum1}')
csvr.dispose_csv_reader()
// remove the temp file
os.rm(file_path_str)!
assert sum == sum1, 'csv coherence test failed'
}
// Debug code // Debug code
fn main() { fn main() {
test_csv_string() test_csv_string()