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 umbrellaOverview
Section titled “Overview”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);Topics
Section titled “Topics”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
Creating
Section titled “Creating”void init(void)Initialises an empty set. Use new CharacterSet() and then add bytes,
or use one of the predefined class methods below.
withCString
Section titled “withCString”static CharacterSet* withCString(u8* s)A set containing the bytes in the C string s, for example
CharacterSet.withCString(",;\t").
withRange
Section titled “withRange”static CharacterSet* withRange(u8 lo, u8 hi)A set of every byte from lo to hi inclusive.
Predefined sets
Section titled “Predefined sets”Each returns a new set for a common lexical class.
whitespace
Section titled “whitespace”static CharacterSet* whitespace(void)Space and tab.
newlines
Section titled “newlines”static CharacterSet* newlines(void)The line-break characters: LF (10), CR (13), VT (11) and FF (12).
whitespaceAndNewlines
Section titled “whitespaceAndNewlines”static CharacterSet* whitespaceAndNewlines(void)Space, tab, CR and LF: the usual set for trimming lines.
decimalDigits
Section titled “decimalDigits”static CharacterSet* decimalDigits(void)0–9.
hexDigits
Section titled “hexDigits”static CharacterSet* hexDigits(void)0–9, a–f, A–F.
letters
Section titled “letters”static CharacterSet* letters(void)ASCII A–Z and a–z.
alphanumerics
Section titled “alphanumerics”static CharacterSet* alphanumerics(void)ASCII letters and digits.
identifiers
Section titled “identifiers”static CharacterSet* identifiers(void)The bytes valid in an identifier: letters, digits and _.
Membership
Section titled “Membership”contains
Section titled “contains”bool contains(u8 c)true if byte c is in the set. O(1).
isEmpty
Section titled “isEmpty”bool isEmpty(void)true if the set contains no bytes.
Building
Section titled “Building”These methods modify the set in place.
void add(u8 c)Adds byte c.
remove
Section titled “remove”void remove(u8 c)Removes byte c.
addRange
Section titled “addRange”void addRange(u8 lo, u8 hi)Adds every byte from lo to hi inclusive.
addCString
Section titled “addCString”void addCString(u8* s)Adds every byte in the C string s.
Set operations
Section titled “Set operations”inverted
Section titled “inverted”CharacterSet* inverted(void)A new set of every byte not in this one.
formUnion
Section titled “formUnion”void formUnion(CharacterSet* other)Adds every byte of other to this set (in place).
formIntersection
Section titled “formIntersection”void formIntersection(CharacterSet* other)Keeps only the bytes present in both this set and other (in place).