fix(els): pos_to_index (renamed to pos_to_byte_index)

Fixed a crash when handling non-ascii characters
This commit is contained in:
Shunsuke Shibayama 2023-02-17 17:54:49 +09:00
parent 66ece61af2
commit 0c579fa6fb
2 changed files with 7 additions and 10 deletions

View file

@ -38,24 +38,21 @@ pub fn pos_in_loc<L: Locational>(loc: &L, pos: Position) -> bool {
}
}
pub fn pos_to_index(src: &str, pos: Position) -> usize {
let mut index = 0;
pub fn pos_to_byte_index(src: &str, pos: Position) -> usize {
let mut line = 0;
let mut col = 0;
for c in src.chars() {
for (index, c) in src.char_indices() {
if line == pos.line && col == pos.character {
return index;
}
if c == '\n' {
line += 1;
col = 0;
index += 1;
} else {
col += 1;
index += 1;
}
}
index
unreachable!()
}
pub fn get_token_stream(uri: Url) -> ELSResult<TokenStream> {