Skip to content

Commit b637927

Browse files
committed
Move filter processing to end-of-row
This allows filters to be evaluated for non-existing elements. Most notably you can now exclude rows based on non-existing elements with either "incl: ." or "excl: ^$".
1 parent 8343fa8 commit b637927

File tree

1 file changed

+20
-30
lines changed

1 file changed

+20
-30
lines changed

src/main.rs

+20-30
Original file line numberDiff line numberDiff line change
@@ -324,10 +324,10 @@ fn add_table<'a>(name: &str, rowpath: &str, outfile: Option<&str>, settings: &Se
324324
fatalerr!("Error: filtering (incl/excl) and 'conv' cannot be used together on a single column");
325325
}
326326
if find.is_some() && !settings.hush_notice {
327-
eprintln!("Notice: when using filtering (incl/excl) and find/replace on a single column, the filter is checked before replacements");
327+
eprintln!("Notice: when using filtering (incl/excl) and find/replace on a single column, the filter is checked after replacements");
328328
}
329329
if aggr.is_some() && !settings.hush_notice {
330-
eprintln!("Notice: when using filtering (incl/excl) and aggregation on a single column, the filter is checked at each step of aggregation separately");
330+
eprintln!("Notice: when using filtering (incl/excl) and aggregation on a single column, the filter is checked after aggregation");
331331
}
332332
}
333333
if bbox.is_some() && (convert.is_none() || convert.unwrap() != "gml-to-ewkb") && !settings.hush_warning {
@@ -561,18 +561,6 @@ fn main() {
561561
if table.columns[i].value.borrow().is_empty() && !settings.hush_warning {
562562
eprintln!("Warning: column {} requested attribute {} not found", table.columns[i].name, request);
563563
}
564-
if let Some(re) = &table.columns[i].include {
565-
if !re.is_match(&table.columns[i].value.borrow()) {
566-
filtered = true;
567-
table.clear_columns();
568-
}
569-
}
570-
if let Some(re) = &table.columns[i].exclude {
571-
if re.is_match(&table.columns[i].value.borrow()) {
572-
filtered = true;
573-
table.clear_columns();
574-
}
575-
}
576564
if let (Some(s), Some(r)) = (table.columns[i].find, table.columns[i].replace) {
577565
let mut value = table.columns[i].value.borrow_mut();
578566
*value = value.replace(s, r);
@@ -623,36 +611,39 @@ fn main() {
623611
else {
624612
table.columns[i].value.borrow_mut().push_str(&decoded.cow_replace("\\", "\\\\").cow_replace("\r", "\\r").cow_replace("\n", "\\n").cow_replace("\t", "\\t"));
625613
}
614+
if let (Some(s), Some(r)) = (table.columns[i].find, table.columns[i].replace) {
615+
let mut value = table.columns[i].value.borrow_mut();
616+
*value = value.replace(s, r);
617+
}
618+
break;
619+
}
620+
}
621+
},
622+
Ok(Event::End(_)) => {
623+
if path == table.path { // This is an end tag of the row path
624+
for i in 0..table.columns.len() {
626625
if let Some(re) = &table.columns[i].include {
627626
if !re.is_match(&table.columns[i].value.borrow()) {
628627
filtered = true;
629-
table.clear_columns();
630-
break;
631628
}
632629
}
633630
if let Some(re) = &table.columns[i].exclude {
634631
if re.is_match(&table.columns[i].value.borrow()) {
635632
filtered = true;
636-
table.clear_columns();
637-
break;
638633
}
639634
}
640-
if let (Some(s), Some(r)) = (table.columns[i].find, table.columns[i].replace) {
641-
let mut value = table.columns[i].value.borrow_mut();
642-
*value = value.replace(s, r);
643-
}
644-
break;
645635
}
646-
}
647-
},
648-
Ok(Event::End(_)) => {
649-
if path == table.path { // This is an end tag of the row path
636+
650637
if filtered {
651638
filtered = false;
652-
filtercount += 1;
639+
table.clear_columns();
640+
if tables.is_empty() { filtercount += 1; } // Only count filtered for the main table
641+
else { // Subtable; nothing more to do in this case
642+
table = tables.pop().unwrap();
643+
continue 'restart;
644+
}
653645
}
654646
else {
655-
656647
if !tables.is_empty() { // This is a subtable
657648
if !table.normalized { // Write the first column value of the parent table as the first column of the subtable (for use as a foreign key)
658649
let key = tables.last().unwrap().columns[0].value.borrow();
@@ -773,7 +764,6 @@ fn main() {
773764
gmltoewkb = false;
774765
if !gml_to_ewkb(&table.columns[i].value, &gmlcoll, table.columns[i].bbox.as_ref(), table.columns[i].multitype, &settings) {
775766
filtered = true;
776-
table.clear_columns();
777767
}
778768
gmlcoll.clear();
779769
break;

0 commit comments

Comments
 (0)