Parse/validate ISBNs in Zig
- Zig 99.6%
- Nix 0.4%
| Filename | Latest commit message | Latest commit date |
|---|---|---|
Co-Authored-By: Claude Fable 5 <[email protected]> Claude-Session: https://claude.ai/code/session_016uAPiLrcvxP4hyvSLokRrp |
||
| .tangled/workflows | ||
| LICENSES | ||
| src | ||
| tools | ||
| .gitignore | ||
| build.zig | ||
| build.zig.zon | ||
| flake.lock | ||
| flake.nix | ||
| README.md | ||
| REUSE.toml | ||
| typos.toml | ||
isbn.zig
A Zig library for parsing, validating, and converting International Standard Book Numbers (ISBNs) in both the 10-digit and 13-digit formats.
Features
- Parse ISBNs from text, with or without
-separators. - Validate check digits during parsing (including the
Xcheck digit used by ISBN-10). - Compute check digits, optionally using SIMD vector operations.
- Upgrade an ISBN-10 to its equivalent ISBN-13.
- Compare ISBNs across formats: an ISBN-10 is equal to its 13-digit equivalent.
- Format ISBNs with proper hyphenation, using the International ISBN Agency's registration group and registrant range data.
Requirements
- Zig 0.16.0 or later
Installation
Add the dependency to your project:
zig fetch --save git+https://tangled.org/jcollie.dev/isbn.zig
Then wire it up in your build.zig:
const isbn = b.dependency("isbn", .{
.target = target,
.optimize = optimize,
});
exe.root_module.addImport("isbn", isbn.module("isbn"));
Usage
const ISBN = @import("isbn").ISBN;
// Parsing validates the check digit.
const ten: ISBN = try .parse("0-306-40615-2");
const thirteen: ISBN = try .parse("978-0-306-40615-7");
// Upgrade an ISBN-10 to an ISBN-13.
const upgraded = ten.upgrade();
// Comparison works across formats.
std.debug.assert(ten.eql(&thirteen));
// Hyphenate explicitly, or format with {f}.
var buf: [ISBN.max_hyphenated_len]u8 = undefined;
const hyphenated = try upgraded.hyphenate(&buf);
std.debug.print("{s}\n", .{hyphenated}); // 978-0-306-40615-7
std.debug.print("{f}\n", .{ten}); // 0-306-40615-2
parse returns error.ParseError for malformed input and
error.InvalidCheckDigit when the check digit doesn't match.
Build options
-Dsimd=[bool]— use SIMD vector operations for computing check digits (default:true).
Range data
Hyphenation uses the registration group and registrant ranges published
by the International ISBN Agency,
embedded at build time as src/ranges.zig. To update them:
zig build update-ranges
Testing
zig build test
The test suite includes unit tests and a fuzz test.
License
MIT. This project is compliant with the REUSE Specification.