Parse a String given some delimiters
sarn via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Sun Oct 30 16:21:59 PDT 2016
On Sunday, 30 October 2016 at 20:50:47 UTC, Alfred Newman wrote:
> Hello,
>
> I'm migrating some Python code to D, but I stuck at a dead
> end...
>
> Sorry to provide some .py lines over here, but I got some
> doubts about the best (fastest) way to do that in D.
The "splitter" generic function sounds like what you want. The
basic versions use a fixed separator, but you can make more
complex separators using either the regex version, or the
function version. The function version is simplest for what
you're doing. Check out the first example here:
https://dlang.org/phobos/std_algorithm_iteration.html#.splitter.3
(You'd use "among" instead of plain ==)
Also check out "walkLength" for getting the number of tokens.
However, if you really care about speed, I suggest changing the
API. With your API, if you want to get multiple tokens from a
string, you have to split the string every single time. Why not
just return the full range? You can use "array" to return a
proper array instead of an ad hoc range struct.
More information about the Digitalmars-d-learn
mailing list