@@ -29,23 +29,24 @@ std::vector<std::string_view> InputReader::ReadRecords(const std::string& input)
2929 records.reserve (1000000 ); // Pre-allocated capacity
3030
3131 const char * start = input.data ();
32- const char * end = start;
33- const char * const input_end = start + input.size ();
34-
35- while (end < input_end) {
36- if (*end == ' \n ' ) {
37- records.emplace_back (start, end - start);
38- start = end + 1 ; // Move past the delimiter
39- }
40- ++end;
41- }
42-
43- // Add the last segment if not empty
44- if (start < input_end) {
32+ const char * input_end = start + input.size ();
33+
34+ while (start < input_end) {
35+ // Use memchr to find the next newline in the remaining input.
36+ const void * pos = std::memchr (start, ' \n ' , input_end - start);
37+ if (pos) {
38+ const char * newline = static_cast <const char *>(pos);
39+ records.emplace_back (start, newline - start);
40+ start = newline + 1 ; // Move past the newline
41+ } else {
4542#ifndef NDEBUG
46- std::cout << " Adding last segment" << std::string_view (start, input_end - start) << " \n " ;
43+ std::cout << " Adding last segment: "
44+ << std::string_view (start, input_end - start) << " \n " ;
4745#endif
48- records.emplace_back (start, input_end - start);
46+ // No newline found, so the rest is the last record.
47+ records.emplace_back (start, input_end - start);
48+ break ;
49+ }
4950 }
5051
5152 return records;
0 commit comments