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

public Class MarkupIterator

extends Object
implements CraftedPyObject
Class Inheritance
All Implemented Interfaces
org.python.core.CraftedPyObject
Imports
java.lang.invoke.MethodHandles, org.python.base.MissingFeature, org.python.core.CraftedPyObject, .Py, .PyTuple, .PyType, .ValueError

Provides an implementation of the object that string.formatter_parser() returns, which is an iterator returning successive 4-tuples, the sequence being equivalent to the original string.

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static class
pack-priv static class
MarkupIterator.FieldNumbering

Class used locally to assign indexes to the automatically-numbered arguments (see String Formatting section of the Python Standard Library).

Field Summary

Modifier and TypeField and Description
private final boolean
bytes

True if originally given a bytes or bytearray (so that when a value is formatted using a chunk, it should be to bytes not the code points of a str).

private int
index

How far along that string we are.

private final String
markup

The UTF-16 string from which elements are being returned.

private final MarkupIterator.FieldNumbering
numbering

A counter used to auto-number fields when not explicitly numbered in the format.

public static final PyType
TYPE

The Python type formatteriterator of this class.

Constructor Summary

AccessConstructor and Description
private
MarkupIterator(String
to parse
markup
,
boolean
if originally bytes-like
bytes
,
MarkupIterator.FieldNumbering
for automatically numbered arguments
numbering
)

Constructor used at top-level to enumerate a format.

public
MarkupIterator(String
to parse
markup
)

Constructor used at top-level to enumerate a format.

public
MarkupIterator(String
to parse
markup
,
boolean
if originally bytes-like
bytes
)

Constructor used at top-level to enumerate a format that may be for a bytes-like object.

public
MarkupIterator(MarkupIterator
within which this is nested
enclosingIterator
,
String
the substring this is to parse
subMarkup
)

Variant constructor used when formats are nested.

Method Summary

Modifier and TypeMethod and Description
pack-priv final Object
pack-priv final Object

Returns:

PyTuple chunk or null
__next__
()

Return the next "chunk" of the format (or return null if ended).

public PyType
getType()

Implements org.python.core.CraftedPyObject.getType.

The Python type of this object.
private int
indexOfFirst(String s, int start, char c1, char c2)

Find the first of two characters, or return -1.

public final boolean

Returns:

true if originally given a PyString
isBytes
()

If originally given a PyString, string elements in the returned tuples must be PyString not PyUnicode.

public MarkupIterator.Chunk

Returns:

the chunk
nextChunk
()

Return the next Chunk from the iterator, which is a structure containing parsed elements of the replacement field (if any), and its preceding text.

private void
parseField(MarkupIterator.Chunk
destination chunk
result
,
String
specifying a replacement field, possibly with nesting
fieldMarkup
)

Parse a "replacement field" consisting of a name, conversion and format specification.

private String
private Object

Returns:

object for tuple
wrap
(String
to wrap as a PyObject or null if defaultValue should be wrapped.
value
,
String
to return or null if default return is None.
defaultValue
)

Convenience method for populating the return tuple, returning a String or Py.None if both arguments are null.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

bytesback to summary
private final boolean bytes

True if originally given a bytes or bytearray (so that when a value is formatted using a chunk, it should be to bytes not the code points of a str).

indexback to summary
private int index

How far along that string we are.

markupback to summary
private final String markup

The UTF-16 string from which elements are being returned.

numberingback to summary
private final MarkupIterator.FieldNumbering numbering

A counter used to auto-number fields when not explicitly numbered in the format.

TYPEback to summary
public static final PyType TYPE

The Python type formatteriterator of this class.

Constructor Detail

MarkupIteratorback to summary
private MarkupIterator(String markup, boolean bytes, MarkupIterator.FieldNumbering numbering)

Constructor used at top-level to enumerate a format.

Parameters
markup:String

to parse

bytes:boolean

if originally bytes-like

numbering:MarkupIterator.FieldNumbering

for automatically numbered arguments

MarkupIteratorback to summary
public MarkupIterator(String markup)

Constructor used at top-level to enumerate a format.

Parameters
markup:String

to parse

MarkupIteratorback to summary
public MarkupIterator(String markup, boolean bytes)

Constructor used at top-level to enumerate a format that may be for a bytes-like object.

Parameters
markup:String

to parse

bytes:boolean

if originally bytes-like

MarkupIteratorback to summary
public MarkupIterator(MarkupIterator enclosingIterator, String subMarkup)

Variant constructor used when formats are nested.

Parameters
enclosingIterator:MarkupIterator

within which this is nested

subMarkup:String

the substring this is to parse

Method Detail

__iter__back to summary
pack-priv final Object __iter__()
__next__back to summary
pack-priv final Object __next__()

Return the next "chunk" of the format (or return null if ended). A chunk is a 4-tuple describing

  1. the text leading up to the next format field,
  2. the field name or number (as a string) for accessing the value,
  3. the format specifier such as "#12x", and
  4. any conversion that should be applied (the 's' or 'r' codes for str() and repr())
Elements 1-3 are None if this chunk contains no format specifier. Elements 0-2 are zero-length strings if missing from the format, while element 3 will be None if missing.
Returns:Object

PyTuple chunk or null

getTypeback to summary
public PyType getType()

Implements org.python.core.CraftedPyObject.getType.

Doc from org.python.core.CraftedPyObject.getType.

The Python type of this object.

Returns:PyType

type of this object

Annotations
@Override

indexOfFirstback to summary
private int indexOfFirst(String s, int start, char c1, char c2)

Find the first of two characters, or return -1.

isBytesback to summary
public final boolean isBytes()

If originally given a PyString, string elements in the returned tuples must be PyString not PyUnicode.

Returns:boolean

true if originally given a PyString

nextChunkback to summary
public MarkupIterator.Chunk nextChunk()

Return the next Chunk from the iterator, which is a structure containing parsed elements of the replacement field (if any), and its preceding text. This is the Java equivalent of the tuple returned by __next__(). This finds use in the implementation of str.format and unicode.format.

Returns:MarkupIterator.Chunk

the chunk

parseFieldback to summary
private void parseField(MarkupIterator.Chunk result, String fieldMarkup)

Parse a "replacement field" consisting of a name, conversion and format specification. According to the Python Standard Library documentation, a replacement field has the structure:

replacement_field ::=  "{" [field_name] ["!" conversion] [":" format_spec] "}"
field_name        ::=  arg_name ("." attribute_name | "[" element_index "]")*
arg_name          ::=  [identifier | integer]
attribute_name    ::=  identifier
element_index     ::=  integer | index_string
except at this point, we have already discarded the outer braces.
Parameters
result:MarkupIterator.Chunk

destination chunk

fieldMarkup:String

specifying a replacement field, possibly with nesting

unescapeBracesback to summary
private String unescapeBraces(String substring)
wrapback to summary
private Object wrap(String value, String defaultValue)

Convenience method for populating the return tuple, returning a String or Py.None if both arguments are null.

Parameters
value:String

to wrap as a PyObject or null if defaultValue should be wrapped.

defaultValue:String

to return or null if default return is None.

Returns:Object

object for tuple

org.python.core.stringlib back to summary

public final Class MarkupIterator.Chunk

extends Object
Class Inheritance

Field Summary

Modifier and TypeField and Description
public String
conversion

Conversion to be applied, e.g. 'r' for repr().

public String
fieldName

The field name or number (as a string) for accessing the value.

public String
formatSpec

The format specifier such as "#12x".

public boolean
formatSpecNeedsExpanding

Signals the formatSpec needs expanding recursively.

public String
literalText

The text leading up to the next format field.

Constructor Summary

AccessConstructor and Description
public

Method Summary

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

conversionback to summary
public String conversion

Conversion to be applied, e.g. 'r' for repr().

fieldNameback to summary
public String fieldName

The field name or number (as a string) for accessing the value.

formatSpecback to summary
public String formatSpec

The format specifier such as "#12x".

formatSpecNeedsExpandingback to summary
public boolean formatSpecNeedsExpanding

Signals the formatSpec needs expanding recursively.

literalTextback to summary
public String literalText

The text leading up to the next format field.

Constructor Detail

Chunkback to summary
public Chunk()
org.python.core.stringlib back to summary

pack-priv final Class MarkupIterator.FieldNumbering

extends Object
Class Inheritance

Class used locally to assign indexes to the automatically-numbered arguments (see String Formatting section of the Python Standard Library).

Field Summary

Modifier and TypeField and Description
private int
private boolean

Constructor Summary

AccessConstructor and Description
pack-priv

Method Summary

Modifier and TypeMethod and Description
pack-priv String

Returns:

index as string
nextAutomaticFieldNumber
()

Generate a numeric argument index automatically, or raise an error if already started numbering manually.

pack-priv void
useManualFieldNumbering()

Remember we are numbering manually, and raise an error if already started numbering automatically.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

automaticFieldNumberback to summary
private int automaticFieldNumber
manualFieldNumberSpecifiedback to summary
private boolean manualFieldNumberSpecified

Constructor Detail

FieldNumberingback to summary
pack-priv FieldNumbering()

Method Detail

nextAutomaticFieldNumberback to summary
pack-priv String nextAutomaticFieldNumber()

Generate a numeric argument index automatically, or raise an error if already started numbering manually.

Returns:String

index as string

useManualFieldNumberingback to summary
pack-priv void useManualFieldNumbering()

Remember we are numbering manually, and raise an error if already started numbering automatically.