collate

package
v0.0.0-...-a04f83e Latest Latest
Warning

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

Go to latest
Published: Dec 4, 2026 License: MIT Imports: 11 Imported by: 1 module, 0 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 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

Constants

This section is empty.

Variables

View Source
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")

	// ErrGlibcUnavailable is returned when libc.so.6 cannot be loaded, or the
	// 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.

func OpenEnv

func OpenEnv(env []string, name string) (Engine, error)

OpenEnv gives compiled locales precedence and never hides malformed stores. env must carry invocation-resolved store paths (locale.StoreEnvAt).

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

func Open(name string) (*Provider, error)

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

func (p *Provider) Close() error

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

func (p *Provider) CollatingElements() ([]bool, error)

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

func (p *Provider) CollationWeights() ([]byte, error)

CollationWeights returns glibc's single-byte LC_COLLATE sequence.

func (*Provider) Compare

func (p *Provider) Compare(a, b string) (int, error)

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.

func (*Provider) EquivalenceClasses

func (p *Provider) EquivalenceClasses() ([]bool, error)

func (*Provider) Equivalents

func (p *Provider) Equivalents(c byte) ([]byte, error)

Equivalents returns the complete single-byte equivalence class for c.

Jump to

Keyboard shortcuts

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