| Thread overview | ||||||||
|---|---|---|---|---|---|---|---|---|
|
April 30, 2007 Does D need a sscanf? | ||||
|---|---|---|---|---|
| ||||
Howdy all.
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.
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. :)
What do you think?
-- 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/
| ||||
April 30, 2007 Re: Does D need a sscanf? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Daniel Keep | Daniel Keep wrote:
>
> Howdy all.
>
> 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.
>
> 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. :)
>
> What do you think?
>
> -- Daniel
>
I think it would be useful, the few time I have used sscanf it has really saved me a lot of work.
| |||
April 30, 2007 Re: Does D need a sscanf? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Daniel Keep | 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. No regular expressions or templates or tuples back then, though. > 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 :-) > What do you think? See http://www.digitalmars.com/webnews/newsgroups.php?art_group=digitalmars.D&article_id=19835 --anders | |||
April 30, 2007 Re: Does D need a sscanf? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Anders F Björklund | 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/ | |||
April 30, 2007 Re: Does D need a sscanf? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Daniel Keep | Daniel Keep wrote: >> 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.) Not sure if it was mentioned in the post, but the crappy old code should still be posted at http://www.algonet.se/~afb/d/stdio/ There you'll find the hack implementation of unFormat, as well as readln/readfln/unformat/writeln/writefln/format and the others... >> 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... Good Old Rock. Nothing Beats Rock. > Is it a problem that it doesn't use the same getc/ungetc interface? Depends on what you want it do :-) I just wanted the reverse of writef. But the lack of TypeInfo made it never work with GCC 3.3 anyway, just like the current stream implementation of readf doesn't. So I left it. Figured it wasn't meant to be, and that you should use printf/scanf... --anders | |||
April 30, 2007 Re: Does D need a sscanf? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Anders F Björklund | Anders F Björklund wrote: > Daniel Keep wrote: > >>> 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.) > > Not sure if it was mentioned in the post, but the crappy old code > should still be posted at http://www.algonet.se/~afb/d/stdio/ And the original is at: http://www.invisibleduck.org/~sean/code/unformat.d http://www.invisibleduck.org/~sean/code/stdio.d http://www.invisibleduck.org/~sean/code/utf.d The utf changes are needed to support the one char unget limit. It's also a bit more efficient. Internally, everything is converted to dchar for comparison. This is arguably not a good idea for performance reasons, but it simplified the implementation. Sean | |||
Copyright © 1999-2021 by the D Language Foundation
Permalink
Reply