Documentation
¶
Overview ¶
Package sanitize removes or replaces problematic Unicode characters from strings and provides strict parsing helpers for numeric input.
It is a Go port of the sanitizing routines in Flamework's lib_sanitize.php. Sanitizing a string involves, in order:
- rejecting input that is not valid UTF-8,
- removing "evil" code points (most control characters, byte order marks, bidirectional controls, zero width spaces, Unicode "tag" characters),
- removing either all "other" (\p{C}) characters or only invalid non-characters, depending on Options.StripReserved,
- normalizing tabs, line breaks, form feeds and replacement characters.
Basic usage:
opts := sanitize.DefaultOptions() clean, err := sanitize.SanitizeString(userInput, opts)
Useful references:
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func SanitizeFloat64 ¶
SanitizeFloat64 parses input as a 64-bit floating point number. It returns the strconv error (syntax or range) if parsing fails, and an error for NaN and infinite values, which strconv otherwise accepts.
func SanitizeInt32 ¶
SanitizeInt32 parses input as a base-10 integer that fits in an int32. It returns the strconv error (syntax or range) if parsing fails.
func SanitizeInt64 ¶
SanitizeInt64 parses input as a base-10 integer that fits in an int64. It returns the strconv error (syntax or range) if parsing fails.
func SanitizeString ¶
SanitizeString returns input with unsafe or invisible Unicode characters removed or replaced according to options.
It returns an error, and an empty string, if input is not valid UTF-8. If options is nil, DefaultOptions is used.
Types ¶
type Options ¶
type Options struct {
// StripReserved removes every character in the Unicode "Other" category
// (\p{C}: controls, format, private use, unassigned, surrogates). When
// false, only invalid non-characters are removed.
StripReserved bool
// AllowNewlines preserves line breaks as "\n" (and form feeds / paragraph
// separators as "\n\n") instead of replacing them.
AllowNewlines bool
// contains filtered or unexported fields
}
Options controls how SanitizeString behaves.
The replacement fields are unexported; obtain a configured value from DefaultOptions or DebugOptions and then adjust the exported fields.
func DebugOptions ¶
func DebugOptions() *Options
DebugOptions returns Options that replace every sanitized character with the visible marker " { SANITIZED } ", making it easy to see what was altered.
func DefaultOptions ¶
func DefaultOptions() *Options
DefaultOptions returns Options that delete unsafe characters and turn tabs and line breaks into single spaces. Unknown/replacement characters become "?".