Skip to content

foxy_xml_source ​

FoXy sources encodings: detection (byte order mark, XML 1.0 Appendix F, encoding declaration) and conversion to UTF-8.

FoXy parses UTF-8. A source in UTF-16, UTF-32, ISO-8859-1, windows-1252 or US-ASCII is converted to UTF-8 piece by piece (StringiFor to_utf8) before the tokenizer: positions refer to the converted text. The encoding is detected from the first bytes (XML 1.0 4.3.3 and Appendix F): a byte order mark, the patterns of <? in UTF-16/UTF-32, then the encoding of the XML declaration. An invalid sequence becomes the byte FF (never in UTF-8), so the tokenizer reports it at its place, in source order, as any other problem.

Source: src/lib/foxy_xml_source.f90

Contents ​

Variables ​

NameTypeAttributesDescription
DETECT_LENinteger(kind=I8P)parameterBytes read to detect the encoding.
MAX_PIECEinteger(kind=I8P)parameterLargest piece converted at once (StringiFor positions).
INVALID_BYTEcharacter(len=*)parameterReplacement of an invalid sequence (never in UTF-8).
BLANKScharacter(len=*)parameterXML blanks.

Derived Types ​

source_encoding ​

Encoding of a source, as detected, and the conversion state.

Components ​

NameTypeAttributesDescription
namecharacter(len=:)allocatableCanonical name, endianness included (UTF-8 if not converted).
problemcharacter(len=:)allocatableProblem of the encoding (reported at position 1), empty if none.
carrycharacter(len=:)allocatableIncomplete character between pieces.
skipinteger(kind=I8P)Bytes of a UTF-16/UTF-32 byte order mark, not converted.
is_convertedlogicalThe source is converted to UTF-8.

Subroutines ​

detect_encoding ​

Detect the encoding of a source from its first bytes (DETECT_LEN at most, all of them if the source is shorter).

@note A declared encoding that is not supported (or that contradicts the byte order mark or the first bytes) is a problem: the source is then read in the detected encoding (UTF-8 if none), as tolerant parsing does.

Attributes: pure

fortran
subroutine detect_encoding(prefix, encoding)

Arguments

NameTypeIntentAttributesDescription
prefixcharacter(len=*)inFirst bytes of the source.
encodingtype(source_encoding)outDetected encoding.

Call graph

detect_file_encoding ​

Detect the encoding of an open stream file from its first bytes (read at position 1).

fortran
subroutine detect_file_encoding(unit, file_size, encoding, iostat, iomsg)

Arguments

NameTypeIntentAttributesDescription
unitinteger(kind=I4P)inFile unit.
file_sizeinteger(kind=I8P)inFile size.
encodingtype(source_encoding)outDetected encoding.
iostatinteger(kind=I4P)outIO status.
iomsgcharacter(len=*)inoutIO message.

Call graph

decode_piece ​

Convert a piece of a source to UTF-8, carrying an incomplete character to the next piece.

Attributes: pure

fortran
subroutine decode_piece(encoding, raw, is_last, utf8)

Arguments

NameTypeIntentAttributesDescription
encodingtype(source_encoding)inoutEncoding (and conversion state).
rawcharacter(len=*)inPiece of the source (at most MAX_PIECE bytes).
is_lastlogicalinThe source ends with this piece.
utf8character(len=:)outallocatablePiece converted.

Call graph

declared_encoding_range ​

Return the range of the encoding value of the XML declaration that starts text (after leading characters that are not markup, as a UTF-8 byte order mark), first 0 if there is none.

Attributes: pure

fortran
subroutine declared_encoding_range(text, first, last)

Arguments

NameTypeIntentAttributesDescription
textcharacter(len=*)inText (UTF-8).
firstinteger(kind=I8P)outFirst character of the value.
lastinteger(kind=I8P)outLast character of the value.

Call graph

Functions ​

decoded ​

Return a whole source (a string) converted to UTF-8, its byte order mark (encoding%skip) removed.

Attributes: pure

Returns: character(len=:)

fortran
function decoded(source, encoding) result(utf8)

Arguments

NameTypeIntentAttributesDescription
sourcecharacter(len=*)inSource.
encodingtype(source_encoding)inDetected encoding.

Call graph

converted_line ​

Return the line of a position of the converted text of an open stream file (read again and converted, in chunks).

Returns: integer(kind=I8P)

fortran
function converted_line(unit, pos, chunk, encoding) result(line)

Arguments

NameTypeIntentAttributesDescription
unitinteger(kind=I4P)inFile unit.
posinteger(kind=I8P)inPosition in the converted text.
chunkinteger(kind=I8P)inSize of the chunks read.
encodingtype(source_encoding)inDetected encoding.

Call graph

with_utf8_declaration ​

Return a prolog whose XML declaration (if it has one, with an encoding) declares UTF-8: the text of a converted source is UTF-8.

Attributes: pure

Returns: character(len=:)

fortran
function with_utf8_declaration(prolog) result(rewritten)

Arguments

NameTypeIntentAttributesDescription
prologcharacter(len=*)inProlog.

Call graph

declared_encoding ​

Return the encoding of the XML declaration that starts text, empty if there is none.

Attributes: pure

Returns: character(len=:)

fortran
function declared_encoding(text) result(declared)

Arguments

NameTypeIntentAttributesDescription
textcharacter(len=*)inText (UTF-8).

Call graph

is_same_family ​

Return true if a declared encoding agrees with the one detected from the byte order mark or the first bytes.

Attributes: pure

Returns: logical

fortran
function is_same_family(declared, detected) result(is_same)

Arguments

NameTypeIntentAttributesDescription
declaredcharacter(len=*)inDeclared encoding, canonical name.
detectedcharacter(len=*)inDetected encoding.

Call graph