Skip to content

CharacterSet

CharacterSet is a set of byte values, stored as a 256-bit bitmap (32 bytes). Membership is O(1). String’s set-based methods (trimmed, splitOnSet, byteIndexOfSet) take one, so “any whitespace” or “any digit” is a single value rather than a hand-written test.

#import "CharacterSet.xc" // or the Foundation umbrella

The set holds byte values 0–255, one bit each, so it works over the bytes of a UTF-8 string, not its code points. This suits the lexical classes it is built for (whitespace, digits, ASCII letters); it is not a Unicode character-property set. Instances are heap objects (-falloc=heap), inherit from Object, and are built either from the predefined class methods or by adding bytes to a new set.

CharacterSet* ws = CharacterSet.whitespaceAndNewlines();
String* trimmed = line.trimmed(ws);
CharacterSet* delims = CharacterSet.withCString((u8*)",;\t");
Array* fields = line.splitOnSet(delims);

Creating · init · withCString · withRange

Predefined sets · whitespace · newlines · whitespaceAndNewlines · decimalDigits · hexDigits · letters · alphanumerics · identifiers

Membership · contains · isEmpty

Building · add · remove · addRange · addCString

Set operations · inverted · formUnion · formIntersection


void init(void)

Initialises an empty set. Use new CharacterSet() and then add bytes, or use one of the predefined class methods below.

static CharacterSet* withCString(u8* s)

A set containing the bytes in the C string s, for example CharacterSet.withCString(",;\t").

static CharacterSet* withRange(u8 lo, u8 hi)

A set of every byte from lo to hi inclusive.

↑ Topics

Each returns a new set for a common lexical class.

static CharacterSet* whitespace(void)

Space and tab.

static CharacterSet* newlines(void)

The line-break characters: LF (10), CR (13), VT (11) and FF (12).

static CharacterSet* whitespaceAndNewlines(void)

Space, tab, CR and LF: the usual set for trimming lines.

static CharacterSet* decimalDigits(void)

09.

static CharacterSet* hexDigits(void)

09, af, AF.

static CharacterSet* letters(void)

ASCII AZ and az.

static CharacterSet* alphanumerics(void)

ASCII letters and digits.

static CharacterSet* identifiers(void)

The bytes valid in an identifier: letters, digits and _.

↑ Topics

bool contains(u8 c)

true if byte c is in the set. O(1).

bool isEmpty(void)

true if the set contains no bytes.

↑ Topics

These methods modify the set in place.

void add(u8 c)

Adds byte c.

void remove(u8 c)

Removes byte c.

void addRange(u8 lo, u8 hi)

Adds every byte from lo to hi inclusive.

void addCString(u8* s)

Adds every byte in the C string s.

↑ Topics

CharacterSet* inverted(void)

A new set of every byte not in this one.

void formUnion(CharacterSet* other)

Adds every byte of other to this set (in place).

void formIntersection(CharacterSet* other)

Keeps only the bytes present in both this set and other (in place).

↑ Topics