Parse/validate ISBNs in Zig
  • Zig 99.6%
  • Nix 0.4%
Find a file
Repository files (latest commit first)
Filename Latest commit message Latest commit date
2026-08-28 22:53:06 -05:00
.tangled/workflows Add spindle workflow running reuse lint and tests 2026-08-28 22:50:34 -05:00
LICENSES initial commit 2026-05-29 23:45:42 -05:00
src Add hyphenation using the ISBN Agency range data 2026-08-28 21:53:28 -05:00
tools Add hyphenation using the ISBN Agency range data 2026-08-28 21:53:28 -05:00
.gitignore initial commit 2026-05-29 23:45:42 -05:00
build.zig Add hyphenation using the ISBN Agency range data 2026-08-28 21:53:28 -05:00
build.zig.zon Add README 2026-08-28 21:05:49 -05:00
flake.lock update flake 2026-08-28 20:54:54 -05:00
flake.nix update flake 2026-08-28 20:54:54 -05:00
README.md Add hyphenation using the ISBN Agency range data 2026-08-28 21:53:28 -05:00
REUSE.toml initial commit 2026-05-29 23:45:42 -05:00
typos.toml initial commit 2026-05-29 23:45:42 -05:00

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 X check 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.