Package org.python.core.stringlib
Class InternalFormat.Spec
java.lang.Object
org.python.core.stringlib.InternalFormat.Spec
- Enclosing class:
- InternalFormat
Parsed PEP-3101 format specification of a single field, encapsulating the format for use by
formatting methods. This class holds the several attributes that might be decoded from a
format specifier. Each attribute has a reserved value used to indicate "unspecified".
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 InternalFormat.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();
-
Field Summary
Modifier and TypeFieldDescriptionfinal char
Alignment indicator is one of {'<', '^', '>', '=', or U+FFFF if unspecified.final boolean
The alternative format flag '#' was given.final char
The fill character specified, or U+FFFF if unspecified.final boolean
Insert the grouping separator (which in Python always indicates a group-size of 3).static final char
Non-character code point used to represent "no value" inchar
attributes.static final InternalFormat.Spec
Defaults applicable to most numeric types.final int
Precision decoded from the format, or -1 if unspecified.final char
Sign-handling flag, one of'+'
,'-'
, or' '
, or U+FFFF if unspecified.static final InternalFormat.Spec
Defaults applicable to string types.final char
Type key from the format, or U+FFFF if unspecified.static final int
Negative value used to represent "no value" inint
attributes.final int
Width to which to pad the result, or -1 if unspecified. -
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionchar
getAlign
(char defaultAlign) The alignment from the parsed format specification, or default.char
getFill
(char defaultFill) The alignment from the parsed format specification, or default.int
getPrecision
(int defaultPrecision) The precision from the parsed format specification, or default.char
getType
(char defaultType) The type code from the parsed format specification, or default supplied.static final boolean
specified
(char c) Test to see if an attribute has been specified.static final boolean
specified
(int value) Test to see if an attribute has been specified.toString()
Return a format specifier (text) equivalent to the value of this Spec.withDefaults
(InternalFormat.Spec other) Return a mergedSpec
object, in which any attribute of this object that is specified (ortrue
), has the same value in the result, and any attribute of this object that is unspecified (orfalse
), has the value that attribute takes in the other object.
-
Field Details
-
fill
public final char fillThe fill character specified, or U+FFFF if unspecified. -
align
public final char alignAlignment indicator is one of {'<', '^', '>', '=', or U+FFFF if unspecified. -
sign
public final char signSign-handling flag, one of'+'
,'-'
, or' '
, or U+FFFF if unspecified. -
alternate
public final boolean alternateThe alternative format flag '#' was given. -
width
public final int widthWidth to which to pad the result, or -1 if unspecified. -
grouping
public final boolean groupingInsert the grouping separator (which in Python always indicates a group-size of 3). -
precision
public final int precisionPrecision decoded from the format, or -1 if unspecified. -
type
public final char typeType key from the format, or U+FFFF if unspecified. -
NONE
public static final char NONENon-character code point used to represent "no value" inchar
attributes.- See Also:
-
UNSPECIFIED
public static final int UNSPECIFIEDNegative value used to represent "no value" inint
attributes.- See Also:
-
NUMERIC
Defaults applicable to most numeric types. Equivalent to " >" -
STRING
Defaults applicable to string types. Equivalent to " <"
-
-
Constructor Details
-
Spec
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]
- Parameters:
fill
- fill character (orNONE
align
- alignment indicator, one of {'<', '^', '>', '='}sign
- policy, one of'+'
,'-'
, or' '
.alternate
- true to request alternate formatting mode ('#'
flag).width
- of field after padding or -1 to defaultgrouping
- true to request comma-separated groupsprecision
- (e.g. decimal places) or -1 to defaulttype
- indicator character
-
Spec
public Spec(int precision, char type) Constructor offering just precision and type.[.precision][type]
- Parameters:
precision
- (e.g. decimal places)type
- indicator character
-
-
Method Details
-
specified
public static final boolean specified(char c) Test to see if an attribute has been specified.- Parameters:
c
- attribute- Returns:
- true only if the attribute is not equal to
NONE
-
specified
public static final boolean specified(int value) Test to see if an attribute has been specified.- Parameters:
value
- of attribute- Returns:
- true only if the attribute is ≥0 (meaning that it has been specified).
-
toString
Return a format specifier (text) equivalent to the value of this Spec. -
withDefaults
Return a mergedSpec
object, in which any attribute of this object that is specified (ortrue
), has the same value in the result, and any attribute of this object that is unspecified (orfalse
), has the value that attribute takes in the other object. Thus the second object supplies default values. (These defaults may also be unspecified.) The use of this method is to allow aSpec
constructed from text to record exactly, and only, what was in the textual specification, while the __format__ method of a client object supplies its type-specific defaults. Thus "20" means "<20s" to astr
, ">20.12" to afloat
and ">20.12g" to acomplex
.- Parameters:
other
- defaults to merge where this object does not specify the attribute.- Returns:
- a new Spec object.
-
getFill
public char getFill(char defaultFill) The alignment from the parsed format specification, or default. -
getAlign
public char getAlign(char defaultAlign) The alignment from the parsed format specification, or default. -
getPrecision
public int getPrecision(int defaultPrecision) The precision from the parsed format specification, or default. -
getType
public char getType(char defaultType) The type code from the parsed format specification, or default supplied.
-