Skip to content

Commit

Permalink
Improve CSV parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
mantas-done committed Feb 17, 2024
1 parent 3c2d167 commit aa56f6a
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/Code/Converters/CsvConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ public function fileContentToInternalFormat($file_content, $original_file_conten
$column_count = count($last_row);
$checked_column = 0;
foreach ($last_row as $k => $column) {
if (TxtConverter::hasText($column)) {
break;
}
if (preg_match(self::timeRegex(), $column)) {
$start_time_column = $k;
$checked_column = $k;
Expand All @@ -71,6 +74,9 @@ public function fileContentToInternalFormat($file_content, $original_file_conten
if ($start_time_column !== null) {
for ($i = $checked_column + 1; $i < $column_count; $i++) {
$column = $last_row[$i];
if (TxtConverter::hasText($column)) {
break;
}
if (preg_match(self::timeRegex(), $column)) {
$end_time_column = $i;
$checked_column = $i;
Expand All @@ -91,13 +97,18 @@ public function fileContentToInternalFormat($file_content, $original_file_conten
}

$data_string = '';
$found_data = false;
foreach ($data as $row) {
if ($start_time_column !== null) {
if (!$found_data && $start_time_column !== null) {
$is_start_time = preg_match(self::timeRegex(), $row[$start_time_column]);
if (!$is_start_time) {
continue; // skip few first rows if label or empty
}
}
if (!$found_data && !TxtConverter::hasText($row[$text_column])) {
continue;
}
$found_data = true;

if ($start_time_column !== null) {
$start_time = $row[$start_time_column];
Expand Down

0 comments on commit aa56f6a

Please sign in to comment.