Something went wrong. Try again.
The Unicode Collation Algorithm (UTS #10) and CLDR locale tailorings in Zig: sort text the way people expect rather than by code point.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142// SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie <jeff@ocjtech.us>// SPDX-License-Identifier: MIT
//! zig-collate: the Unicode Collation Algorithm, [UTS #10][uca].//!//! Text compared by code point is not merely unlocalized, it is wrong in a//! way people notice. `Mueller` sorts before `Märtens`, because `u` is//! U+0075 and `ä` is U+00E4. This library exists so that a program which//! needs to put names in order does not have to reimplement a collation to//! get that right, and so that it does not get it wrong in the same way//! every such reimplementation does.//!//! The short version://!//! ```zig//! const collate = @import("collate");//!//! const c: collate.Collator = .{};//! const order = try c.order(gpa, "Märtens", "Mueller"); // .lt//! ```//!//! Sorting a list means building one sort key per string and then comparing//! the keys, which is what makes the whole thing affordable -- *n* key//! constructions and `n log n` byte comparisons, rather than `n log n`//! collations://!//! ```zig//! var builder: collate.Builder = .empty;//! defer builder.deinit(gpa);//!//! const key = try builder.sortKeyAlloc(gpa, c, name);//! // ... and later//! if (collate.orderKeys(key_a, key_b) == .lt) { ... }//! ```//!//! ## What is here and what is not//!//! The root collation, which is the untailored Default Unicode Collation//! Element Table: all four steps of section 7, the variable weighting//! options of section 4, strengths from primary to identical, and the//! derived weights of section 10.1 for everything the table does not//! mention. The CLDR root collation and CLDR's per-locale tailorings, for//! the locales a build asks for with `-Dlocales`. And section 11's//! searching: `Searcher` finds a pattern by what a collation cannot tell//! apart, exactly or within a number of edits, and `Collator.matchKeyAlloc`//! writes match keys, for finding text that is stored and indexed rather than//! at hand.//!//! Not here: section 11.1's readable folded strings, whole-word match//! boundaries, or searching backwards.//!//! [uca]: https://www.unicode.org/reports/tr10/
const collator = @import("collator.zig");const key = @import("key.zig");const table = @import("table.zig");const search = @import("search.zig");
pub const Collator = collator.Collator;pub const Builder = collator.Builder;pub const Error = collator.Error;pub const orderKeys = collator.orderKeys;
pub const Options = key.Options;pub const Strength = key.Strength;pub const Alternate = key.Alternate;pub const CaseFirst = key.CaseFirst;/// The settings a locale's rules ask for that are a collator's business/// rather than a table's. `Options.applying` is how they are taken up.pub const Settings = @import("data").Settings;
/// UTS #10 section 11's searching: find a pattern in a target by what a/// collation cannot tell apart, rather than by code point.////// ```zig/// var s = try collate.Searcher.init(gpa, .{ .options = .{ .strength = .primary } }, "resume", .{});/// defer s.deinit();/// const m = try s.find("Your Résumé", 0); // .{ .start = 5, .end = 13 }/// ```pub const Searcher = search.Searcher;pub const SearchOptions = search.SearchOptions;pub const Match = search.Match;/// The types `SearchOptions` is made of.pub const search_types = struct { pub const Span = search.Span; pub const Boundary = search.Boundary;};
pub const Table = table.Table;/// The Default Unicode Collation Element Table, generated at build time from/// the Unicode data files in `data/`. Empty when the library was built with/// `-Dembed-ducet=false`.pub const ducet = table.ducet;
/// The CLDR root collation, which is a tailoring of DUCET applied before/// anyone's language is considered: scripts regrouped, the set of variable/// characters narrowed, and the sentinels that sort key merging needs added./// It is what ICU orders by.////// Empty unless the library was built with `-Dembed-cldr`.pub const cldr_root = table.cldr_root;
pub const Tailoring = @import("data").Tailoring;
/// The CLDR collation tailorings this build was asked for, by `-Dlocales`.////// ```zig/// const swedish: collate.Collator = .{/// .table = collate.ducet.tailored(&collate.locales.sv),/// };/// ```////// A locale the build was not asked for is a compile error rather than a/// run-time surprise, which is the point of making these declarations rather/// than a lookup by string.pub const locales = @import("locales");
/// Collation elements and the weights they are made of: the vocabulary of/// section 3.1, and the shapes a table is stored in.pub const data = @import("data");
/// Step 1 on its own, for a caller that wants NFD without a collation.pub const nfd = @import("nfd.zig");
/// Steps 2 and 3, for a caller that wants to see the collation elements or/// build a key by hand.pub const elements = @import("elements.zig");pub const sort_key = key;
test { // Every declaration rather than every file: the doctests live inside // the types they document, and a type's tests are only compiled when // something refers to the type. const std = @import("std"); std.testing.refAllDecls(@import("collator.zig")); std.testing.refAllDecls(@import("elements.zig")); std.testing.refAllDecls(@import("key.zig")); std.testing.refAllDecls(@import("nfd.zig")); std.testing.refAllDecls(@import("search.zig")); std.testing.refAllDecls(@import("table.zig"));}