Does D need a sscanf?

Daniel Keep daniel.keep.lists at gmail.com
Mon Apr 30 07:20:06 PDT 2007



Anders F Björklund wrote:
> Daniel Keep wrote:
> 
>> For a current project, I've had to go and implement a D version of C's
>> venerable sscanf function.  Thanks to a combination of regular
>> expressions, templates and tuples, the sucker's type-safe and
>> arbitrarily extensible.
> 
> I called mine "unformat", to match "format". Like writef / readf.

Mine's called parsef (although it should probably be just parse to match
format), with a convenience function readfln (to match writefln, naturally.)

> No regular expressions or templates or tuples back then, though.

I remember those days... huddled around a camp fire in a dark cave,
eating freshly bludgeoned Dromaeosaurid... back before deodorant...

>> Anyway, it's good enough for what I need, but I was wondering if there's
>> any demand for it in the standard library.  I'm happy to work to bring
>> it up to scratch and public domain it if people want to see it in Phobos
>> (and if Walter wants it there, of course.)  Heaven knows I've wanted
>> this exact function from std.stdio myself a few times. :)
> 
> a.k.a. std.stdo :-)

I noticed :P

>> What do you think?
> 
> See
> http://www.digitalmars.com/webnews/newsgroups.php?art_group=digitalmars.D&article_id=19835
> 
> 
> --anders

Hmm... parsef doesn't work the same way as doFormat does... actually, it
looks like this:

> bool parsef(argsT...)(char[] pattern, char[] string, out argsT args)
> {
>     char[] regex;   scope(exit) delete regex;
>     uint[] groups;  scope(exit) delete groups;
>     regex = ftor(pattern, groups);
>     return parsefr(regex, string, groups, args);
> }

Is it a problem that it doesn't use the same getc/ungetc interface?
Obviously, that wouldn't work with RegExp, although I am rather partial
to this addition I made:

  char[] word, restOfLine;
  readfln("%/[^ ]+/%s", word, restOfLine);

%/.../ lets you use an arbitrary regexp :)

	-- Daniel

-- 
int getRandomNumber()
{
    return 4; // chosen by fair dice roll.
              // guaranteed to be random.
}

http://xkcd.com/

v2sw5+8Yhw5ln4+5pr6OFPma8u6+7Lw4Tm6+7l6+7D
i28a2Xs3MSr2e4/6+7t4TNSMb6HTOp5en5g6RAHCP  http://hackerkey.com/



More information about the Digitalmars-d mailing list