Random D geekout

Dmitry Olshansky dmitry.olsh at gmail.com
Thu Apr 19 22:52:15 PDT 2012


On 20.04.2012 8:44, Denis Shelomovskij wrote:
> 20.04.2012 8:06, H. S. Teoh написал:
>> I'm writing some code that does some very simplistic parsing, and I'm
>> just totally geeking out on how awesome D is for writing such code:
>>
>> import std.conv;
>> import std.regex;
>> import std.stdio;
>>
>> struct Data {
>> string name;
>> string phone;
>> int age;
>> ... // a whole bunch of other stuff
>> }
>>
>> void main() {
>> Data d;
>> foreach (line; stdin.byLine()) {
>> auto m = match(line, "(\w+)\s+(\w+)");
>
> It's better not to create a regex every iteration. Use e.g.
> ---
> auto regEx = regex(`(\w+)\s+(\w+)`);
> ---
> before foreach. Of course, you are not claiming this as a
> high-performance program, but creating a regex every iteration is too
> common mistake to show such code to newbies.

And that's why I pluged this hole - it happens too often. At least up to 
mm... 16 regexes are cached.



-- 
Dmitry Olshansky


More information about the Digitalmars-d mailing list