public static class InternalFormat.Formatter
extends java.lang.Object
implements java.lang.Appendable
| Modifier and Type | Field and Description |
|---|---|
protected boolean |
bytes
Signals the client's intention to make a PyString (or other byte-like) interpretation of
result, rather than a PyUnicode one. |
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 |
mark
The start of the formatted data for padding purposes, <=
start |
protected java.lang.StringBuilder |
result
The (partial) result.
|
protected InternalFormat.Spec |
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.
|
| Constructor and Description |
|---|
Formatter(InternalFormat.Spec spec,
int width)
Construct the formatter from a specification and initial buffer capacity.
|
Formatter(java.lang.StringBuilder result,
InternalFormat.Spec spec)
Construct the formatter from a client-supplied buffer and a specification.
|
| Modifier and Type | Method and Description |
|---|---|
static PyException |
alignmentNotAllowed(char align,
java.lang.String forType)
Convenience method returning a
Py.ValueError reporting that the given alignment
flag is not allowed in a format specifier for the named type. |
static PyException |
alternateFormNotAllowed(java.lang.String forType)
Convenience method returning a
Py.ValueError reporting that alternate form is not
allowed in a format specifier for the named type. |
static PyException |
alternateFormNotAllowed(java.lang.String forType,
char code)
Convenience method returning a
Py.ValueError reporting that alternate form is not
allowed in a format specifier for the named type and specified typoe code. |
InternalFormat.Formatter |
append(char c) |
InternalFormat.Formatter |
append(java.lang.CharSequence csq) |
InternalFormat.Formatter |
append(java.lang.CharSequence csq,
int start,
int end) |
PyString |
getPyResult()
|
java.lang.String |
getResult()
Current (possibly final) result of the formatting, as a
String. |
protected void |
groupDigits(int groupSize,
char comma)
Insert grouping characters (conventionally commas) into the whole part of the number.
|
boolean |
isBytes()
Whether initialised for a byte-like interpretation.
|
protected void |
makeSpaceAt(int pos,
int size)
Make a space in
result of a certain size and position. |
static PyException |
notAllowed(java.lang.String outrage,
java.lang.String forType)
Convenience method returning a
Py.ValueError reporting that some format specifier
feature is not allowed for the named data type. |
static PyException |
notAllowed(java.lang.String outrage,
java.lang.String forType,
char code)
Convenience method returning a
Py.ValueError reporting that some format specifier
feature is not allowed for the named format code and data type. |
InternalFormat.Formatter |
pad()
|
protected void |
pad(int leftIndex,
int n)
|
static PyException |
precisionNotAllowed(java.lang.String forType)
Convenience method returning a
Py.ValueError reporting that specifying a
precision is not allowed in a format specifier for the named type. |
static PyException |
precisionTooLarge(java.lang.String type)
Convenience method returning a
Py.OverflowError reporting: |
protected void |
reset()
Clear the instance variables describing the latest object in
result, ready to
receive a new one. |
protected int[] |
sectionLengths()
Supports
toString() by returning the lengths of the successive sections in the
result buffer, used for navigation relative to start. |
void |
setBytes(boolean bytes)
Signals the client's intention to make a PyString (or other byte-like) interpretation of
result, rather than a PyUnicode one. |
void |
setStart()
|
static PyException |
signNotAllowed(java.lang.String forType,
char code)
Convenience method returning a
Py.ValueError reporting that specifying a sign is
not allowed in a format specifier for the named type. |
java.lang.String |
toString() |
static PyException |
unknownFormat(char code,
java.lang.String forType)
Convenience method returning a
Py.ValueError reporting: |
protected void |
uppercase()
Convert letters in the representation of the current number (in
result) to upper
case. |
protected void |
zeroPadAfterSignWithGroupingFixup(int groupSize,
char comma)
|
static PyException |
zeroPaddingNotAllowed(java.lang.String forType)
Convenience method returning a
Py.ValueError reporting that zero padding is not
allowed in a format specifier for the named type. |
protected final InternalFormat.Spec spec
protected java.lang.StringBuilder result
protected boolean bytes
result, rather than a PyUnicode one.protected int mark
startprotected int start
protected int lenSign
protected int lenWhole
public Formatter(java.lang.StringBuilder result,
InternalFormat.Spec spec)
mark and start to the end of the buffer. The new formatted object will
therefore be appended there and, when the time comes, padding will be applied to (just)
the new text.result - destination bufferspec - parsed conversion specificationpublic Formatter(InternalFormat.Spec spec, int width)
mark to the end of the buffer.spec - parsed conversion specificationwidth - of buffer initiallypublic void setBytes(boolean bytes)
result, rather than a PyUnicode one. Only formatters that could produce
characters >255 are affected by this (e.g. c-format). Idiom:
MyFormatter f = new MyFormatter( InternalFormatter.fromText(formatSpec) ); f.setBytes(!(formatSpec instanceof PyUnicode)); // ... formatting work return f.getPyResult();
bytes - true to signal the intention to make a byte-like interpretationpublic boolean isBytes()
public java.lang.String getResult()
String.public PyString getPyResult()
PyObject, either PyString or PyUnicode according to
bytes.public InternalFormat.Formatter append(char c)
append in interface java.lang.Appendablepublic InternalFormat.Formatter append(java.lang.CharSequence csq)
append in interface java.lang.Appendablepublic InternalFormat.Formatter append(java.lang.CharSequence csq, int start, int end) throws java.lang.IndexOutOfBoundsException
append in interface java.lang.Appendablejava.lang.IndexOutOfBoundsExceptionpublic void setStart()
result, ready to
receive a new one: sets start and calls reset(). This is necessary when
a Formatter is to be re-used. Note that this leaves mark where it
is. In the core, we need this to support complex: two floats in the same
format, but padded as a unit.protected void reset()
result, ready to
receive a new one. This is called from setStart(). Subclasses override this
method and call setStart() at the start of their format method.protected int[] sectionLengths()
toString() by returning the lengths of the successive sections in the
result buffer, used for navigation relative to start. The toString
method shows a '|' character between each section when it prints out the buffer. Override
this when you define more lengths in the subclass.public java.lang.String toString()
Overridden to provide a debugging view in which the actual text is shown divided up by
the len* member variables. If the dividers don't look right, those variables
have not remained consistent with the text.
toString in class java.lang.Objectprotected void groupDigits(int groupSize,
char comma)
lenWhole will increase correspondingly.groupSize - normally 3.comma - or some other character to use as a separator.protected void makeSpaceAt(int pos,
int size)
result of a certain size and position. On return, the segment
lengths are likely to be invalid until the caller adjusts them corresponding to the
insertion. There is no guarantee what the opened space contains.pos - at which to make the spacesize - of the spaceprotected void uppercase()
result) to upper
case.public InternalFormat.Formatter pad()
result from mark to
the end) using the alignment, target width and fill character defined in spec.
The action of padding will increase the length of this segment to the target width, if
that is greater than the current length.
When the padding method has decided that that it needs to add n padding characters, it
will affect start or lenWhole as follows.
| align | meaning | start | lenWhole | result.length() |
|---|---|---|---|---|
< |
left-aligned | +0 | +0 | +n |
> |
right-aligned | +n | +0 | +n |
^ |
centred | +(n/2) | +0 | +n |
= |
pad after sign | +0 | +n | +n |
result[mark:].) When this would not be
appropriate, it is up to the client to disallow this (which complex does).protected void pad(int leftIndex,
int n)
result from argument
leftIndex to the end) using the alignment, by n repetitions of
the fill character defined in spec, and distributed according to
spec.align. The value of leftIndex is only used if the
alignment is '>' (left) or '^' (both). The value of the critical lengths (lenWhole,
lenSign, etc.) are not affected, because we assume that leftIndex <=
start.leftIndex - the index in result at which to insert left-fill characters.n - number of fill characters to insert.protected void zeroPadAfterSignWithGroupingFixup(int groupSize,
char comma)
result in the special
case where a sign-aware padding (spec.align='=') was requested, the
fill character is '0', and the digits are to be grouped. In these exact
circumstances, the grouping, which must already have been applied to the (whole part)
number itself, has to be extended into the zero-padding.
>>> 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'
groupSize - normally 3.comma - or some other character to use as a separator.public static PyException unknownFormat(char code, java.lang.String forType)
Py.ValueError reporting:
"Unknown format code '"+code+"' for object of type '"+forType+"'"
code - the presentation typeforType - the type it was found applied topublic static PyException alternateFormNotAllowed(java.lang.String forType)
Py.ValueError reporting that alternate form is not
allowed in a format specifier for the named type.forType - the type it was found applied topublic static PyException alternateFormNotAllowed(java.lang.String forType, char code)
Py.ValueError reporting that alternate form is not
allowed in a format specifier for the named type and specified typoe code.forType - the type it was found applied tocode - the formatting code (or '\0' not to mention one)public static PyException alignmentNotAllowed(char align, java.lang.String forType)
Py.ValueError reporting that the given alignment
flag is not allowed in a format specifier for the named type.align - type of alignmentforType - the type it was found applied topublic static PyException signNotAllowed(java.lang.String forType, char code)
Py.ValueError reporting that specifying a sign is
not allowed in a format specifier for the named type.forType - the type it was found applied tocode - the formatting code (or '\0' not to mention one)public static PyException precisionNotAllowed(java.lang.String forType)
Py.ValueError reporting that specifying a
precision is not allowed in a format specifier for the named type.forType - the type it was found applied topublic static PyException zeroPaddingNotAllowed(java.lang.String forType)
Py.ValueError reporting that zero padding is not
allowed in a format specifier for the named type.forType - the type it was found applied topublic static PyException notAllowed(java.lang.String outrage, java.lang.String forType)
Py.ValueError reporting that some format specifier
feature is not allowed for the named data type.outrage - committed in the present caseforType - the data type (e.g. "integer") it where it is an outragepublic static PyException notAllowed(java.lang.String outrage, java.lang.String forType, char code)
Py.ValueError reporting that some format specifier
feature is not allowed for the named format code and data type. Produces a message like:
outrage+" not allowed with "+forType+" format specifier '"+code+"'"
outrage+" not allowed in "+forType+" format specifier"
outrage - committed in the present caseforType - the data type (e.g. "integer") it where it is an outragecode - the formatting code for which it is an outrage (or '\0' not to mention one)public static PyException precisionTooLarge(java.lang.String type)
Py.OverflowError reporting:
"formatted "+type+" is too long (precision too large?)"
type - of formatting ("integer", "float")