use super::{EndState, Staged, StagedToken, Token, code_point_at, units_to_string};
const MAXIMUM_WORD_LENGTH: usize = 255;
const NEWLINE: u32 = '\n' as u32;
const DEFAULT_TYPE: &str = "word";
pub(super) fn tokenize(units: &[u16]) -> Staged {
let mut tokens = Vec::new();
let mut at = 0usize;
while at < units.len() {
let mut length = 0usize;
while at < units.len() {
let (point, width) = code_point_at(units, at);
if point == NEWLINE {
if length > 0 {
break;
}
at += width;
continue;
}
at += width;
length += width;
if length >= MAXIMUM_WORD_LENGTH {
break;
}
}
if length == 0 {
break;
}
let end = at;
tokens.push(StagedToken {
token: Token {
term: units_to_string(&units[end - length..end]),
position_increment: 1,
start_offset: (end - length) as u32,
end_offset: end as u32,
token_type: DEFAULT_TYPE.to_owned(),
},
stop: EndState {
position_increment: 0,
offset: end as u32,
},
});
}
Staged {
tokens,
end: EndState {
position_increment: 0,
offset: units.len() as u32,
},
}
}