wcwidth.api

The public API of clj-wcwidth.

code-point->string

(code-point->string code-point)

Returns the String representation of any Unicode code-point†, or nil when code-point is nil.

One of the ways this is useful is because Clojure/Java String literals only support escape sequences (i.e. "\uXXXX") for code points in the basic plane; code points in the supplementary planes must be manually converted into their UTF-16 surrogate pair, and then each UTF-16 code unit in the pair escaped separately (which is tedious and error prone).

†a char or int, but int is usually the better choice, because of historical limitations with Java’s char type

code-points->string

(code-points->string code-points)

Returns a String made up of all of the given Unicode code-points†, or nil when code-points is nil.

†a sequence of chars or ints, but ints are usually the better choice, because of historical limitations with Java’s char type

combining?

(combining? code-point)

Is code-point† a combining character?

†a char or int, but int is usually the better choice, because of historical limitations with Java’s char type

display-width

(display-width cs)(display-width cs & {:keys [ignore-ansi?], :or {ignore-ansi? false}})

Returns the number of columns needed to display cs (a CharSequence), but deviates from POSIX wcswidth behaviour in these ways:

  • non-printing characters are considered zero width (instead of causing the entire result to be -1)
  • ANSI escape sequences are (by default, but configurable) also considered zero width

For most use cases, this function is more useful than wcswidth, despite not adhering to POSIX.

Returns 0 when cs is nil.

grapheme-clusters

(grapheme-clusters cs)

Returns the Unicode grapheme clusters (what we tend to think of as “characters”) in cs (a CharSequence) as a sequence of Strings, or nil when cs is nil.

Notes:

non-printing?

(non-printing? code-point)

Is code-point† a non-printing (control) character?

†a char or int, but int is usually the better choice, because of historical limitations with Java’s char type

null?

(null? code-point)

Is code-point† a null character?

†a char or int, but int is usually the better choice, because of historical limitations with Java’s char type

print-code-point

(print-code-point code-point)

Prints code-point in normative Unicode notation (U+codepoint) to stdout, returning nil. This is primarily intended to be a convenience at the REPL.

print-code-points

(print-code-points cs)

Prints the code points in cs (a CharSequence) in normative Unicode notation (U+codepoint) to stdout, returning nil. This is primarily intended to be a convenience at the REPL.

string->code-points

(string->code-points cs)

Returns all of the Unicode code points in cs (a CharSequence), as a sequence of ints, or nil when cs is nil.

wcswidth

(wcswidth cs)

Returns the number of columns needed to represent cs (a CharSequence). If a non-printing (control) code point occurs in cs, -1 is returned (as defined in POSIX).

Returns 0 when cs is nil.

wcwidth

(wcwidth code-point)

Returns the number of columns needed to represent the code-point †, based on these rules:

  • Null character, or nil: 0
  • Printable: 0, 1, or 2
  • Non-printing: -1

†a char or int, but int is usually the better choice, because of historical limitations with Java’s char type

wide?

(wide? code-point)

Is code-point† in the East Asian Wide (W), East Asian Full-width (F), or other wide character (e.g. emoji) category?

Note that many emoji are comprised of multiple code points, and this function cannot detect those.

†a char or int, but int is usually the better choice, because of historical limitations with Java’s char type