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, U = uint)(T[] digits, U 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, U = uint)(T[] digits, U 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()
char[] toString(long i, char[] fmt = null) #
Wrapper to make life simpler. Returns a text version of the provided value.
See format() for details
wchar[] toString16(long i, wchar[] fmt = null) #
Wrapper to make life simpler. Returns a text version of the provided value.
See format() for details
dchar[] toString32(long i, dchar[] fmt = null) #
Wrapper to make life simpler. Returns a text version of the provided value.
See format() for details
T[] format(T, U = long)(T[] dst, U i, 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, U = uint)(T[] digits, U radix = 0, uint* 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, U = uint)(T[] digits, U radix = 10, uint* 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.
uint trim(T, U = uint)(T[] digits, ref bool sign, ref U 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, U = uint)(T[] output, U 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