Documentation
¶
Overview ¶
Package collate compares strings using compiled Go locale data (OpenEnv), or the bounded host ISO-8859-1 provider (Open). Compiled providers are pure Go on every platform and carry explicit order and multi-level weights. The host provider uses purego (Apache-2.0) on Linux; see THIRD_PARTY_LICENSES.md.
Index ¶
- Variables
- type Engine
- type Provider
- func (p *Provider) Close() error
- func (p *Provider) CollatingElements() ([]bool, error)
- func (p *Provider) CollationWeights() ([]byte, error)
- func (p *Provider) Compare(a, b string) (int, error)
- func (p *Provider) EquivalenceClasses() ([]bool, error)
- func (p *Provider) Equivalents(c byte) ([]byte, error)
Constants ¶
This section is empty.
Variables ¶
var ( // ErrUnsupportedPlatform is returned by Open when no glibc provider is // built: outside linux/amd64 and linux/arm64, or under bashy_scratch. ErrUnsupportedPlatform = errors.New("collate: glibc collation provider is only built for linux/amd64 and linux/arm64") // ErrUnsupportedLocale is returned when the requested locale name is not one // of the two accepted ISO-8859-1 aliases. Reported BEFORE any libc is loaded. ErrUnsupportedLocale = errors.New("collate: unsupported locale; only the ISO-8859-1 aliases \"de_DE.ISO-8859-1\" and \"de_DE.iso88591\" are accepted") // loaded C library is not glibc (honest detection via gnu_get_libc_version), // or a required symbol is missing. ErrGlibcUnavailable = errors.New("collate: glibc runtime not detected") // ErrMissingLocale is returned when glibc is present but the requested locale // data is not installed (newlocale failed). ErrMissingLocale = errors.New("collate: locale data is not installed") // ErrInitFailure is returned when glibc newlocale fails due to ENOMEM or other initialization failure. ErrInitFailure = errors.New("collate: initialization failure") // ErrCodeset is returned when the opened locale's CODESET is not ISO-8859-1, // which would mean the byte-oriented Compare contract does not hold. ErrCodeset = errors.New("collate: locale codeset is not ISO-8859-1") // ErrNulInput is returned by Compare when either operand contains a NUL byte, // which a C string cannot represent unambiguously. ErrNulInput = errors.New("collate: input contains a NUL byte") // ErrClosed is returned by Compare after the provider has been closed. ErrClosed = errors.New("collate: provider is closed") ErrInvalidCollatingElement = errors.New("collate: byte is not a valid single-byte collating element") )
Sentinel errors. These are platform-independent so callers can switch on them identically on Linux and on the stub platforms.
Functions ¶
This section is empty.
Types ¶
type Engine ¶
type Engine interface {
Compare(string, string) (int, error)
Close() error
Equivalents(byte) ([]byte, error)
EquivalenceClasses() ([]bool, error)
CollationWeights() ([]byte, error)
CollatingElements() ([]bool, error)
}
Engine is the shared compiled/host collation contract. Byte-table consumers receive an explicit error for orders requiring multibyte or multi-character matching, rather than a lossy table.
type Provider ¶
type Provider struct {
// contains filtered or unexported fields
}
Provider compares strings in a glibc ISO-8859-1 locale via strcoll_l.
It holds two independent locale_t handles — one carrying LC_COLLATE (used by strcoll_l) and one carrying LC_CTYPE (used to read and verify CODESET). A RWMutex fences concurrent Compare calls against Close; Close is idempotent.
func Open ¶
Open returns a Provider for one of the accepted ISO-8859-1 locale aliases.
The locale name is validated before any libc is touched; an unaccepted name returns ErrUnsupportedLocale. It then requires glibc (ErrGlibcUnavailable if absent), the locale data to be installed (ErrMissingLocale), and the locale's CODESET to be exactly ISO-8859-1 (ErrCodeset). The caller owns the returned Provider and must Close it.
func (*Provider) Close ¶
Close frees the provider's locale_t handles. It is safe to call more than once and safe against concurrent Compare calls: the write lock waits for in-flight comparisons to finish, and the closed flag stops later ones deterministically.
func (*Provider) CollatingElements ¶
CollatingElements reports which individual bytes glibc accepts as named collating elements. NUL is always false because POSIX regex strings cannot represent it.
func (*Provider) CollationWeights ¶
CollationWeights returns glibc's single-byte LC_COLLATE sequence.
func (*Provider) Compare ¶
Compare returns -1, 0, or +1 as a sorts before, equal to, or after b under the provider's LC_COLLATE order. The operands must be ISO-8859-1 (single-byte) encoded, matching the locale's codeset. A NUL byte in either operand returns ErrNulInput; a closed provider returns ErrClosed.