generic

package
v0.5.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 28, 2026 License: MIT Imports: 9 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

View Source
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.

View Source
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.

View Source
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

func BlocksHint(size int64) int

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

func Clean(s string, preserve bool, b *Builder) string

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

func CleanName(s string, b *Builder) string

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.

func CutString

func CutString(s string, n int) string

CutString cuts s to at most n bytes at a character boundary.

func Skipped

func Skipped(e *xmlread.Element, a *xmlread.Attr) bool

Skipped reports whether an attribute is not a note: a namespace declaration, xml:space, and on the root xml:lang and the schema location hints, which go to the metadata.

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) Append

func (b *Builder) Append(t []byte, preserve bool)

Append appends t. Under preserve, XML white space is kept as written.

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

func (b *Builder) Lead() bool

Lead and Trail report that the text appended began or ended with white space, which trimming removed.

func (*Builder) Len

func (b *Builder) Len() int

Len returns the bytes of text held, not counting white space at the end.

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

func (b *Builder) Pos() int

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) Size

func (b *Builder) Size() int

Size returns the bytes held, white space at the end included.

func (*Builder) Space

func (b *Builder) Space()

Space appends a boundary of white space, as one collapsed space.

func (*Builder) Split

func (b *Builder) Split(limit int) string

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

func (b *Builder) SplitAt(limit int) (string, int)

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

func (b *Builder) SplitWord(pos int) (piece string, took int, kept bool)

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.

func (*Builder) String

func (b *Builder) String() string

String returns the text, trimmed.

func (*Builder) Trail

func (b *Builder) Trail() bool

type Generic

type Generic struct {
	// contains filtered or unexported fields
}

Generic is the generic consumer. It implements xmlread.Handler.

func New

func New(out *Out, opts Options) *Generic

New returns a generic consumer writing to out.

func (*Generic) Comment

func (g *Generic) Comment(r *xmlread.Reader, t []byte, final bool, start, end int64)

Comment implements xmlread.Handler.

func (*Generic) Counts

func (g *Generic) Counts() (controls, invalid int)

Counts returns the control characters removed and invalid bytes met in all the text made.

func (*Generic) EndElement

func (g *Generic) EndElement(r *xmlread.Reader, e *xmlread.Element, start, end int64)

EndElement implements xmlread.Handler.

func (*Generic) Finish

func (g *Generic) Finish(end int64)

Finish ends the reading at offset end: what is still open is closed and everything is written out.

func (*Generic) StartElement

func (g *Generic) StartElement(r *xmlread.Reader, e *xmlread.Element)

StartElement implements xmlread.Handler.

func (*Generic) Stop

func (g *Generic) Stop() bool

Stop implements xmlread.Handler.

func (*Generic) Text

func (g *Generic) Text(r *xmlread.Reader, t []byte, start, end int64)

Text 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

func (o *Out) Add(b gofastcore.Block) bool

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.

func (*Out) Append

func (o *Out) Append(b gofastcore.Block)

Append appends b to the part, its cost already taken. The first block of the document gives the part Hint blocks of room.

func (*Out) Reserve

func (o *Out) Reserve(n int)

Reserve makes room for n more blocks in the part at once.

type WordMark

type WordMark struct {
	At  int64
	Pos int
	OK  bool
}

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.

func (*WordMark) Note

func (m *WordMark) Note(b *Builder, t []byte, start, end int64, pos0 int, trail, utf8 bool)

Note updates m for the text t, at [start,end) in the file, just appended to b; pos0 and trail are b.Pos() and b.Trail() before the append. utf8: the file's bytes are the text's, so that a token as long as its span holds no reference and no line end changed.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL