| Modifier and Type | Class and Description |
|---|---|
| public static class | InternalFormat.
A class that provides the base for implementations of type-specific formatting. |
| private static class | InternalFormat.
Parser for PEP-3101 field format specifications. |
| public static class | InternalFormat.
Parsed PEP-3101 format specification of a single field, encapsulating the format for use by formatting methods. |
| Access | Constructor and Description |
|---|---|
| public |
| Modifier and Type | Method and Description |
|---|---|
| public static InternalFormat. | |
| public static InternalFormat. |
| InternalFormat | back to summary |
|---|---|
| public InternalFormat() | |
| fromText | back to summary |
|---|---|
| public static InternalFormat. Create a
| |
| fromText | back to summary |
|---|---|
| public static InternalFormat. Create a
| |
| Modifier and Type | Field and Description |
|---|---|
| protected boolean | |
| protected int | lenSign
If it contains no sign, this length is zero, and >0 otherwise. |
| protected int | lenWhole
The length of the whole part (to left of the decimal point or exponent) |
| protected int | |
| protected StringBuilder | result
The (partial) result. |
| protected final InternalFormat. | spec
The specification according to which we format any number supplied to the method. |
| protected int | start
The latest number we are working on floats at the end of the result, and starts here. |
| Access | Constructor and Description |
|---|---|
| public | Formatter(StringBuilder
destination buffer result, InternalFormat.parsed conversion specification specConstruct the formatter from a client-supplied buffer and a specification. |
| public | Formatter(InternalFormat.
parsed conversion specification spec,of buffer initially width)Construct the formatter from a specification and initial buffer capacity. |
| Modifier and Type | Method and Description |
|---|---|
| public static PyException | Returns: exception to throwtype of alignment align, String the type it was found applied to forType)Convenience method returning a |
| public static PyException | Returns: exception to throwthe type it was found applied to forType)Convenience method returning a |
| public static PyException | Returns: exception to throwthe type it was found applied to forType, char the formatting code (or '\0' not to mention one) code)Convenience method returning a |
| public InternalFormat. | append(char
The character to append c)Implements java. Appends the specified character to this |
| public InternalFormat. | append(CharSequence
The character sequence to append. If csq)csq is
null, then the four characters "null" are
appended to this Appendable.Implements java. Appends the specified character sequence to this |
| public InternalFormat. | append(CharSequence
The character sequence from which a subsequence will be
appended. If csq, int csq is null, then characters
will be appended as if csq contained the four
characters "null".The index of the first character in the subsequence start, int The index of the character following the last character in the
subsequence end)Implements java. Appends a subsequence of the specified character sequence to this
|
| public PyString | Returns: formatted resultConvenience method to return the current result of the formatting, as a
|
| public String | |
| protected void | groupDigits(int
normally 3. groupSize, char or some other character to use as a separator. comma)Insert grouping characters (conventionally commas) into the whole part of the number. |
| public boolean | |
| protected void | makeSpaceAt(int
at which to make the space pos, int of the space size)Make a space in |
| public static PyException | Returns: exception to throwcommitted in the present case outrage, String the data type (e.g. "integer") it where it is an outrage forType)Convenience method returning a |
| public static PyException | Returns: exception to throwcommitted in the present case outrage, String the data type (e.g. "integer") it where it is an outrage forType, char the formatting code for which it is an outrage (or '\0' not to mention one) code)Convenience method returning a |
| public InternalFormat. | |
| protected void | pad(int
the index in result at which to insert left-fill characters. leftIndex, int number of fill characters to insert. n)Pad the last result (defined as the contents of |
| public static PyException | Returns: exception to throwthe type it was found applied to forType)Convenience method returning a |
| public static PyException | Returns: exception to throwof formatting ("integer", "float") type)Convenience method returning a
|
| protected void | |
| protected int[] | Returns: the lengths of the successive sectionsSupports |
| public void | |
| public void | |
| public static PyException | Returns: exception to throwthe type it was found applied to forType, char the formatting code (or '\0' not to mention one) code)Convenience method returning a |
| public String | |
| public static PyException | Returns: exception to throwthe presentation type code, String the type it was found applied to forType)Convenience method returning a
|
| protected void | |
| protected void | zeroPadAfterSignWithGroupingFixup(int
normally 3. groupSize, char or some other character to use as a separator. comma)Fix-up the zero-padding of the last formatted number in |
| public static PyException | Returns: exception to throwthe type it was found applied to forType)Convenience method returning a |
| bytes | back to summary |
|---|---|
| protected boolean bytes Signals the client's intention to make a PyString (or other byte-like) interpretation of
| |
| lenSign | back to summary |
|---|---|
| protected int lenSign If it contains no sign, this length is zero, and >0 otherwise. | |
| lenWhole | back to summary |
|---|---|
| protected int lenWhole The length of the whole part (to left of the decimal point or exponent) | |
| mark | back to summary |
|---|---|
| protected int mark The start of the formatted data for padding purposes, <= | |
| result | back to summary |
|---|---|
| protected StringBuilder result The (partial) result. | |
| spec | back to summary |
|---|---|
| protected final InternalFormat. The specification according to which we format any number supplied to the method. | |
| start | back to summary |
|---|---|
| protected int start The latest number we are working on floats at the end of the result, and starts here. | |
| Formatter | back to summary |
|---|---|
| public Formatter(StringBuilder result, InternalFormat. Construct the formatter from a client-supplied buffer and a specification. Sets
| |
| Formatter | back to summary |
|---|---|
| public Formatter(InternalFormat. Construct the formatter from a specification and initial buffer capacity. Sets
| |
| alignmentNotAllowed | back to summary |
|---|---|
| public static PyException alignmentNotAllowed(char align, String forType) Convenience method returning a
exception to throw | |
| alternateFormNotAllowed | back to summary |
|---|---|
| public static PyException alternateFormNotAllowed(String forType) Convenience method returning a
| |
| alternateFormNotAllowed | back to summary |
|---|---|
| public static PyException alternateFormNotAllowed(String forType, char code) Convenience method returning a
exception to throw | |
| append | back to summary |
|---|---|
| public InternalFormat. Implements java. Doc from java. Appends the specified character to this
| |
| append | back to summary |
|---|---|
| public InternalFormat. Implements java. Doc from java. Appends the specified character sequence to this Depending on which class implements the character sequence
The contents of this
| |
| append | back to summary |
|---|---|
| public InternalFormat. Implements java. Doc from java. Appends a subsequence of the specified character sequence to this
An invocation of this method of the form
out.append(csq.subSequence(start, end))
The contents of this
A reference to this
| |
| getPyResult | back to summary |
|---|---|
| public PyString getPyResult() Convenience method to return the current result of the formatting, as a
| |
| getResult | back to summary |
|---|---|
| public String getResult() Current (possibly final) result of the formatting, as a
| |
| groupDigits | back to summary |
|---|---|
| protected void groupDigits(int groupSize, char comma) Insert grouping characters (conventionally commas) into the whole part of the number.
| |
| isBytes | back to summary |
|---|---|
| public boolean isBytes() Whether initialised for a byte-like interpretation.
| |
| makeSpaceAt | back to summary |
|---|---|
| protected void makeSpaceAt(int pos, int size) Make a space in
| |
| notAllowed | back to summary |
|---|---|
| public static PyException notAllowed(String outrage, String forType) Convenience method returning a
exception to throw | |
| notAllowed | back to summary |
|---|---|
| public static PyException notAllowed(String outrage, String forType, char code) Convenience method returning a
exception to throw | |
| pad | back to summary | |||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| public InternalFormat. Pad the result so far (defined as the contents of
When the padding method has decided that that it needs to add n padding characters, it
will affect
result[mark:].) When this would not be
appropriate, it is up to the client to disallow this (which complex does).
| ||||||||||||||||||||||||||
| pad | back to summary |
|---|---|
| protected void pad(int leftIndex, int n) Pad the last result (defined as the contents of
| |
| precisionNotAllowed | back to summary |
|---|---|
| public static PyException precisionNotAllowed(String forType) Convenience method returning a
| |
| precisionTooLarge | back to summary |
|---|---|
| public static PyException precisionTooLarge(String type) Convenience method returning a
| |
| reset | back to summary |
|---|---|
| protected void reset() Clear the instance variables describing the latest object in | |
| sectionLengths | back to summary |
|---|---|
| protected int[] sectionLengths() Supports
| |
| setBytes | back to summary |
|---|---|
| public void setBytes(boolean bytes) Signals the client's intention to make a PyString (or other byte-like) interpretation of
MyFormatter f = new MyFormatter( InternalFormatter.fromText(formatSpec) ); f.setBytes(!(formatSpec instanceof PyUnicode)); // ... formatting work return f.getPyResult();
| |
| setStart | back to summary |
|---|---|
| public void setStart() Clear the instance variables describing the latest object in | |
| signNotAllowed | back to summary |
|---|---|
| public static PyException signNotAllowed(String forType, char code) Convenience method returning a
exception to throw | |
| toString | back to summary |
|---|---|
| public String toString() Overrides java. Doc from java. Returns a string representation of the object.
Overridden to provide a debugging view in which the actual text is shown divided up by
the | |
| unknownFormat | back to summary |
|---|---|
| public static PyException unknownFormat(char code, String forType) Convenience method returning a
exception to throw | |
| uppercase | back to summary |
|---|---|
| protected void uppercase() Convert letters in the representation of the current number (in | |
| zeroPadAfterSignWithGroupingFixup | back to summary |
|---|---|
| protected void zeroPadAfterSignWithGroupingFixup(int groupSize, char comma) Fix-up the zero-padding of the last formatted number in >>> format(-12e8, " =30,.3f") '- 1,200,000,000.000' >>> format(-12e8, "*=30,.3f") '-************1,200,000,000.000' >>> format(-12e8, "*>30,.3f") '************-1,200,000,000.000' >>> format(-12e8, "0>30,.3f") '000000000000-1,200,000,000.000' >>> format(-12e8, "0=30,.3f") '-0,000,000,001,200,000,000.000'The padding has increased the overall length of the result to the target width. About one in three calls to this method adds one to the width, because the whole part cannot start with a comma. >>> format(-12e8, " =30,.4f") '- 1,200,000,000.0000' >>> format(-12e8, "0=30,.4f") '-0,000,000,001,200,000,000.0000'
| |
| zeroPaddingNotAllowed | back to summary |
|---|---|
| public static PyException zeroPaddingNotAllowed(String forType) Convenience method returning a
| |
parse()
method that translates the format specification into an Spec object.
| Access | Constructor and Description |
|---|---|
| pack-priv |
| Modifier and Type | Method and Description |
|---|---|
| private boolean | |
| private boolean | |
| private boolean | |
| pack-priv InternalFormat. | |
| private int | |
| private boolean | scanPast(char c)
Test that the next character is exactly the one specified, and advance past it if it is. |
| Parser | back to summary |
|---|---|
| pack-priv Parser(String spec) Constructor simply holds the specification string ahead of the
| |
| isAlign | back to summary |
|---|---|
| private boolean isAlign() Test that the next character is one of the alignment characters. | |
| isAt | back to summary |
|---|---|
| private boolean isAt(String chars) Test that the next character is one of a specified set. | |
| isDigit | back to summary |
|---|---|
| private boolean isDigit() Test that the next character is a digit. | |
| parse | back to summary |
|---|---|
| pack-priv InternalFormat. Parse the specification with which this object was initialised into an
| |
| scanInteger | back to summary |
|---|---|
| private int scanInteger() The current character is a digit (maybe a sign). Scan the integer, | |
| scanPast | back to summary |
|---|---|
| private boolean scanPast(char c) Test that the next character is exactly the one specified, and advance past it if it is. | |
Spec objects may be merged such that one Spec provides values,
during the construction of a new Spec, for attributes that are unspecified in a
primary source.
This structure is returned by factory method fromText(String), and having public
final members is freely accessed by formatters such as FloatFormatter, and the
__format__ methods of client object types.
The fields correspond to the elements of a format specification. The grammar of a format specification is:
[[fill]align][sign][#][0][width][,][.precision][type]A typical idiom is:
private static final InternalFormatSpec FLOAT_DEFAULTS = InternalFormatSpec.from(">");
...
InternalFormat.Spec spec = InternalFormat.fromText(specString);
spec = spec.withDefaults(FLOAT_DEFAULTS);
... // Validation of spec.type, and other attributes, for this type.
FloatFormatter f = new FloatFormatter(spec);
String result = f.format(value).getResult();
| Modifier and Type | Field and Description |
|---|---|
| public final char | align
Alignment indicator is one of {'<', '^', '>', '=', or U+FFFF if unspecified. |
| public final boolean | alternate
The alternative format flag '#' was given. |
| public final char | fill
The fill character specified, or U+FFFF if unspecified. |
| public final boolean | grouping
Insert the grouping separator (which in Python always indicates a group-size of 3). |
| public static final char | NONE
Non-character code point used to represent "no value" in |
| public static final InternalFormat. | NUMERIC
Defaults applicable to most numeric types. |
| public final int | precision
Precision decoded from the format, or -1 if unspecified. |
| public final char | sign
Sign-handling flag, one of |
| public static final InternalFormat. | STRING
Defaults applicable to string types. |
| public final char | type
Type key from the format, or U+FFFF if unspecified. |
| public static final int | UNSPECIFIED
Negative value used to represent "no value" in |
| public final int | width
Width to which to pad the result, or -1 if unspecified. |
| Access | Constructor and Description |
|---|---|
| public | Spec(char
fill character (or fill, char NONEalignment indicator, one of {'<', '^', '>', '='} align, char policy, one of sign, boolean '+', '-', or ' '.true to request alternate formatting mode ( alternate, int '#' flag).of field after padding or -1 to default width, boolean true to request comma-separated groups grouping, int (e.g. decimal places) or -1 to default precision, char indicator character type)Constructor to set all the fields in the format specifier. |
| public | Spec(int
(e.g. decimal places) precision, char indicator character type)Constructor offering just precision and type. |
| Modifier and Type | Method and Description |
|---|---|
| public char | |
| public char | |
| public int | |
| public char | |
| public static final boolean | |
| public static final boolean | Returns: true only if the attribute is ≥0 (meaning that it has been specified).of attribute value)Test to see if an attribute has been specified. |
| public String | toString()
Overrides java. Return a format specifier (text) equivalent to the value of this Spec. |
| public InternalFormat. | Returns: a new Spec object.defaults to merge where this object does not specify the attribute. otherReturn a merged |
| align | back to summary |
|---|---|
| public final char align Alignment indicator is one of {'<', '^', '>', '=', or U+FFFF if unspecified. | |
| alternate | back to summary |
|---|---|
| public final boolean alternate The alternative format flag '#' was given. | |
| fill | back to summary |
|---|---|
| public final char fill The fill character specified, or U+FFFF if unspecified. | |
| grouping | back to summary |
|---|---|
| public final boolean grouping Insert the grouping separator (which in Python always indicates a group-size of 3). | |
| NONE | back to summary |
|---|---|
| public static final char NONE Non-character code point used to represent "no value" in | |
| NUMERIC | back to summary |
|---|---|
| public static final InternalFormat. Defaults applicable to most numeric types. Equivalent to " >" | |
| precision | back to summary |
|---|---|
| public final int precision Precision decoded from the format, or -1 if unspecified. | |
| sign | back to summary |
|---|---|
| public final char sign Sign-handling flag, one of | |
| STRING | back to summary |
|---|---|
| public static final InternalFormat. Defaults applicable to string types. Equivalent to " <" | |
| type | back to summary |
|---|---|
| public final char type Type key from the format, or U+FFFF if unspecified. | |
| UNSPECIFIED | back to summary |
|---|---|
| public static final int UNSPECIFIED Negative value used to represent "no value" in | |
| width | back to summary |
|---|---|
| public final int width Width to which to pad the result, or -1 if unspecified. | |
| Spec | back to summary |
|---|---|
| public Spec(char fill, char align, char sign, boolean alternate, int width, boolean grouping, int precision, char type) Constructor to set all the fields in the format specifier. [[fill]align][sign][#][0][width][,][.precision][type]
| |
| Spec | back to summary |
|---|---|
| public Spec(int precision, char type) Constructor offering just precision and type. [.precision][type]
| |
| getAlign | back to summary |
|---|---|
| public char getAlign(char defaultAlign) The alignment from the parsed format specification, or default. | |
| getFill | back to summary |
|---|---|
| public char getFill(char defaultFill) The alignment from the parsed format specification, or default. | |
| getPrecision | back to summary |
|---|---|
| public int getPrecision(int defaultPrecision) The precision from the parsed format specification, or default. | |
| getType | back to summary |
|---|---|
| public char getType(char defaultType) The type code from the parsed format specification, or default supplied. | |
| specified | back to summary |
|---|---|
| public static final boolean specified(char c) Test to see if an attribute has been specified.
| |
| specified | back to summary |
|---|---|
| public static final boolean specified(int value) Test to see if an attribute has been specified.
| |
| toString | back to summary |
|---|---|
| public String toString() Overrides java. Return a format specifier (text) equivalent to the value of this Spec.
| |
| withDefaults | back to summary |
|---|---|
| public InternalFormat. Return a merged
| |