runes

package
v0.0.0-...-7272a43 Latest Latest
Warning

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

Go to latest
Published: Nov 12, 2022 License: Apache-2.0 Imports: 3 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewArrayBased

func NewArrayBased(replArray [][]rune, safeMin, safeMax rune, escapeUnsafe func(rune) []rune) (e *arrayBasedEscaper)

NewArrayBased creates a new arrayBasedEscaper instance with the given replacement map and specified safe range. If safeMax < safeMin then no runes are considered safe. This initializer is useful when explicit instances of EscaperMap are used to allow the sharing of large replacement mappings.

If a rune has no mapped replacement then it is checked against the safe range. If it lies outside that, then escapeUnsafe(rune) is called, otherwise no escaping is performed.

func NewArrayBasedFromMap

func NewArrayBasedFromMap(replMap map[rune]string, safeMin, safeMax rune, escapeUnsafe func(rune) []rune) *arrayBasedEscaper

NewArrayBasedFromMap creates a new arrayBasedEscaper instance with the given replacement map and specified safe range. If safeMax < safeMin then no runes are considered safe.

If a rune has no mapped replacement then it is checked against the safe range. If it lies outside that, then escapeUnsafe(rune) is called, otherwise no escaping is performed.

Types

type Builder

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

Builder creates and initializes simple, fast escapers.

Typically an escaper needs to deal with the escaping of high valued bytes or code points. In these cases it is necessary to extend either bytes.arrayBasedEscaper or runes.arrayBasedEscaper to provide the desired behavior. However this builder is suitable for creating escapers that replace a relative small set of runes.

func NewBuilder

func NewBuilder() *Builder

NewBuilder returns a builder for creating simple, fast escapers. A builder instance can be reused and each escaper that is created will be a snapshot of the current builder state. Builders are not thread safe.

The initial state of the builder is such that:

• There are no replacement mappings

• safeMin == 0

• safeMax == unicode.MaxRune

• unsafeRepl == nil

For performance reasons escapers created by this builder will not validate the well-formedness of their input.

func (*Builder) AddEscape

func (b *Builder) AddEscape(r rune, replacement string) *Builder

AddEscape adds a replacement string for the given input rune. The specified rune will be replaced by the given string whenever it occurs in the input, irrespective of whether it lies inside or outside the 'safe' range.

func (*Builder) AddEscapes

func (b *Builder) AddEscapes(rs []rune, r string) *Builder

AddEscapes adds multiple mappings at once for a particular index.

func (*Builder) Build

func (b *Builder) Build() *arrayBasedEscaper

Build returns a new escaper based on the current state of the builder.

func (*Builder) SetSafeRange

func (b *Builder) SetSafeRange(safeMin, safeMax rune) *Builder

SetSafeRange sets the safe range of runes for the escaper. Runes in this range that have no explicit replacement are considered 'safe' and remain unescaped in the output. If safeMax < safeMin then the safe range is empty.

func (*Builder) SetUnsafeReplacement

func (b *Builder) SetUnsafeReplacement(unsafeReplacement string) *Builder

SetUnsafeReplacement sets the replacement string for any runes outside the 'safe' range that have no explicit replacement. If unsafeRepl is nil then no replacement will occur, if it is "" then the unsafe runes are removed from the output.

type Escaper

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

Escaper converts literal text into a format safe for inclusion in a particular context (such as an XML document). Typically (but not always), the inverse process of "unescaping" the text is performed automatically by the relevant parser.

For example, an XML escaper would convert the literal string "Foo<Bar>" into "Foo&lt;Bar&gt;" to prevent "<Bar>" from being confused with an XML tag. When the resulting XML document is parsed, the parser API will return this text as the original literal string "Foo<Bar>".

An escaper instance is required to be stateless, and safe when used concurrently by multiple go routines.

Popular escapers are defined as constants:

• html.Escaper

• xml.Escaper

func NewEscaper

func NewEscaper(escapeFunc func(rune) []rune) *Escaper

NewEscaper creates a new Escaper instance with the given escape function.

func (*Escaper) Escape

func (e *Escaper) Escape(str string) string

Escape returns the escaped form of a given literal string.

func (*Escaper) EscapeSlow

func (e *Escaper) EscapeSlow(str string, start int) string

EscapeSlow returns the escaped form of a given literal string, starting at the given index. This method is called by the Escape(string) method when it discovers that escaping is required.

type EscaperMap

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

EscaperMap is an implementation-specific parameter type suitable for initializing arrayBasedEscaper instances. This struct should be used when more than one escaper is created using the same replacement mapping to allow the underlying (implementation specific) data structures to be shared.

The size of the data structure used by arrayBasedEscaper is proportional to the highest valued rune that has a replacement.

func NewEscaperMap

func NewEscaperMap(replMap map[rune]string) *EscaperMap

NewEscaperMap returns a new EscaperMap for creating arrayBasedEscaper instances.

func (*EscaperMap) GetReplacements

func (em *EscaperMap) GetReplacements() [][]rune

GetReplacements returns the non-nil array of replacements for fast lookup.

Jump to

Keyboard shortcuts

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