parse

Parses an associative array from a string given the left bracket (default '['), right bracket (default ']'), key-value separator (default ':'), and element seprator (by default ',').

  1. auto parse(Source source)
  2. auto parse(Source s)
  3. auto parse(Source source, uint radix)
  4. auto parse(Source s)
  5. auto parse(Source source)
  6. auto parse(Source s)
  7. auto parse(Source s)
  8. auto parse(Source s)
  9. auto parse(Source s, dchar lbracket, dchar rbracket, dchar comma)
  10. auto parse(Source s, dchar lbracket, dchar rbracket, dchar comma)
  11. auto parse(Source s, dchar lbracket, dchar rbracket, dchar keyval, dchar comma)
    parse
    (
    Target
    Source
    Flag!"doCount" doCount = No.doCount
    )
    (
    ref Source s
    ,
    dchar lbracket = '['
    ,
    dchar rbracket = ']'
    ,
    dchar keyval = ':'
    ,
    dchar comma = ','
    )
    if (
    !is(Target == enum)
    &&
    &&
    !is(Source == enum)
    )

Parameters

s Source

the string to parse

lbracket dchar

the character that starts the associative array

rbracket dchar

the character that ends the associative array

keyval dchar

the character that associates the key with the value

comma dchar

the character that separates the elements of the associative array

doCount

the flag for deciding to report the number of consumed characters

Return Value

Type: auto
  • An associative array of type Target if doCount is set to No.doCount
  • A tuple containing an associative array of type Target and a size_t if doCount is set to Yes.doCount

Examples

import std.typecons : Flag, Yes, No, tuple;
import std.range.primitives : save;
import std.array : assocArray;
auto s1 = "[1:10, 2:20, 3:30]";
auto copyS1 = s1.save;
auto aa1 = parse!(int[int])(s1);
assert(aa1 == [1:10, 2:20, 3:30]);
assert(tuple([1:10, 2:20, 3:30], copyS1.length) == parse!(int[int], string, Yes.doCount)(copyS1));

auto s2 = `["aaa":10, "bbb":20, "ccc":30]`;
auto copyS2 = s2.save;
auto aa2 = parse!(int[string])(s2);
assert(aa2 == ["aaa":10, "bbb":20, "ccc":30]);
assert(tuple(["aaa":10, "bbb":20, "ccc":30], copyS2.length) ==
    parse!(int[string], string, Yes.doCount)(copyS2));

auto s3 = `["aaa":[1], "bbb":[2,3], "ccc":[4,5,6]]`;
auto copyS3 = s3.save;
auto aa3 = parse!(int[][string])(s3);
assert(aa3 == ["aaa":[1], "bbb":[2,3], "ccc":[4,5,6]]);
assert(tuple(["aaa":[1], "bbb":[2,3], "ccc":[4,5,6]], copyS3.length) ==
    parse!(int[][string], string, Yes.doCount)(copyS3));

auto s4 = `[]`;
int[int] emptyAA;
assert(tuple(emptyAA, s4.length) == parse!(int[int], string, Yes.doCount)(s4));

Meta