textnorm

package
v1.10.0 Latest Latest
Warning

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

Go to latest
Published: Nov 4, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package textnorm folds text into one canonical form for pattern matching and maps every folded byte back to the original text, so a detector can match the folded form and still redact the exact original bytes.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsHidden

func IsHidden(r rune) bool

IsHidden reports whether r is an invisible rune that Fold removes: zero-width and joiner characters (U+200B to U+200F, U+2060 to U+2064, U+FEFF, U+180E, U+034F), the soft hyphen U+00AD, bidi controls (U+202A to U+202E, U+2066 to U+2069), tag characters (U+E0000 to U+E007F) and the supplementary variation selectors (U+E0100 to U+E01EF).

Types

type Text

type Text struct {
	// Original is the text as given.
	Original string
	// Folded is the canonical form that patterns run on.
	Folded string
	// contains filtered or unexported fields
}

Text is a string and its folded form. Folded is NFKC normalised and lower case, has invisible runes removed (see IsHidden), has Cyrillic and Greek lookalikes inside Latin words replaced by their Latin letters, and has every whitespace run collapsed to one space. Build it with Fold.

func Fold

func Fold(s string) Text

Fold builds the folded form of s with its offset map.

func (Text) Hidden

func (t Text) Hidden() int

Hidden returns the number of removed runes that hid something: every invisible rune Fold removed except a leading byte order mark, joiners in emoji sequences and non-Latin words, subdivision flag tags, and the first non-joiner, LRM and RLM of a run (and an immediate repeat of one) that do not sit between two Latin letters.

func (Text) HiddenAt

func (t Text) HiddenAt() []int

HiddenAt returns the original byte offset of every rune Hidden counts, in order.

func (Text) Lines

func (t Text) Lines(start, end int) (lineStart, lineEnd int)

Lines widens the original byte range [start, end) to the whole lines it touches: from just after the preceding '\n' to just before the next one. A range that ends with a '\n' does not pull in the following line. It binary-searches a newline index, so many calls on one long line stay cheap.

func (Text) Span

func (t Text) Span(start, end int) (origStart, origEnd int)

Span maps the folded byte range [start, end) to the original byte range it came from. Offsets are clamped to Folded. A Text not built by Fold maps any range to the whole original.

type Visible

type Visible struct {
	// Original is the text as given.
	Original string
	// Text is Original without its invisible runes.
	Text string
	// contains filtered or unexported fields
}

Visible is a string with its invisible runes (see IsHidden) removed and a map back to the original. Unlike Text it keeps case, width and whitespace, for detectors whose formats depend on them, such as secret tokens. Build it with Strip.

func Strip

func Strip(s string) Visible

Strip removes the invisible runes of s, so a zero-width space cannot split a token a detector looks for.

func (Visible) Span

func (v Visible) Span(start, end int) (origStart, origEnd int)

Span maps the byte range [start, end) of Text to the original byte range, which includes any invisible runes inside it. Offsets are clamped to Text.

Jump to

Keyboard shortcuts

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