x::interval
is a template class for generic parsing of multidimensional
numeric values, using localized strings as dimension names.
This template class is used by other interval string parsers,
but is useful enough for other purposes.
#include <x/locale.H> #include <x/messages.H> #include <x/interval.H> x::locale en_us(x::locale::create("en_us.UTF-8")); x::messages msgcat(x::messages::create(en_us), "application"); static const char *const units[]={"block", "blocks", "section", "sections", "lot", "lots", 0}; x::interval<int> i(units, 0, msgcat); std::vector>int< value(i.parse("3 blocks, 1 section, 1 lot"));
The first parameter to x::interval
's
constructor is a read-only array of strings. The number of strings must
be even, and the last one followed by a null pointer. Each pair of
strings specifies the name of a dimension, in the single and plural
form. The single and the plural form is used to look up the localized
dimensional name in the
message catalog.
The above example passes an array of six strings, or three dimensions.
The second aprameter to the constructor is the 0-based index of the
default dimension. In the above example the default dimension parameter
may be between 0 and 2, inclusive.
parse
() parses a character
string, which must consists of pair of names and values. Each value
gets associated with a dimension specified by the name. The value
may appear before or after the corresponding name.
parse
() places each value in a vector sized
to the number of dimensions, with the value being placed in the
appropriate index in the vector.
The above example returns a vector of three values: element 0 is
the block value, element 1 is the section value, element 2 is the
lot value.
If the parsed string does not specify a value for some particular
dimension, its value is 0.
If the parsed string consists of a single numeric value, without a
dimension name, the default dimension index specifies its dimension,
and the corresponding vector element.