Skip to content

Char

extension typeChar(int)

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(intcodeUnit)

Char.checked() factory

factoryChar.checked(intcodeUnit)

Creates a Char after validating that codeUnit is in the range [MinValue, MaxValue], throwing a RangeError otherwise.

Implementation
dart
factory Char.checked(int codeUnit) {
  RangeError.checkValueInInterval(codeUnit, MinValue.codeUnit, MaxValue.codeUnit, 'codeUnit');
  return Char(codeUnit);
}

Char.fromString() factory

factoryChar.fromString(Stringstr)

Creates a Char from the first code unit of str.

Asserts that str contains exactly one code unit.

Implementation
dart
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

StringgetasString

Returns a one-character String containing this code unit.

Implementation
dart
String get asString => String.fromCharCode(codeUnit);

codeUnit final

finalintcodeUnit

isAscii no setter

boolgetisAscii

Whether this character falls in the ASCII range (code unit < 128).

Implementation
dart
bool get isAscii => codeUnit < 128;

isDigit no setter

boolgetisDigit

Whether this character is an ASCII decimal digit (09).

Implementation
dart
bool get isDigit => 0x30 <= codeUnit && codeUnit <= 0x39;

isLetter no setter

boolgetisLetter

Whether this character is a letter (ASCII, Latin Extended, Greek, or CJK).

Implementation
dart
bool get isLetter => _category(codeUnit) <= 4;

isLowerCase no setter

boolgetisLowerCase

Whether this character is an ASCII lower-case letter (az).

Implementation
dart
bool get isLowerCase => 0x61 <= codeUnit && codeUnit <= 0x7a;

isUpperCase no setter

boolgetisUpperCase

Whether this character is an ASCII upper-case letter (AZ).

Implementation
dart
bool get isUpperCase => 0x41 <= codeUnit && codeUnit <= 0x5a;

isWhitespace no setter

boolgetisWhitespace

Whether this character is ASCII whitespace (space, tab, LF, or CR).

Implementation
dart
bool get isWhitespace =>
    codeUnit == 0x20 || codeUnit == 0x09 || codeUnit == 0x0a || codeUnit == 0x0d;

toLowerCase no setter

ChargettoLowerCase

Returns the lower-case equivalent of this character if it is an ASCII upper-case letter; otherwise returns this unchanged.

Implementation
dart
Char get toLowerCase => isUpperCase ? Char(codeUnit + 32) : this;

toUpperCase no setter

ChargettoUpperCase

Returns the upper-case equivalent of this character if it is an ASCII lower-case letter; otherwise returns this unchanged.

Implementation
dart
Char get toUpperCase => isLowerCase ? Char(codeUnit - 32) : this;

Operators

operator +()

Charoperator +(intoffset)

Returns a new Char whose code unit is (this.codeUnit + offset) & 0xffff.

Implementation
dart
Char operator +(int offset) => Char((codeUnit + offset) & 0xffff);

operator -()

Charoperator -(intoffset)

Returns a new Char whose code unit is (this.codeUnit - offset) & 0xffff.

Implementation
dart
Char operator -(int offset) => Char((codeUnit - offset) & 0xffff);

operator <()

booloperator <(Charother)

Whether this character's code unit is strictly less than other's.

Implementation
dart
bool operator <(Char other) => codeUnit < other.codeUnit;

operator <=()

booloperator <=(Charother)

Whether this character's code unit is less than or equal to other's.

Implementation
dart
bool operator <=(Char other) => codeUnit <= other.codeUnit;

operator >()

booloperator >(Charother)

Whether this character's code unit is strictly greater than other's.

Implementation
dart
bool operator >(Char other) => codeUnit > other.codeUnit;

operator >=()

booloperator >=(Charother)

Whether this character's code unit is greater than or equal to other's.

Implementation
dart
bool operator >=(Char other) => codeUnit >= other.codeUnit;

Static Properties

MaxValue read / write

CharMaxValue

getter:

The largest possible Char value (U+FFFF).

setter:

The largest possible Char value (U+FFFF).

Implementation
dart
static Char MaxValue = Char(0xffff);

MinValue read / write

CharMinValue

getter:

The smallest possible Char value (U+0000).

setter:

The smallest possible Char value (U+0000).

Implementation
dart
static Char MinValue = Char(0x0000);