4 ms·
> Rust is one of those languages where I need to work to make the compiler happy, but once I manage that, the code generally works on the first try. I can conf
by burntsushi 12y ago
> Rust is one of those languages where I need to work to make the compiler happy, but once I manage that, the code generally works on the first try.
I can confirm this. I have a CSV parser[1] that is maybe twice as fast as Python's CSV parser (which is written in C)+. There's nothing magical going on: with Rust, I can expose a safe iterator over fields in a record without allocating.
[1] - https://github.com/BurntSushi/rust-csv https://github.com/BurntSushi/rust-csv
The docs explain the different access patterns (start with convenience and move toward performance): http://burntsushi.net/rustdoc/csv/#iteratoring-over-records http://burntsushi.net/rustdoc/csv/#iteratoring-over-records
+ - Still working on gathering evidence...
- moron4hire 12y agoI don't understand. What more do you need out of CSV parsing that any standard regex library doesn't provide?
- eridius 12y agoI don't understand; why would you try and parse CSV with a regex? Note: CSV is actually much more complicated in practice than just splitting on commas.
- moron4hire 12y agoregex is much more capable than splitting on commas
- pselbert 12y agoThere are plenty of values in a purpose built CSV parser. Several important features, depending on the CSVs you'll be working with are: 1. Sometimes you want to actually write files, in which case you'll want help escaping quoting. 2. Shortcuts for using header information to provide more convenient access into a particular column, rather than always doing it by index. 3. Conveniently slurp in only parts of a file, or only particular columns in a file, without loading everything into memory.
- moron4hire 12y agoAll things a proper understanding of regex and a minimal understanding of streaming file IO can cover. The whole "if you think regex is the solution to your problem, now you have two problems" thing has gotten out of hand. Regex is not that hard.
- newobj 12y agoSo, what is the regex to parse csv?
- Tyr42 12y agoIt's simpler to handle quotes and backslashes escaping commas with a custom parser. And then there's the domain knowledge baked into the lib. Does your regex solution produce excel compatible csv files when you have leading zeros? That's important to some people.
- moron4hire 12y agoI think people who are downvoting me don't understand how ludicrously retarded most people who output CSV are. CSV is not RFC4180. It's whatever bullshit text file your client has handed you and convinced your project manager is your problem to parse, not their problem to generate even remotely correctly. There is no CSV library capable of handling "CSV". Every time someone asks you for it, you better kick and scream or expect to do a custom job.
- Too 12y agoThe csv library of python has handled every csv i've ever thrown on it, csv is "standardized" enough for that. Just set two things, the delimiter, the escape quote method and be done with it. The output is a list of dictionaries with the column headers as keys, very elegant. The best part is that using the same input you did to read a file you can use to save/modify the file and be sure it will look the same when your client re-opens it. A regexp would take twice the time to write and wouldn't give you half of those features, and it would probably fail at escaping sooner or later, for the same reason as xml can't be parsed with regexps.
- Ygg2 12y agoIt's kinda funny, because burntsushi wrote Rust's standard regex library.
- jxf 12y agoA regexp would work, but it's usually the wrong level of abstraction to operate at. One wants to say "for the next 2,000 rows, retrieve columns 2 and 4, and the column labeled 'foo'", not write a regexp.
- phaylon 12y agoI should've taken a deeper look at your CSV handling code earlier. That's quite a nice demonstration of an API using Decoder/Decodable.