module

Bidi::UTF16::TextSource

Text source utilities for UTF-16 (Array(UInt16))

Provides character iteration and access methods for UTF-16 encoded text, with proper handling of surrogate pairs and invalid sequences.

Class methods

char_at(text : Array(UInt16), index : Int32) : Tuple(Char, Int32) | Nil

Get character at index with its length in code units

Source
char_len(char : Char) : Int32

Number of code units a character uses in UTF-16

Source
each_char(text : Array(UInt16), &block : Char -> )

Iterates over characters in a UTF-16 array, handling surrogate pairs

Correctly processes:

  • Single code units (U+0000 to U+D7FF, U+E000 to U+FFFF)
  • Surrogate pairs (high: 0xD800-0xDBFF, low: 0xDC00-0xDFFF)
  • Invalid sequences (replaced with U+FFFD REPLACEMENT CHARACTER)

Parameters:

  • text: UTF-16 encoded text as Array(UInt16)
  • &block: Block to yield each character to
Source
length(text : Array(UInt16)) : Int32

Get length in code units (UInt16 elements)

Source
subrange(text : Array(UInt16), range : Range(Int32, Int32)) : Array(UInt16)

Get subrange of UTF-16 array

Source