Random D geekout

Ary Manzana ary at esperanto.org.ar
Thu Apr 19 22:25:53 PDT 2012


On 4/20/12 1:09 PM, H. S. Teoh wrote:
> On Fri, Apr 20, 2012 at 08:44:06AM +0400, 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.
>
> You're right, it was unoptimized code. I ended up using ctRegex for
> them:
>
> 	enum attrRx = ctRegex!`...`;
> 	enum blockRx = ctRegex!`...`;
>
> 	if (auto m = match(line, attrRx)) {
> 		...
> 	} else if (auto m = match(line, blockRx)) {
> 		...
> 	}
>
> The fact that D enums can be arbitrary types is just beyond awesome.

No, enum there means "manifest constant", it has nothing to do with an 
enumeration...


More information about the Digitalmars-d mailing list