Package org.python.core.stringlib
Class InternalFormat.Formatter
java.lang.Object
org.python.core.stringlib.InternalFormat.Formatter
- All Implemented Interfaces:
Appendable
- Direct Known Subclasses:
FloatFormatter
,IntegerFormatter
,TextFormatter
- Enclosing class:
- InternalFormat
A class that provides the base for implementations of type-specific 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.
-
Constructor Summary
ConstructorDescriptionFormatter
(StringBuilder result, InternalFormat.Spec spec) Construct the formatter from a client-supplied buffer and a specification.Formatter
(InternalFormat.Spec spec, int width) Construct the formatter from a specification and initial buffer capacity. -
Method Summary
Modifier and TypeMethodDescriptionstatic PyException
alignmentNotAllowed
(char align, String forType) Convenience method returning aPy.ValueError
reporting that the given alignment flag is not allowed in a format specifier for the named type.static PyException
alternateFormNotAllowed
(String forType) Convenience method returning aPy.ValueError
reporting that alternate form is not allowed in a format specifier for the named type.static PyException
alternateFormNotAllowed
(String forType, char code) Convenience method returning aPy.ValueError
reporting that alternate form is not allowed in a format specifier for the named type and specified typoe code.append
(char c) append
(CharSequence csq) append
(CharSequence csq, int start, int end) Current (possibly final) result of the formatting, as aString
.boolean
isBytes()
Whether initialised for a byte-like interpretation.static PyException
notAllowed
(String outrage, String forType) Convenience method returning aPy.ValueError
reporting that some format specifier feature is not allowed for the named data type.static PyException
notAllowed
(String outrage, String forType, char code) Convenience method returning aPy.ValueError
reporting that some format specifier feature is not allowed for the named format code and data type.pad()
Pad the result so far (defined as the contents ofresult
frommark
to the end) using the alignment, target width and fill character defined inspec
.static PyException
precisionNotAllowed
(String forType) Convenience method returning aPy.ValueError
reporting that specifying a precision is not allowed in a format specifier for the named type.static PyException
precisionTooLarge
(String type) Convenience method returning aPy.OverflowError
reporting:void
setBytes
(boolean bytes) Signals the client's intention to make a PyString (or other byte-like) interpretation ofresult
, rather than a PyUnicode one.void
setStart()
Clear the instance variables describing the latest object inresult
, ready to receive a new one: setsstart
and callsreset()
.static PyException
signNotAllowed
(String forType, char code) Convenience method returning aPy.ValueError
reporting that specifying a sign is not allowed in a format specifier for the named type.toString()
static PyException
unknownFormat
(char code, String forType) Convenience method returning aPy.ValueError
reporting:static PyException
zeroPaddingNotAllowed
(String forType) Convenience method returning aPy.ValueError
reporting that zero padding is not allowed in a format specifier for the named type.
-
Constructor Details
-
Formatter
Construct the formatter from a client-supplied buffer and a specification. Setsmark
andstart
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.- Parameters:
result
- destination bufferspec
- parsed conversion specification
-
Formatter
Construct the formatter from a specification and initial buffer capacity. Setsmark
to the end of the buffer.- Parameters:
spec
- parsed conversion specificationwidth
- of buffer initially
-
-
Method Details
-
setBytes
public void setBytes(boolean bytes) Signals the client's intention to make a PyString (or other byte-like) interpretation ofresult
, 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();
- Parameters:
bytes
- true to signal the intention to make a byte-like interpretation
-
isBytes
public boolean isBytes()Whether initialised for a byte-like interpretation.- Returns:
- bytes attribute
-
getResult
Current (possibly final) result of the formatting, as aString
.- Returns:
- formatted result
-
getPyResult
Convenience method to return the current result of the formatting, as aPyObject
, eitherPyString
orPyUnicode
according tobytes
.- Returns:
- formatted result
-
append
- Specified by:
append
in interfaceAppendable
-
append
- Specified by:
append
in interfaceAppendable
-
append
public InternalFormat.Formatter append(CharSequence csq, int start, int end) throws IndexOutOfBoundsException - Specified by:
append
in interfaceAppendable
- Throws:
IndexOutOfBoundsException
-
setStart
public void setStart()Clear the instance variables describing the latest object inresult
, ready to receive a new one: setsstart
and callsreset()
. This is necessary when aFormatter
is to be re-used. Note that this leavesmark
where it is. In the core, we need this to supportcomplex
: two floats in the same format, but padded as a unit. -
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. -
pad
Pad the result so far (defined as the contents ofresult
frommark
to the end) using the alignment, target width and fill character defined inspec
. 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
orlenWhole
as follows.Effect of padding on start
orlenWhole
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 (whichcomplex
does).- Returns:
- this Formatter object
-
unknownFormat
Convenience method returning aPy.ValueError
reporting:"Unknown format code '"+code+"' for object of type '"+forType+"'"
- Parameters:
code
- the presentation typeforType
- the type it was found applied to- Returns:
- exception to throw
-
alternateFormNotAllowed
Convenience method returning aPy.ValueError
reporting that alternate form is not allowed in a format specifier for the named type.- Parameters:
forType
- the type it was found applied to- Returns:
- exception to throw
-
alternateFormNotAllowed
Convenience method returning aPy.ValueError
reporting that alternate form is not allowed in a format specifier for the named type and specified typoe code.- Parameters:
forType
- the type it was found applied tocode
- the formatting code (or '\0' not to mention one)- Returns:
- exception to throw
-
alignmentNotAllowed
Convenience method returning aPy.ValueError
reporting that the given alignment flag is not allowed in a format specifier for the named type.- Parameters:
align
- type of alignmentforType
- the type it was found applied to- Returns:
- exception to throw
-
signNotAllowed
Convenience method returning aPy.ValueError
reporting that specifying a sign is not allowed in a format specifier for the named type.- Parameters:
forType
- the type it was found applied tocode
- the formatting code (or '\0' not to mention one)- Returns:
- exception to throw
-
precisionNotAllowed
Convenience method returning aPy.ValueError
reporting that specifying a precision is not allowed in a format specifier for the named type.- Parameters:
forType
- the type it was found applied to- Returns:
- exception to throw
-
zeroPaddingNotAllowed
Convenience method returning aPy.ValueError
reporting that zero padding is not allowed in a format specifier for the named type.- Parameters:
forType
- the type it was found applied to- Returns:
- exception to throw
-
notAllowed
Convenience method returning aPy.ValueError
reporting that some format specifier feature is not allowed for the named data type.- Parameters:
outrage
- committed in the present caseforType
- the data type (e.g. "integer") it where it is an outrage- Returns:
- exception to throw
-
notAllowed
Convenience method returning aPy.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"
- Parameters:
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)- Returns:
- exception to throw
-
precisionTooLarge
Convenience method returning aPy.OverflowError
reporting:"formatted "+type+" is too long (precision too large?)"
- Parameters:
type
- of formatting ("integer", "float")- Returns:
- exception to throw
-