///|
/// ECMAScript WhiteSpace and LineTerminator, as matched by StringStream.eatSpace.
pub fn is_space(c : UInt16) -> Bool {
  match c {
    0x0009..<=0x000D
    | 0x0020
    | 0x00A0
    | 0x1680
    | 0x2000..<=0x200A
    | 0x2028
    | 0x2029
    | 0x202F
    | 0x205F
    | 0x3000
    | 0xFEFF => true
    _ => false
  }
}

///|
pub fn whitespace_end(text : String, start : Int32) -> Int32 {
  guard start < text.length() else { return text.length() }
  let first = text[start]
  guard is_space(first) else { return start }
  let units = text[:]
  let mut pos = start + 1
  // Indentation and trailing padding often repeat one code unit. Check four
  // at a time, then classify the mixed suffix and any remaining units.
  while units.length() - pos >= 4 &&
        units.unsafe_get(pos) == first &&
        units.unsafe_get(pos + 1) == first &&
        units.unsafe_get(pos + 2) == first &&
        units.unsafe_get(pos + 3) == first {
    pos += 4
  }
  for i = pos; i < units.length(); i = i + 1 {
    guard is_space(units.unsafe_get(i)) else { break i }
  } nobreak {
    text.length()
  }
}