Note that there are some explanatory texts on larger screens.

plurals
  1. POParsing a string in D
    primarykey
    data
    text
    <p>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.</p> <p>Anyway, here goes:</p> <p>I would like to parse a string for different things.</p> <p>String format is something like:</p> <pre><code>[&lt;label&gt;] &lt;mnemonic&gt; [parameters] </code></pre> <p>If there is no label, there is mandatory whitespace. Parameters can be comma-delimited. Parameter types are dependent on the mnemonic.</p> <p>I would like to use <code>std.conv: parse</code> 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 as <code>int i = parse!int(line)</code>. But if I were to do <code>string s = parse!string(line)</code> it would grab the entire line.</p> <p>I cold parse this by hand, using <code>char**</code> (or, <code>ref string</code>) as a datatype, just like I did when I wrote this in C. But I'm learning D to not have to.</p> <p>I tried something like this to do it manually:</p> <pre><code>string get_word(ref string s) { int i = 0; while (i &lt; s.length &amp;&amp; isAlphaNum(s[i])) i++; string word = s[0 .. i]; s = s[i+1 .. $]; return word; } </code></pre> <p>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 <code>i+1</code> index always exists.</p> <p>Thanks for the help!</p> <p>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.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload