Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / scripts / maps / gtfs_ingest / src / gtfs_extra.rs
gtfs_extra.rs37 lines · 1.3 KiBRaw
| 1 | /// Split one CSV line into fields, honouring quotes and `""` escapes. Unlike |
| 2 | /// [`crate::gtfs::parse_csv`] this cannot span newlines, which `shapes.txt` never needs — all |
| 3 | /// five of its columns are numbers or an id. |
| 4 | pub(crate) fn split_line(line: &str) -> Vec<String> { |
| 5 | let mut out = Vec::new(); |
| 6 | let mut field = String::new(); |
| 7 | let mut in_quotes = false; |
| 8 | let mut chars = line.chars().peekable(); |
| 9 | while let Some(c) = chars.next() { |
| 10 | if in_quotes { |
| 11 | if c == '"' { |
| 12 | if chars.peek() == Some(&'"') { |
| 13 | field.push('"'); |
| 14 | let _ = chars.next(); |
| 15 | } else { |
| 16 | in_quotes = false; |
| 17 | } |
| 18 | } else { |
| 19 | field.push(c); |
| 20 | } |
| 21 | } else if c == '"' { |
| 22 | in_quotes = true; |
| 23 | } else if c == ',' { |
| 24 | out.push(std::mem::take(&mut field)); |
| 25 | } else { |
| 26 | field.push(c); |
| 27 | } |
| 28 | } |
| 29 | out.push(field); |
| 30 | out |
| 31 | } |
| 32 | |
| 33 | /// A `shapes.txt` row before ordering: `(sequence, lat_e7, lon_e7, dist)`, where |
| 34 | /// `dist` is NaN when the row has none. 24 bytes rather than the 32 an |
| 35 | /// `(i64, i32, i32, Option<f64>)` needs — this intermediate has exactly the |
| 36 | /// defect the streaming loader exists to avoid. |
| 37 | pub(crate) type RawShapePoint = (u32, i32, i32, f64); |