Char
A single UTF-16 code unit, represented as a zero-cost extension type over int.
Parsers that inspect individual characters receive a Char rather than a raw int, which makes predicate signatures self-documenting and avoids confusion with other integer values.
Constructors
Char()
Char.checked() factory
Creates a Char after validating that codeUnit is in the range [MinValue, MaxValue], throwing a RangeError otherwise.
Implementation
factory Char.checked(int codeUnit) {
RangeError.checkValueInInterval(codeUnit, MinValue.codeUnit, MaxValue.codeUnit, 'codeUnit');
return Char(codeUnit);
}Char.fromString() factory
Creates a Char from the first code unit of str.
Asserts that str contains exactly one code unit.
Implementation
factory Char.fromString(String str) {
assert(str.length == 1, 'Char.fromString requires single code unit string, found: $str');
return Char.checked(str.codeUnitAt(0));
}Properties
asString no setter
Returns a one-character String containing this code unit.
Implementation
String get asString => String.fromCharCode(codeUnit);codeUnit final
isAscii no setter
Whether this character falls in the ASCII range (code unit < 128).
Implementation
bool get isAscii => codeUnit < 128;isDigit no setter
Whether this character is an ASCII decimal digit (0–9).
Implementation
bool get isDigit => 0x30 <= codeUnit && codeUnit <= 0x39;isLetter no setter
Whether this character is a letter (ASCII, Latin Extended, Greek, or CJK).
Implementation
bool get isLetter => _category(codeUnit) <= 4;isLowerCase no setter
Whether this character is an ASCII lower-case letter (a–z).
Implementation
bool get isLowerCase => 0x61 <= codeUnit && codeUnit <= 0x7a;isUpperCase no setter
Whether this character is an ASCII upper-case letter (A–Z).
Implementation
bool get isUpperCase => 0x41 <= codeUnit && codeUnit <= 0x5a;isWhitespace no setter
Whether this character is ASCII whitespace (space, tab, LF, or CR).
Implementation
bool get isWhitespace =>
codeUnit == 0x20 || codeUnit == 0x09 || codeUnit == 0x0a || codeUnit == 0x0d;toLowerCase no setter
Returns the lower-case equivalent of this character if it is an ASCII upper-case letter; otherwise returns this unchanged.
Implementation
Char get toLowerCase => isUpperCase ? Char(codeUnit + 32) : this;toUpperCase no setter
Returns the upper-case equivalent of this character if it is an ASCII lower-case letter; otherwise returns this unchanged.
Implementation
Char get toUpperCase => isLowerCase ? Char(codeUnit - 32) : this;Operators
operator +()
Returns a new Char whose code unit is (this.codeUnit + offset) & 0xffff.
Implementation
Char operator +(int offset) => Char((codeUnit + offset) & 0xffff);operator -()
Returns a new Char whose code unit is (this.codeUnit - offset) & 0xffff.
Implementation
Char operator -(int offset) => Char((codeUnit - offset) & 0xffff);operator <()
Whether this character's code unit is strictly less than other's.
Implementation
bool operator <(Char other) => codeUnit < other.codeUnit;operator <=()
Whether this character's code unit is less than or equal to other's.
Implementation
bool operator <=(Char other) => codeUnit <= other.codeUnit;operator >()
Whether this character's code unit is strictly greater than other's.
Implementation
bool operator >(Char other) => codeUnit > other.codeUnit;operator >=()
Whether this character's code unit is greater than or equal to other's.
Implementation
bool operator >=(Char other) => codeUnit >= other.codeUnit;Static Properties
MaxValue read / write
getter:
The largest possible Char value (U+FFFF).
setter:
The largest possible Char value (U+FFFF).
Implementation
static Char MaxValue = Char(0xffff);MinValue read / write
getter:
The smallest possible Char value (U+0000).
setter:
The smallest possible Char value (U+0000).
Implementation
static Char MinValue = Char(0x0000);