digitalmars.D - Does D need a sscanf?
- Daniel Keep (21/21) Apr 30 2007 Howdy all.
- Johan Granberg (3/21) Apr 30 2007 I think it would be useful, the few time I have used sscanf it has reall...
- =?UTF-8?B?QW5kZXJzIEYgQmrDtnJrbHVuZA==?= (7/17) Apr 30 2007 I called mine "unformat", to match "format". Like writef / readf.
- Daniel Keep (24/54) Apr 30 2007 Mine's called parsef (although it should probably be just parse to match
- =?UTF-8?B?QW5kZXJzIEYgQmrDtnJrbHVuZA==?= (11/20) Apr 30 2007 Not sure if it was mentioned in the post, but the crappy old code
- Sean Kelly (10/20) Apr 30 2007 And the original is at:
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/
Apr 30 2007
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? -- DanielI think it would be useful, the few time I have used sscanf it has really saved me a lot of work.
Apr 30 2007
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
Apr 30 2007
Anders F Björklund wrote:Daniel Keep wrote:Mine's called parsef (although it should probably be just parse to match format), with a convenience function readfln (to match writefln, naturally.)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.I remember those days... huddled around a camp fire in a dark cave, eating freshly bludgeoned Dromaeosaurid... back before deodorant...I noticed :PAnyway, 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 :-)Hmm... parsef doesn't work the same way as doFormat does... actually, it looks like this:What do you think?See http://www.digitalmars.com/webnews/newsgroups.php?art_group=digitalmars.D&article_id=19835 --andersbool 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/
Apr 30 2007
Daniel Keep wrote: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...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.)Good Old Rock. Nothing Beats Rock.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...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
Apr 30 2007
Anders F Björklund wrote:Daniel Keep wrote: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. SeanNot 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/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.)
Apr 30 2007