Created
April 1, 2015 06:55
-
-
Save psyomn/216d541151e7224ffde7 to your computer and use it in GitHub Desktop.
Crappy CSV parser in Rust.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /// Very, very, very, VERY bad CSV parser. If you're wondering, I wrote this because | |
| /// rustc-serializer was not working at some point, and needed a simple parser. | |
| pub fn parse_csv(s : String) -> Vec< Vec<String> > { | |
| let mut buff: String = String::new(); | |
| let mut record: Vec<String> = Vec::new(); | |
| let mut ret: Vec< Vec<String> > = Vec::new(); | |
| for c in s.chars() { | |
| println!("{}.", c); | |
| match c { | |
| /* Field separator */ | |
| ',' => { | |
| record.push(buff.clone()); | |
| buff.clear() | |
| }, | |
| /* Record separator */ | |
| '\n' => { | |
| record.push(buff.clone()); | |
| buff.clear(); | |
| ret.push(record.clone()); | |
| record.clear(); | |
| }, | |
| /* Aggregate data */ | |
| _ => { | |
| buff.push(c); | |
| } | |
| } | |
| } | |
| if buff.len() > 0 { | |
| record.push(buff.clone()); | |
| } | |
| if record.len() > 0 { | |
| ret.push(record.clone()); | |
| } | |
| return ret; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment