code is wrote on the fly
import std.string; import std.stdio; import std.algorithm; import std.math; enum string[] separators = [ " ", "\t", ",", ";", "\n", "\r\n" ]; string get_word( ref string s ){ string token; sizediff_t storePositions[separators.length + 1]; // set size array to the number of separator in array "separators" and latest field for current string lenght foreach( i, separator; separators ){ // compute position for each separator sizediff_t position = countUntil( s, separator ); if( position == -1 ) position = sizediff_t.max; storePositions[i] = position; } storePositions[ $ -1 ] = s.length; sizediff_t end = reduce!min( storePositions ); token = s[0 .. end].idup; writefln( "%s | %d", s, end ); return token; } void main( string[] args ){ string s = "a long;string\tyeah\n strange; ok"; bool isRunning= true; size_t start = 0; writefln( "parse: %s", s ); while( isRunning ){ string result = get_word( s[ start .. $] ); if( result == "" ) isRunning = false; else{ start += result.length + 1; result = get_word( s[ start .. $] ); } writefln( "token: %s, position: %d", result, start ); writeln( "----" ); } }
output:
parse: a long;string yeah
strange; ok
a long;string yeah
strange; ok | 1
long;string yeah
strange; ok | 4
token: long, position: 2
----
long;string yeah
strange; ok | 4
string yeah
strange; ok | 6
token: string, position: 7
----
string yeah
strange; ok | 6
yeah
strange; ok | 4
token: yeah, position: 14
----
yeah
strange; ok | 4
strange; ok | 0
token: , position: 19
----
strange; ok | 0
token: , position: 19
manpreet
Best Answer
2 years ago
I'm trying to learn D but am struggling with lack of documentation (or my understanding of it), so I came here. I already asked a different but unrelated question earlier today.
Anyway, here goes:
I would like to parse a string for different things.
String format is something like:
If there is no label, there is mandatory whitespace. Parameters can be comma-delimited. Parameter types are dependent on the mnemonic.
I would like to use
std.conv: parse
from the Phobos library to aid me, but I fail to understand the documentation on how to parse a "word", as in, some characters seperated by whitespace on either end. It works fine for integers and the like asint i = parse!int(line)
. But if I were to dostring s = parse!string(line)
it would grab the entire line.I cold parse this by hand, using
char**
(or,ref string
) as a datatype, just like I did when I wrote this in C. But I'm learning D to not have to.I tried something like this to do it manually:
Is this a good way to do it? Is there a cleaner way? A faster way? A safer way, perhaps? I'm not sure the
i+1
index always exists.Thanks for the help!
My faith in D is slightly dwindling already, as I've run into all sorts of problems. But the path is surely going to be worth it.