Documentation ¶
Index ¶
- func NewArrayBased(replArray [][]rune, safeMin, safeMax rune, escapeUnsafe func(rune) []rune) (e *arrayBasedEscaper)
- func NewArrayBasedFromMap(replMap map[rune]string, safeMin, safeMax rune, escapeUnsafe func(rune) []rune) *arrayBasedEscaper
- type Builder
- func (b *Builder) AddEscape(r rune, replacement string) *Builder
- func (b *Builder) AddEscapes(rs []rune, r string) *Builder
- func (b *Builder) Build() *arrayBasedEscaper
- func (b *Builder) SetSafeRange(safeMin, safeMax rune) *Builder
- func (b *Builder) SetUnsafeReplacement(unsafeReplacement string) *Builder
- type Escaper
- type EscaperMap
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 ¶
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 ¶
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 ¶
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 ¶
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<Bar>" 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 ¶
NewEscaper creates a new Escaper instance with the given escape function.
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.