Lately, I have been spending my evenings in Common Lisp on two very different fronts: for my long-time hobby of building an Emacs clone in Common Lisp, I was hacking on a regular expression engine, and the other day I was porting more of GNU coreutils and working on my wordcount program. While working on those, I noticed that Common Lisp lacks some character classes I am used to from both C and Emacs Lisp. If you check for example Emacs Lisp manual for char classes, there is nothing similar in Common Lisp.
I don’t know how necessary they are, but I did need this kind of predicate to implement an Elisp regex engine in Common Lisp, as well as the printf tool from coreutils. Of course, these are trivial to add so I didn’t really think much of them, but when I got back to my clone of GNU wc, and wanted to implement support for Unicode white spaces and new lines, I did realize this is actually not a trivial problem.
It is trivial as long as we are in ASCII land, but once we enter UTF-8 and Unicode, it becomes rather complicated, at least for me. Personally, I have always been as uninterested in Unicode and encodings as one could be, so I never really learned how complicated it is. Well, until now :).
ANSI Common Lisp gives us a solid foundation in Section 13.2: cl:alpha-char-p, cl:alphanumericp, cl:digit-char-p, cl:graphic-char-p, cl:lower-case-p, cl:standard-char-p, and cl:upper-case-p. For ordinary Lisp code it is perhaps enough, and the few missing ones are not hard to add ourselves when we are dealing just with ASCII. But step outside Lisp into POSIX bracket expressions or text utilities and the gaps show up. There is no [:blank:], no [:cntrl:], no [:punct:], no [:word:], and no dedicated [:xdigit:] (though (digit-char-p c 16) comes close). Even the closest matches have subtle traps, because Common Lisp uses a POSIX-like name, graphic-char-p, but with the POSIX [:print:] meaning: cl:graphic-char-p returns true for #\Space, which makes it POSIX [:print:] rather than POSIX [:graph:]. Outside standard characters the exact behavior of cl:alpha-char-p and friends is left up to the implementation.
Pressed mostly by the need for my wordcount program, and partly just by curiosity (or OCD), I thought I should implement something (re)usable for all of my projects. I didn’t want to shadow or replace the standard predicates, and I wanted my extra predicates to feel “natural” alongside those existing ones, sort of to fill the “missing gap”, if I am allowed to say so.
The result is character-classes (also nicknamed posix-char-classes), a small, zero-dependency Common Lisp library that implements POSIX.1 character classes alongside some of the extensions I actually needed in real code: [:ascii:], [:odigit:], [:word:], and newline-char-p. None of its symbols shadow anything in COMMON-LISP, so we can drop (:use #:cl #:character-classes) into a defpackage without conflicts.
Repertoires in bags
Thirteen of the sixteen predicates take an optional second argument specifying the character repertoire — the character set and classification rules to test against. Borrowing Common Lisp’s designator idiom, this repertoire designator defaults to *default-char-repertoire* (:unicode) and can be either a keyword or a sequence of characters (a character bag, as in cl:string-trim). Recognized keywords are:
:unicodefor full Unicode Character Database semantics aligned with UTS #18 Annex C:asciifor strict 7-bit ASCII:base-charfor Unicode rules applied only tobase-charcharacters.
If you need something custom, say treating only spaces and tabs as delimiters, or matching a custom set of identifier characters, pass any sequence of characters (character bag) directly and it checks membership with cl:char=.
(whitespace-char-p (code-char #x00A0)) ; => T (:unicode by default)
(whitespace-char-p (code-char #x00A0) :ascii) ; => NIL
(let ((*default-char-repertoire* :ascii))
(alphabetic-char-p (code-char #x00E9))) ; => NIL
(visible-char-p #\Space) ; => NIL (POSIX [:graph:])
(printing-char-p #\Space) ; => T (POSIX [:print:])
The remaining three predicates, ascii-char-p, octal-digit-char-p, and hex-digit-char-p, take a single character. octal-digit-char-p and hex-digit-char-p follow cl:digit-char-p and return numeric weights (0 through 7 and 0 through 15) rather than plain T, so you can use them directly in escape parsers and integer readers while still passing #'octal-digit-char-p straight to remove-if-not or find-if.
Fast paths around small tables
In order not to depend on a large library such as cl-unicode (about 13 MB of source and data, plus cl-ppcre and flexi-streams), character-classes precomputes the Unicode property data into compact lookup tables (LUTs). An offline generator (character-classes/generator) walks code points from 0 to #x10FFFF through sb-unicode (currently Unicode 18.0.0), and emits a standalone, portable src/generated-tables.lisp file. The library has thus zero third party dependencies, and is plain portable Common Lisp (it loads and works on SBCL and CCL; the full test suite has so far been run only on SBCL).
Some predicates, like sparse classes—blank, newline, whitespace, and control—don’t need lookup tables at all; the generator emits tiny inline range comparisons. Denser classes like alpha, punct, word, and visible are sliced into 256-bit pages, deduplicated, trimmed of trailing empty pages, and indexed with (unsigned-byte 8) arrays. Across all seven table-backed Unicode 18.0.0 properties combined, the entire dataset comes out to just over 30 kilobytes. That is portable, and far lighter than depending on cl-unicode, though cl-unicode is the better choice if you need its other features.
On the hot path, every repertoire-aware predicate checks for 7-bit ASCII first. ASCII characters never touch a lookup table: they resolve through a single range test, a (<= 97 (logior code 32) 122) case-folding trick, or a pair of 64-bit bitmasks. On SBCL, all sixteen predicates are declared sb-ext:maybe-inline, and non-character errors are pushed off into a separate non-returning helper function so inlined loops over typed strings elide the type check altogether and stay compact. Whenever the tables are regenerated, the generator immediately loads, compiles, and brute-force checks every single code point from 0 to #x10FFFF against the source bit-vectors.
The docs CLHS-style
Since the whole point of pulling these predicates was to complement already existing ones, I thought it would be nice to have a dpANS3R / CLHS-style LaTeX specification as documentation. I don’t know if others would like it, but I am actually quite happy with it, probably the most fun part in it all for me.
MIT License
The code is MIT-licensed, with the Unicode License v3 notice included alongside it for the derived Unicode 18.0.0 tables.