tango.text.convert.Integer

License:

BSD style: see license.txt

Version:

Initial release: Nov 2005

Author:

Kris

A set of functions for converting between string and integer values.

Applying the D "import alias" mechanism to this module is highly recommended, in order to limit namespace pollution:
1
2
3
import Integer = tango.text.convert.Integer;

auto i = Integer.parse ("32767");
int toInt(T)(const(T[]) digits, uint radix = 0)
Parse an integer value from the provided 'digits' string.
The string is inspected for a sign and an optional radix prefix. A radix may be provided as an argument instead, whereupon it must match the prefix (where present). When radix is set to zero, conversion will default to decimal.

Throws:

IllegalArgumentException where the input text is not parsable in its entirety.

See Also:

the low level functions parse() and convert()
long toLong(T)(const(T[]) digits, uint radix = 0)
Parse an integer value from the provided 'digits' string.
The string is inspected for a sign and an optional radix prefix. A radix may be provided as an argument instead, whereupon it must match the prefix (where present). When radix is set to zero, conversion will default to decimal.

Throws:

IllegalArgumentException where the input text is not parsable in its entirety.

See Also:

the low level functions parse() and convert()
ulong toUlong(T)(const(T[]) digits, uint radix = 0)
Parse an unsignedinteger value from the provided 'digits' string.
The string is inspected for an optional radix prefix. A radix may be provided as an argument instead, whereupon it must match the prefix (where present). When radix is set to zero, conversion will default to decimal.

Throws:

IllegalArgumentException where the input text is not parsable in its entirety.

See Also:

the low level functions parse() and convert()
char[] toString(long i, const(char[]) fmt = null)
Wrapper to make life simpler. Returns a text version of the provided value.
See format() for details
wchar[] toString16(long i, const(wchar[]) fmt = null)
Wrapper to make life simpler. Returns a text version of the provided value.
See format() for details
dchar[] toString32(long i, const(dchar[]) fmt = null)
Wrapper to make life simpler. Returns a text version of the provided value.
See format() for details
T[] format(T)(T[] dst, long i, const(T[]) fmt = null)
Supports format specifications via an array, where format follows the notation given below:
1
type width prefix
Type is one of [d, g, u, b, x, o] or uppercase equivalent, and dictates the conversion radix or other semantics.

Width is optional and indicates a minimum width for zero-padding, while the optional prefix is one of ['#', ' ', '+'] and indicates what variety of prefix should be placed in the output. e.g.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
"d"      integer
"u"      unsigned
"o"      octal
"b"      binary
"x"      hexadecimal
"X"      hexadecimal uppercase

"d+"     integer prefixed with "+"
"b#"     binary prefixed with "0b"
"x#"     hexadecimal prefixed with "0x"
"X#"     hexadecimal prefixed with "0X"

"d8"     decimal padded to 8 places as required
"b8"     binary padded to 8 places as required
"b8#"    binary padded to 8 places and prefixed with "0b"

Note that the specified width is exclusive of the prefix, though the width padding will be shrunk as necessary in order to ensure a requested prefix can be inserted into the provided output.
long parse(T)(T[] digits, uint radix = 0, size_t* ate = null)
Parse an integer value from the provided 'digits' string.
The string is inspected for a sign and an optional radix prefix. A radix may be provided as an argument instead, whereupon it must match the prefix (where present). When radix is set to zero, conversion will default to decimal.

A non-null 'ate' will return the number of characters used to construct the returned value.

Throws:

none. The 'ate' param should be checked for valid input.
ulong convert(T)(const(T[]) digits, uint radix = 10, size_t* ate = null)
Convert the provided 'digits' into an integer value, without checking for a sign or radix. The radix defaults to decimal (10).
Returns the value and updates 'ate' with the number of characters consumed.

Throws:

none. The 'ate' param should be checked for valid input.
size_t trim(T)(const(T[]) digits, ref bool sign, ref uint radix)
Strip leading whitespace, extract an optional +/- sign, and an optional radix prefix. If the radix value matches an optional prefix, or the radix is zero, the prefix will be consumed and assigned. Where the radix is non zero and does not match an explicit prefix, the latter will remain unconsumed. Otherwise, radix will default to 10.
Returns the number of characters consumed.
uint atoi(T)(T[] s, int radix = 10)
quick & dirty text-to-unsigned int converter. Use only when you know what the content is, or use parse() or convert() instead.
Return the parsed uint
T[] itoa(T)(T[] output, uint value, int radix = 10)
quick & dirty unsigned to text converter, where the provided output must be large enough to house the result (10 digits in the largest case). For mainstream use, consider utilizing format() instead.
Returns a populated slice of the provided output
T[] consume(T)(T[] src, bool fp = false)
Consume a number from the input without converting it. Argument 'fp' enables floating-point consumption. Supports hex input for numbers which are prefixed appropriately
Since version 0.99.9