Make utf8 default, implement utf16 in terms of it

This commit is contained in:
Aleksey Kladov 2021-02-12 22:09:53 +03:00
parent 00cc778c8c
commit 95209aa3f8
6 changed files with 28 additions and 11 deletions

View file

@ -22,6 +22,14 @@ pub struct LineColUtf16 {
pub col: u32,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct LineCol {
/// Zero-based
pub line: u32,
/// Zero-based utf8 offset
pub col: u32,
}
#[derive(Clone, Debug, Hash, PartialEq, Eq)]
pub(crate) struct Utf16Char {
/// Start offset of a character inside a line, zero-based
@ -88,18 +96,25 @@ impl LineIndex {
LineIndex { newlines, utf16_lines }
}
pub fn line_col(&self, offset: TextSize) -> LineColUtf16 {
pub fn line_col(&self, offset: TextSize) -> LineCol {
let line = partition_point(&self.newlines, |&it| it <= offset) - 1;
let line_start_offset = self.newlines[line];
let col = offset - line_start_offset;
LineColUtf16 { line: line as u32, col: self.utf8_to_utf16_col(line as u32, col) as u32 }
LineCol { line: line as u32, col: col.into() }
}
pub fn offset(&self, line_col: LineColUtf16) -> TextSize {
//FIXME: return Result
pub fn offset(&self, line_col: LineCol) -> TextSize {
self.newlines[line_col.line as usize] + TextSize::from(line_col.col)
}
pub fn to_utf16(&self, line_col: LineCol) -> LineColUtf16 {
let col = self.utf8_to_utf16_col(line_col.line, line_col.col.into());
LineColUtf16 { line: line_col.line, col: col as u32 }
}
pub fn to_utf8(&self, line_col: LineColUtf16) -> LineCol {
let col = self.utf16_to_utf8_col(line_col.line, line_col.col);
self.newlines[line_col.line as usize] + col
LineCol { line: line_col.line, col: col.into() }
}
pub fn lines(&self, range: TextRange) -> impl Iterator<Item = TextRange> + '_ {