Skip to content

Commit efd4798

Browse files
committed
csv2::Reader::rows(): Add argument determining whether to ignore empty lines.
1 parent 6069575 commit efd4798

File tree

2 files changed

+16
-10
lines changed

2 files changed

+16
-10
lines changed

include/csv2/reader.hpp

+8-5
Original file line numberDiff line numberDiff line change
@@ -273,22 +273,25 @@ class Reader {
273273
/**
274274
* @returns The number of rows (excluding the header)
275275
*/
276-
size_t rows() const {
276+
size_t rows(bool ignore_empty_lines = false) const {
277277
size_t result{0};
278278
if (!buffer_ || buffer_size_ == 0)
279279
return result;
280280

281281
// Count the first row if not header
282282
if (not first_row_is_header::value
283-
and *(static_cast<const char*>(buffer_)) != '\r')
283+
and (not ignore_empty_lines
284+
or *(static_cast<const char*>(buffer_)) != '\r'))
284285
++result;
285286

286287
for (const char *p = buffer_
287288
; (p = static_cast<const char *>(memchr(p, '\n', (buffer_ + buffer_size_) - p)))
288289
; ++p) {
289-
if (p < buffer_ + buffer_size_ - 1
290-
and *(p + 1) != '\r')
291-
++result;
290+
if (ignore_empty_lines
291+
and (p >= buffer_ + buffer_size_ - 1
292+
or *(p + 1) == '\r'))
293+
continue;
294+
++result;
292295
}
293296
return result;
294297
}

single_include/csv2/csv2.hpp

+8-5
Original file line numberDiff line numberDiff line change
@@ -1904,22 +1904,25 @@ class Reader {
19041904
/**
19051905
* @returns The number of rows (excluding the header)
19061906
*/
1907-
size_t rows() const {
1907+
size_t rows(bool ignore_empty_lines = false) const {
19081908
size_t result{0};
19091909
if (!buffer_ || buffer_size_ == 0)
19101910
return result;
19111911

19121912
// Count the first row if not header
19131913
if (not first_row_is_header::value
1914-
and *(static_cast<const char*>(buffer_)) != '\r')
1914+
and (not ignore_empty_lines
1915+
or *(static_cast<const char*>(buffer_)) != '\r'))
19151916
++result;
19161917

19171918
for (const char *p = buffer_
19181919
; (p = static_cast<const char *>(memchr(p, '\n', (buffer_ + buffer_size_) - p)))
19191920
; ++p) {
1920-
if (p < buffer_ + buffer_size_ - 1
1921-
and *(p + 1) != '\r')
1922-
++result;
1921+
if (ignore_empty_lines
1922+
and (p >= buffer_ + buffer_size_ - 1
1923+
or *(p + 1) == '\r'))
1924+
continue;
1925+
++result;
19231926
}
19241927
return result;
19251928
}

0 commit comments

Comments
 (0)