Skip to content

Commit ef3b0ec

Browse files
authored
encoding.csv: fix bug in RandomAccessReader, spotted on windows with mingw32 (#20571)
1 parent 4c47bb5 commit ef3b0ec

File tree

2 files changed

+51
-3
lines changed

2 files changed

+51
-3
lines changed

vlib/encoding/csv/csv_reader_random_access.v

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ pub fn (mut cr RandomAccessReader) map_csv() ! {
252252
// println("${i:-12d} of ${cr.f_len:-12d} readed: ${read_bytes_count}")
253253
mut p1 := p
254254
mut i1 := i64(0)
255-
for i1 <= read_bytes_count {
255+
for i1 < read_bytes_count {
256256
// println("loop char: ${*&u8(p1):c}")
257257
// manage quote char
258258
if *p1 == cr.quote {
@@ -529,7 +529,7 @@ pub fn (mut cr RandomAccessReader) rows_count() !i64 {
529529
// println("${i:-12d} of ${cr.f_len:-12d} readed: ${read_bytes_count}")
530530
mut p1 := p
531531
mut i1 := 0
532-
for i1 <= read_bytes_count {
532+
for i1 < read_bytes_count {
533533
if *p1 == cr.end_line {
534534
count++
535535
}

vlib/encoding/csv/csv_reader_test.v

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ This file contains tests
1010
Known limitations:
1111
*/
1212
import encoding.csv
13+
import strings
1314
import os
1415

1516
/******************************************************************************
@@ -267,7 +268,7 @@ fn test_csv_string() {
267268
// parse the temp file
268269
csvr = csv.csv_reader(
269270
file_path: file_path_str
270-
mem_buf_size: 64
271+
mem_buf_size: 32
271272
end_line_len: csv.endline_crlf_len
272273
)!
273274
perform_test(mut csvr)!
@@ -295,6 +296,53 @@ fn test_csv_string() {
295296
csvr.dispose_csv_reader()
296297
}
297298

299+
fn test_coherence() {
300+
file_path_str := os.join_path(os.temp_dir(), 'test_csv.csv')
301+
mut f := os.open_file(file_path_str, 'w')!
302+
mut b := strings.new_builder(64536)
303+
mut i := u64(0)
304+
mut sum := u64(0)
305+
for rows in 0 .. 1000 {
306+
for col in 0 .. 1000 {
307+
if col > 0 {
308+
b.write_u8(`,`)
309+
}
310+
b.write_string(i.str())
311+
i++
312+
sum += i
313+
}
314+
b.write_string('\n')
315+
}
316+
f.write_string(b.str())!
317+
f.close()
318+
319+
sum -= i
320+
// println('sum: ${sum}')
321+
322+
// parse the temp file
323+
mut csvr := csv.csv_reader(
324+
file_path: file_path_str
325+
mem_buf_size: 32
326+
end_line_len: csv.endline_cr_len
327+
)!
328+
329+
mut sum1 := u64(0)
330+
for row_index in 0 .. csvr.csv_map.len {
331+
row := csvr.get_row(row_index)!
332+
for x in row {
333+
sum1 += u64(x.int())
334+
}
335+
}
336+
// println('sum: ${sum1}')
337+
338+
csvr.dispose_csv_reader()
339+
340+
// remove the temp file
341+
os.rm(file_path_str)!
342+
343+
assert sum == sum1, 'csv coherence test failed'
344+
}
345+
298346
// Debug code
299347
fn main() {
300348
test_csv_string()

0 commit comments

Comments
 (0)