clean

package
v0.1.21 Latest Latest
Warning

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

Go to latest
Published: Nov 28, 2026 License: MIT Imports: 6 Imported by: 5 modules, 3 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 clean turns bytes read from a file into strings that obey the text conventions of the shared document model: valid UTF-8, no control characters other than line feed and tab, and for prose no surrounding white space and no presentation-form ligatures.

Content is kept where it can be: what is changed is how it is written. Line and page separators become line feeds, ligatures become their letters. The only thing removed is control characters that have no meaning as text, and they are counted so the caller can report it.

Index

Constants

View Source
const MaxAddress = 8 << 10

MaxAddress bounds an address. Addresses run longer than names: one with a query or a token in it is often over a thousand bytes, and one cut short leads nowhere. This is more than servers accept.

View Source
const MaxLabel = 4 << 10

MaxLabel bounds a label. Names and messages are short, but a title used as a name is a line of text, and in a script of three bytes a character 512 bytes held only 170: 4 KiB holds any real one.

Variables

This section is empty.

Functions

func Address

func Address(s string) string

Address is Label for the target of a link: the same cleaning, with room for the length addresses have. A caller that puts an address into the document counts it against the text the document may hold, since one address may be linked to any number of times.

func AddressCut

func AddressCut(s string) (string, bool)

AddressCut is Address that also reports whether the bound left out anything but white space.

func Append

func Append(dst, b []byte, flags Flags) ([]byte, int)

Append appends b to dst in the form the conventions ask for, and returns the extended slice and the number of control characters it removed. It does not trim: a caller that builds a paragraph out of pieces trims the whole once, and the offsets of the pieces stay true until then.

func Bounded

func Bounded(s string, limit int) (string, bool)

Bounded is Label with a bound of the caller's, for values that are content and so run longer than names. It reports whether the bound left out anything but white space.

func Label

func Label(s string) string

Label makes a string that came from the file fit to appear as a name, an identifier or part of a message in the output: valid UTF-8, no control characters, no line breaks, trimmed, and of bounded length. It is for part names, content types, addresses and error text, which a file controls as much as it controls its content.

func LabelCut

func LabelCut(s string) (string, bool)

LabelCut is Label that also reports whether the bound left out anything but white space.

func Name

func Name(s string) string

Name is Label for a name that is text to be read, such as the name of an author: its ligatures are written as their letters as well.

func NameCut

func NameCut(s string) (string, bool)

NameCut is Name that also reports whether the bound left out anything but white space.

func Plain

func Plain(b []byte) bool

Plain reports whether b is printable ASCII, with line feeds and tabs: text that obeys the conventions as it stands, apart from trimming.

func String

func String(b []byte, flags Flags) (s string, removed int)

String returns b as a string that obeys the conventions selected by flags, and the number of control characters it had to remove.

Types

type Flags

type Flags uint8

Flags select the conventions to apply beyond validity and control characters, which always apply.

const (
	// Trim removes leading and trailing white space.
	Trim Flags = 1 << iota
	// Ligatures expands U+FB00 to U+FB06 to their letters.
	Ligatures

	// Prose is what block text and table cells need.
	Prose = Trim | Ligatures
	// Verbatim is for code: nothing beyond validity and control characters.
	Verbatim Flags = 0
)

Jump to

Keyboard shortcuts

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