Top Description Inners Fields Constructors Methods
org.python.core.stringlib

public abstract Class IntegerFormatter

extends AbstractFormatter
Class Inheritance
Known Direct Subclasses
org.python.core.stringlib.IntegerFormatter.Traditional, org.python.core.PyLong.Formatter
Imports
java.math.BigInteger, org.python.core.stringlib.InternalFormat.FormatError, .InternalFormat.FormatOverflow, .InternalFormat.Spec

A class that provides the implementation of integer formatting. In a limited way, it acts like a StringBuilder to which text and one or more numbers may be appended, formatted according to the format specifier supplied at construction. These are ephemeral objects that are not, on their own, thread safe.

Nested and Inner Type Summary

Modifier and TypeClass and Description
public abstract static class
IntegerFormatter.Traditional

A minor variation on IntegerFormatter to handle "traditional" %-formatting.

Field Summary

Modifier and TypeField and Description
private static final BigInteger
private static final BigInteger
private static final String
Inherited from org.python.core.stringlib.InternalFormat.AbstractFormatter:
byteslenSignlenWholemarkresultspecstart

Constructor Summary

AccessConstructor and Description
public
IntegerFormatter(StringBuilder
destination buffer
result
,
InternalFormat.Spec
parsed conversion specification
spec
)

Construct the formatter from a client-supplied buffer, to which the result will be appended, and a specification.

public
IntegerFormatter(InternalFormat.Spec
parsed conversion specification
spec
)

Construct the formatter from a specification, allocating a buffer internally for the result.

Method Summary

Modifier and TypeMethod and Description
public IntegerFormatter
append(char
The character to append
c
)

Overrides org.python.core.stringlib.InternalFormat.AbstractFormatter.append.

Implements java.lang.Appendable.append.

Appends the specified character to this Appendable.
public IntegerFormatter
append(CharSequence
The character sequence to append. If csq is null, then the four characters "null" are appended to this Appendable.
csq
)

Overrides org.python.core.stringlib.InternalFormat.AbstractFormatter.append.

Implements java.lang.Appendable.append.

Appends the specified character sequence to this Appendable.
public IntegerFormatter
append(CharSequence
The character sequence from which a subsequence will be appended. If csq is null, then characters will be appended as if csq contained the four characters "null".
csq
,
int
The index of the first character in the subsequence
start
,
int
The index of the character following the last character in the subsequence
end
)

Overrides org.python.core.stringlib.InternalFormat.AbstractFormatter.append.

Implements java.lang.Appendable.append.

Appends a subsequence of the specified character sequence to this Appendable.
pack-priv void
appendNumber(String
to append
number
)

Append a string (number) to result and set lenWhole to its length .

public IntegerFormatter

Returns:

this object
format
(BigInteger
to convert
value
)

Format a BigInteger, which is the implementation type of Jython long, according to the specification represented by this IntegerFormatter.

pack-priv void
format_b(BigInteger
to convert
value
)

Format the value as binary (into result).

pack-priv void
format_c(BigInteger
to convert
value
)

Format the value as a character (into result).

pack-priv void
format_d(BigInteger
to convert
value
)

Format the value as decimal (into result).

pack-priv void
format_o(BigInteger
to convert
value
)

Format the value as octal (into result).

pack-priv void
format_x(BigInteger
to convert
value
,
boolean
if the hexadecimal should be upper case
upper
)

Format the value as hexadecimal (into result), with the option of using upper-case or lower-case letters.

pack-priv final void
negativeSign(String
marker ("0x" or "0X" for hex, "0" for octal, null or "" for decimal.
base
)

Append to result buffer a minus sign and, in alternate mode, the base marker provided.

pack-priv final void
positiveSign(String
marker "0x" or "0X" for hex, "0o" for octal, "0b" for binary, "" or null for decimal.
base
)

Append to result buffer a sign (if one is specified for positive numbers) and, in alternate mode, the base marker provided.

private static final String

Returns:

the binary representation of value, with "-" sign prepended if necessary
toBinaryString
(BigInteger
the value to generate a binary string from
value
)

A more efficient algorithm for generating a binary representation of a byte array.

private static final String

Returns:

the hexadecimal representation of value, with "-" sign prepended if necessary
toHexString
(BigInteger
the value to generate a hexadecimal string from
value
)

A more efficient algorithm for generating a hexadecimal representation of a byte array.

private static final String

Returns:

the octal representation of value, with "-" sign prepended if necessary
toOctalString
(BigInteger
the value to generate an octal string from
value
)

A more efficient algorithm for generating an octal representation of a byte array.

Inherited from org.python.core.stringlib.InternalFormat.AbstractFormatter:
alignmentNotAllowedalternateFormNotAllowedalternateFormNotAllowedappendformatgetResultgroupDigitsisBytesmakeSpaceAtnotAllowednotAllowednotAllowedpadpadprecisionNotAllowedprecisionTooLargeresetsectionLengthssetBytessetStartsignNotAllowedtoStringunknownFormatuppercasezeroPadAfterSignWithGroupingFixupzeroPaddingNotAllowed

Field Detail

LIMIT_BYTEback to summary
private static final BigInteger LIMIT_BYTE
LIMIT_UNICODEback to summary
private static final BigInteger LIMIT_UNICODE
LOOKUPback to summary
private static final String LOOKUP

Constructor Detail

IntegerFormatterback to summary
public IntegerFormatter(StringBuilder result, InternalFormat.Spec spec)

Construct the formatter from a client-supplied buffer, to which the result will be appended, and a specification. Sets mark to the end of the buffer.

Parameters
result:StringBuilder

destination buffer

spec:InternalFormat.Spec

parsed conversion specification

IntegerFormatterback to summary
public IntegerFormatter(InternalFormat.Spec spec)

Construct the formatter from a specification, allocating a buffer internally for the result.

Parameters
spec:InternalFormat.Spec

parsed conversion specification

Method Detail

appendback to summary
public IntegerFormatter append(char c)

Overrides org.python.core.stringlib.InternalFormat.AbstractFormatter.append.

Implements java.lang.Appendable.append.

Doc from java.lang.Appendable.append.

Appends the specified character to this Appendable.

Parameters
c:char

The character to append

Returns:IntegerFormatter

A reference to this Appendable

Annotations
@Override
appendback to summary
public IntegerFormatter append(CharSequence csq)

Overrides org.python.core.stringlib.InternalFormat.AbstractFormatter.append.

Implements java.lang.Appendable.append.

Doc from java.lang.Appendable.append.

Appends the specified character sequence to this Appendable.

Depending on which class implements the character sequence csq, the entire sequence may not be appended. For instance, if csq is a java.nio.CharBuffer then the subsequence to append is defined by the buffer's position and limit.

Parameters
csq:CharSequence

The character sequence to append. If csq is null, then the four characters "null" are appended to this Appendable.

Returns:IntegerFormatter

A reference to this Appendable

Annotations
@Override
appendback to summary
public IntegerFormatter append(CharSequence csq, int start, int end) throws IndexOutOfBoundsException

Overrides org.python.core.stringlib.InternalFormat.AbstractFormatter.append.

Implements java.lang.Appendable.append.

Doc from java.lang.Appendable.append.

Appends a subsequence of the specified character sequence to this Appendable.

An invocation of this method of the form out.append(csq, start, end) when csq is not null, behaves in exactly the same way as the invocation

    out.append(csq.subSequence(start, end)) 
Parameters
csq:CharSequence

The character sequence from which a subsequence will be appended. If csq is null, then characters will be appended as if csq contained the four characters "null".

start:int

The index of the first character in the subsequence

end:int

The index of the character following the last character in the subsequence

Returns:IntegerFormatter

A reference to this Appendable

Annotations
@Override
Exceptions
IndexOutOfBoundsException:
If start or end are negative, start is greater than end, or end is greater than csq.length()
appendNumberback to summary
pack-priv void appendNumber(String number)

Append a string (number) to result and set lenWhole to its length .

Parameters
number:String

to append

formatback to summary
public IntegerFormatter format(BigInteger value) throws FormatOverflow, FormatError

Format a BigInteger, which is the implementation type of Jython long, according to the specification represented by this IntegerFormatter. The conversion type, and flags for grouping or base prefix are dealt with here. At the point this is used, we know the spec is one of the integer types.

Parameters
value:BigInteger

to convert

Returns:IntegerFormatter

this object

Exceptions
FormatOverflow:
if a value is out of range (including the precision)
FormatError:
if an unsupported format character is encountered
format_bback to summary
pack-priv void format_b(BigInteger value)

Format the value as binary (into result). The options for mandatory sign and for the presence of a base-prefix "0b" are dealt with by reference to the format specification.

Parameters
value:BigInteger

to convert

format_cback to summary
pack-priv void format_c(BigInteger value) throws FormatOverflow

Format the value as a character (into result).

Parameters
value:BigInteger

to convert

Exceptions
FormatOverflow:
if value out of range
format_dback to summary
pack-priv void format_d(BigInteger value)

Format the value as decimal (into result). The option for mandatory sign is dealt with by reference to the format specification.

Parameters
value:BigInteger

to convert

format_oback to summary
pack-priv void format_o(BigInteger value)

Format the value as octal (into result). The options for mandatory sign and for the presence of a base-prefix "0o" are dealt with by reference to the format specification.

Parameters
value:BigInteger

to convert

format_xback to summary
pack-priv void format_x(BigInteger value, boolean upper)

Format the value as hexadecimal (into result), with the option of using upper-case or lower-case letters. The options for mandatory sign and for the presence of a base-prefix "0x" or "0X" are dealt with by reference to the format specification.

Parameters
value:BigInteger

to convert

upper:boolean

if the hexadecimal should be upper case

negativeSignback to summary
pack-priv final void negativeSign(String base)

Append to result buffer a minus sign and, in alternate mode, the base marker provided. The sign and base marker are together considered to be the "sign" of the converted number, spanned by lenSign. This is relevant when we come to insert padding.

Parameters
base:String

marker ("0x" or "0X" for hex, "0" for octal, null or "" for decimal.

positiveSignback to summary
pack-priv final void positiveSign(String base)

Append to result buffer a sign (if one is specified for positive numbers) and, in alternate mode, the base marker provided. The sign and base marker are together considered to be the "sign" of the converted number, spanned by lenSign. This is relevant when we come to insert padding.

Parameters
base:String

marker "0x" or "0X" for hex, "0o" for octal, "0b" for binary, "" or null for decimal.

toBinaryStringback to summary
private static final String toBinaryString(BigInteger value)

A more efficient algorithm for generating a binary representation of a byte array. BigInteger#toString(int) is too slow because it generalizes to any radix and, consequently, is implemented using expensive mathematical operations.

Parameters
value:BigInteger

the value to generate a binary string from

Returns:String

the binary representation of value, with "-" sign prepended if necessary

toHexStringback to summary
private static final String toHexString(BigInteger value)

A more efficient algorithm for generating a hexadecimal representation of a byte array. BigInteger#toString(int) is too slow because it generalizes to any radix and, consequently, is implemented using expensive mathematical operations.

Parameters
value:BigInteger

the value to generate a hexadecimal string from

Returns:String

the hexadecimal representation of value, with "-" sign prepended if necessary

toOctalStringback to summary
private static final String toOctalString(BigInteger value)

A more efficient algorithm for generating an octal representation of a byte array. BigInteger#toString(int) is too slow because it generalizes to any radix and, consequently, is implemented using expensive mathematical operations.

Parameters
value:BigInteger

the value to generate an octal string from

Returns:String

the octal representation of value, with "-" sign prepended if necessary

org.python.core.stringlib back to summary

public abstract Class IntegerFormatter.Traditional

extends IntegerFormatter
Class Inheritance

A minor variation on IntegerFormatter to handle "traditional" %-formatting. The difference is in support for spec.precision, the formatting octal in "alternate" mode (0 and 0123, not 0o0 and 0o123), and in c-format (in the error logic).

Constructor Summary

AccessConstructor and Description
public
Traditional(StringBuilder
destination buffer
result
,
InternalFormat.Spec
parsed conversion specification
spec
)

Construct the formatter from a client-supplied buffer, to which the result will be appended, and a specification.

public
Traditional(InternalFormat.Spec
parsed conversion specification
spec
)

Construct the formatter from a specification, allocating a buffer internally for the result.

Method Summary

Modifier and TypeMethod and Description
pack-priv void
appendNumber(String
to append
number
)

Overrides org.python.core.stringlib.IntegerFormatter.appendNumber.

Append a string (number) to result, but insert leading zeros first in order that, on return, the whole-part length #lenWhole should be no less than the precision.
pack-priv void
appendOctalNumber(String
to append
number
)

Append a string (number) to result, but insert leading zeros first in order that, on return, the whole-part length #lenWhole should be no less than the precision.

pack-priv void
format_c(BigInteger
to convert
value
)

Overrides org.python.core.stringlib.IntegerFormatter.format_c.

Format the value as a character (into result).
pack-priv void
format_o(BigInteger
to convert
value
)

Overrides org.python.core.stringlib.IntegerFormatter.format_o.

Format the value as octal (into result).
Inherited from org.python.core.stringlib.IntegerFormatter:
appendappendappendformatformat_bformat_dformat_xnegativeSignpositiveSign

Constructor Detail

Traditionalback to summary
public Traditional(StringBuilder result, InternalFormat.Spec spec)

Construct the formatter from a client-supplied buffer, to which the result will be appended, and a specification. Sets mark to the end of the buffer.

Parameters
result:StringBuilder

destination buffer

spec:InternalFormat.Spec

parsed conversion specification

Traditionalback to summary
public Traditional(InternalFormat.Spec spec)

Construct the formatter from a specification, allocating a buffer internally for the result.

Parameters
spec:InternalFormat.Spec

parsed conversion specification

Method Detail

appendNumberback to summary
pack-priv void appendNumber(String number)

Overrides org.python.core.stringlib.IntegerFormatter.appendNumber.

Append a string (number) to result, but insert leading zeros first in order that, on return, the whole-part length #lenWhole should be no less than the precision.

Parameters
number:String

to append

Annotations
@Override
appendOctalNumberback to summary
pack-priv void appendOctalNumber(String number)

Append a string (number) to result, but insert leading zeros first in order that, on return, the whole-part length #lenWhole should be no less than the precision. Octal numbers must begin with a zero if spec.alternate==true, so if the number passed in does not start with a zero, at least one will be inserted.

Parameters
number:String

to append

format_cback to summary
pack-priv void format_c(BigInteger value) throws FormatOverflow

Overrides org.python.core.stringlib.IntegerFormatter.format_c.

Format the value as a character (into result).

Parameters
value:BigInteger

to convert

Annotations
@Override
Exceptions
FormatOverflow:
if value out of range
format_oback to summary
pack-priv void format_o(BigInteger value)

Overrides org.python.core.stringlib.IntegerFormatter.format_o.

Format the value as octal (into result). The options for mandatory sign and for the presence of a base-prefix "0" are dealt with by reference to the format specification.

Parameters
value:BigInteger

to convert

Annotations
@Override