Documentation
¶
Overview ¶
Package generic builds blocks from XML with no knowledge of its schema (README, "How XML maps without a profile"), and holds the part writer every consumer of element events shares.
An element that directly contains character data other than white space is a text element: one inferred paragraph with its own text and that of its descendants in document order. An element that holds only elements is a container and has no block of its own. A text element inside another is part of the outer one's block.
Whether an element is a text element is known only when its direct text comes, which may follow child elements whose own blocks are already made. Those blocks wait in a queue while any ancestor is undecided, and are taken into the ancestor's block if it turns out to be a text element. The queue is bounded (MaxPendingBytes): past it, every undecided element still open is taken for a container and the queue is written out; such an element that has text after that point gives a block from there on.
Index ¶
- Constants
- func BlocksHint(size int64) int
- func Clean(s string, preserve bool, b *Builder) string
- func CleanName(s string, b *Builder) string
- func CutString(s string, n int) string
- func Skipped(e *xmlread.Element, a *xmlread.Attr) bool
- type Builder
- func (b *Builder) Append(t []byte, preserve bool)
- func (b *Builder) Break()
- func (b *Builder) Lead() bool
- func (b *Builder) Len() int
- func (b *Builder) LineBreak()
- func (b *Builder) Pos() int
- func (b *Builder) Reset()
- func (b *Builder) Size() int
- func (b *Builder) Space()
- func (b *Builder) Split(limit int) string
- func (b *Builder) SplitAt(limit int) (string, int)
- func (b *Builder) SplitWord(pos int) (piece string, took int, kept bool)
- func (b *Builder) String() string
- func (b *Builder) Trail() bool
- type Generic
- func (g *Generic) Comment(r *xmlread.Reader, t []byte, final bool, start, end int64)
- func (g *Generic) Counts() (controls, invalid int)
- func (g *Generic) EndElement(r *xmlread.Reader, e *xmlread.Element, start, end int64)
- func (g *Generic) Finish(end int64)
- func (g *Generic) StartElement(r *xmlread.Reader, e *xmlread.Element)
- func (g *Generic) Stop() bool
- func (g *Generic) Text(r *xmlread.Reader, t []byte, start, end int64)
- type Options
- type Out
- type WordMark
Constants ¶
const ( // MaxBlockBytes bounds the text of one block. A longer text is split // into blocks marked Continued between the scanner's pieces (which // end after white space where there is some); white space at a cut // is dropped, as at a block's ends. MaxBlockBytes = 1 << 20 // MaxNameBytes bounds an element or attribute name as it appears in // Attrs. MaxNameBytes = 256 // MaxPathBytes bounds Attrs["element"], the path of names from the // root: MaxNameBytes times 16. MaxPathBytes = MaxNameBytes * 16 // MaxValueBytes bounds the text of an attribute's note and an anchor. MaxValueBytes = 1 << 20 // MaxPendingBytes and MaxPendingBlocks bound the blocks that wait for // an undecided ancestor (see the package comment). MaxPendingBytes = 4 << 20 MaxPendingBlocks = 8192 // MaxLangBytes bounds the root's xml:lang taken for the document's // language; a longer one is an attribute note. MaxLangBytes = 64 )
Fixed bounds. What they cut is reported with limit.exceeded.
const ( // AttrElement is the path of element names from the root, as written // (prefixes included), joined with "/". AttrElement = "element" // AttrAttribute is the name of the attribute an attribute note holds, // as written. AttrAttribute = "attribute" )
Keys of Block.Attrs this library writes.
const ( BlockHintBytes = 128 MaxBlocksHint = 4096 )
BlockHintBytes and MaxBlocksHint size BlocksHint. Service-Public files make a block for every 126 to 419 bytes of input with the profile (1st to 95th percentile), every 43 to 89 bytes generically.
Variables ¶
This section is empty.
Functions ¶
func BlocksHint ¶
BlocksHint is the room for blocks given to a document of size bytes before its first block: one block per BlockHintBytes of input, at most MaxBlocksHint.
func Clean ¶
Clean returns s with the text conventions applied, white space collapsed and trimmed, and the counts of what was removed. When nothing changes, s itself is returned.
func CleanName ¶
CleanName returns s, a name or identifier, valid UTF-8 and with no control characters; nothing else is changed. A byte that is not part of a character is U+FFFD and counted: the scanner does not check names. A U+FFFD already in s is kept and not counted.
Types ¶
type Builder ¶
type Builder struct {
// Controls counts control characters removed, Invalid the bytes of
// names that were not UTF-8 (in CleanName, and in markup kept as
// text). Text and attribute values come from the scanner as valid
// UTF-8, with its own count.
Controls, Invalid int
// contains filtered or unexported fields
}
Builder makes the text of one block from pieces, applying the text conventions as it goes: XML white space collapsed to one space (or kept as written under xml:space="preserve"), white space trimmed at the ends, control characters other than tab and line feed removed, bytes that are not UTF-8 written as U+FFFD, ligatures written as their letters.
func (*Builder) Break ¶
func (b *Builder) Break()
Break appends a hard line break: the white space held before it becomes one "\n". Like other white space it is dropped at the ends.
func (*Builder) Lead ¶
Lead and Trail report that the text appended began or ended with white space, which trimming removed.
func (*Builder) LineBreak ¶
func (b *Builder) LineBreak()
LineBreak appends a line break written in the markup (HTML's <br>): white space held before it is dropped and a "\n" is added, so that two breaks give two. Like other white space it is dropped at the ends, and white space after it is not added.
func (*Builder) Pos ¶
Pos returns the offset in the text where the next character appended will be, white space held before it included.
func (*Builder) Reset ¶
func (b *Builder) Reset()
Reset empties the builder, keeping its counts and buffers.
func (*Builder) Space ¶
func (b *Builder) Space()
Space appends a boundary of white space, as one collapsed space.
func (*Builder) Split ¶
Split takes the first piece of a text longer than limit, cut at white space where there is some in the last part of the piece, else at a character boundary, and keeps the rest.
func (*Builder) SplitAt ¶
SplitAt is Split that also returns how many bytes of the text the piece took, white space at the cut included: the offset in the old text of the rest's first byte.
func (*Builder) SplitWord ¶
SplitWord takes the text up to its last white space as a piece and keeps the word after it, which what comes next continues: a text cut where a token starts is not cut inside a word. pos is where the caller knows that word to start in the text (its source offset is known); a text whose last white space is not just before pos, one that ends in white space, or one that holds none is taken whole (kept is false). took is as in SplitAt.
type Generic ¶
type Generic struct {
// contains filtered or unexported fields
}
Generic is the generic consumer. It implements xmlread.Handler.
func (*Generic) Counts ¶
Counts returns the control characters removed and invalid bytes met in all the text made.
func (*Generic) EndElement ¶
EndElement implements xmlread.Handler.
func (*Generic) Finish ¶
Finish ends the reading at offset end: what is still open is closed and everything is written out.
func (*Generic) StartElement ¶
StartElement implements xmlread.Handler.
type Options ¶
type Options struct {
// NoAttributes leaves attribute notes out.
NoAttributes bool
}
Options are the generic reader's settings.
type Out ¶
type Out struct {
Part *gofastcore.Part
Budget *gofastcore.TextBudget
// Clean collects the counts of removed control characters and
// invalid bytes.
Clean Builder
// Stopped is set when the text budget is spent; StoppedAt is the
// offset reading had reached.
Stopped bool
StoppedAt int64
// Counts of cuts by the fixed bounds.
NamesCut, PathsCut, ValuesCut int
// Splits counts texts split into Continued blocks.
Splits int
// PendingCut counts cuts by MaxPendingBlocks and MaxPendingBytes that
// show in the output: a block given as a piece because more than
// MaxPendingBlocks notes waited for it, and an element taken for a
// container because the queue passed its bound, which then had text
// of its own.
PendingCut int
// Hint is the room for blocks the part is given when its first block
// is added (see BlocksHint); zero lets the part grow from nothing.
Hint int
// UTF8: the file is UTF-8, its bytes the text's (see WordMark).
UTF8 bool
}
Out writes blocks to a part within the text budget, and counts what the text conventions and the fixed bounds did.
func (*Out) Add ¶
Add appends b, taking BlockCost, its text and its element path from the budget. It reports false, and adds nothing, once the budget is spent.
type WordMark ¶
WordMark records where the last word of a text being built starts: in the file (At) and in the text (Pos), when both are known (OK). A long text cut while reading is cut there, so that no word is cut in two and each piece's span holds its text.