CharacterSet

struct CharacterSet

A reusable character-membership policy for trimming and classification.

Explicit sets compare complete grapheme clusters from characters; predefined sets dispatch to Unicode properties without materializing their members. This is a policy object, not a mutable collection.

Methods

static func whitespace() -> CharacterSet

Matches clusters whose first scalar has Unicode White_Space.

View Source
pub static func whitespace() -> CharacterSet {
	CharacterSet(kind: 1, members: "")
}

static func newlines() -> CharacterSet

Matches LF, CR, NEL, LINE SEPARATOR, and PARAGRAPH SEPARATOR clusters.

View Source
pub static func newlines() -> CharacterSet {
	CharacterSet(kind: 2, members: "")
}

static func alphanumerics() -> CharacterSet

Matches clusters whose first scalar is Unicode alphabetic or numeric.

View Source
pub static func alphanumerics() -> CharacterSet {
	CharacterSet(kind: 3, members: "")
}

static func punctuation() -> CharacterSet

Matches clusters whose first scalar belongs to a Unicode punctuation category.

View Source
pub static func punctuation() -> CharacterSet {
	CharacterSet(kind: 4, members: "")
}

func contains(_ character: Character) -> Bool

Applies the predefined Unicode policy or searches explicit members.

Explicit sets compare complete grapheme clusters byte-for-byte. Predefined sets classify the candidate's first scalar.

View Source
pub func contains(_ character: Character) -> Bool {
	if self.kind == 1 { return character.is_whitespace() }
	if self.kind == 2 {
		let scalar = character.first_scalar()
		return scalar == 10 || scalar == 13 || scalar == 133 || scalar == 8232 || scalar == 8233
	}
	if self.kind == 3 { return character.is_alphanumeric() }
	if self.kind == 4 { return character.is_punctuation() }
	self.members.contains(character: character)
}