segmentation/grapheme_line_break.mbt1///|2/// Return the default UAX #14 break kind after the last grapheme returned by3/// next() or iter(). Before the first grapheme, including for empty input,4/// returns Prohibited. Exhaustion preserves the last kind (Mandatory for5/// nonempty text). Repeated queries do not advance the grapheme cursor.6///7/// Only complete grapheme ends are queried; default line break opportunities8/// inside a cluster are not exposed. This does not change UAX #29 segmentation9/// or recompute line breaking classes per cluster.10///11/// The first query after a grapheme prepares line breaking context for the12/// whole input in O(n) time and space. Subsequent queries scan that context13/// forward, taking O(n) total time over a complete traversal. Traversal without14/// this query does not initialize or run the line breaking algorithm.15///16/// ```mbt check17/// test {18/// let graphemes = @segmentation.graphemes("ab c")19/// let kinds = []20/// for grapheme in graphemes {21/// kinds.push((grapheme.to_owned(), graphemes.line_break()))22/// }23/// debug_inspect(24/// kinds,25/// content=(26/// #|[27/// #| ("a", Prohibited),28/// #| ("b", Prohibited),29/// #| (" ", Allowed),30/// #| ("c", Mandatory),31/// #|]32/// ),33/// )34/// }35/// ```36pub fn Graphemes::line_break(self : Graphemes) -> BreakOpportunity {37 if self.offset == 0 {38 return Prohibited39 }40 let breaks = if self.breaks is Some(breaks) {41 breaks42 } else {43 let breaks = line_breaks(self.text)44 self.breaks = Some(breaks)45 breaks46 }47 // The cursors share the same input, whose final boundary is Mandatory.48 // Consequently this scan always reaches or passes the current cluster end.49 while breaks.offset < self.offset {50 ignore(breaks.next())51 }52 if breaks.offset == self.offset {53 breaks.opportunity()54 } else {55 Prohibited56 }57}58
///|
/// Return the default UAX #14 break kind after the last grapheme returned by
/// next() or iter(). Before the first grapheme, including for empty input,
/// returns Prohibited. Exhaustion preserves the last kind (Mandatory for
/// nonempty text). Repeated queries do not advance the grapheme cursor.
///
/// Only complete grapheme ends are queried; default line break opportunities
/// inside a cluster are not exposed. This does not change UAX #29 segmentation
/// or recompute line breaking classes per cluster.
///
/// The first query after a grapheme prepares line breaking context for the
/// whole input in O(n) time and space. Subsequent queries scan that context
/// forward, taking O(n) total time over a complete traversal. Traversal without
/// this query does not initialize or run the line breaking algorithm.
///
/// ```mbt check
/// test {
/// let graphemes = @segmentation.graphemes("ab c")
/// let kinds = []
/// for grapheme in graphemes {
/// kinds.push((grapheme.to_owned(), graphemes.line_break()))
/// }
/// debug_inspect(
/// kinds,
/// content=(
/// #|[
/// #| ("a", Prohibited),
/// #| ("b", Prohibited),
/// #| (" ", Allowed),
/// #| ("c", Mandatory),
/// #|]
/// ),
/// )
/// }
/// ```
pub fn Graphemes::line_break(self : Graphemes) -> BreakOpportunity {
if self.offset == 0 {
return Prohibited
}
let breaks = if self.breaks is Some(breaks) {
breaks
} else {
let breaks = line_breaks(self.text)
self.breaks = Some(breaks)
breaks
}
// The cursors share the same input, whose final boundary is Mandatory.
// Consequently this scan always reaches or passes the current cluster end.
while breaks.offset < self.offset {
ignore(breaks.next())
}
if breaks.offset == self.offset {
breaks.opportunity()
} else {
Prohibited
}
}