rtQuantityParser

Creates a runtime parser capable of working on user-defined units and prefixes.

template rtQuantityParser(N, alias symbolList, alias parseFun = (ref string s) => parse!N(s))
rtQuantityParser
(
Q
S
)
(
S str
)

Members

Functions

rtQuantityParser
auto rtQuantityParser(S str)
Undocumented in source. Be warned that the author may not have intended to support it.

Parameters

N

The type of the value type stored in the Quantity struct.

symbolList

A prefilled SymbolList struct that contains all units and prefixes.

parseFun

A function that can parse the beginning of a string to return a numeric value of type N. After this function returns, it must have consumed the numeric part and leave only the unit part.

Examples

import std.bigint;

enum bit = unit!(BigInt, "bit");
alias BinarySize = typeof(bit);

SymbolList!BigInt symbolList;
symbolList.addUnit("bit", bit);
symbolList.addPrefix("hob", BigInt("1234567890987654321"));

static BigInt parseFun(ref string input)
{
    import std.exception, std.regex;
    enum rgx = ctRegex!`^(\d*)\s*(.*)$`;
    auto m = enforce(match(input, rgx));
    input = m.captures[2];
    return BigInt(m.captures[1]);
}

alias parse = rtQuantityParser!(BigInt, symbolList, parseFun);

auto foo = BigInt("1234567890987654300") * bit;
foo += BigInt(21) * bit;
assert(foo == parse!BinarySize("1 hobbit"));

Meta