Skip to content

Commit 3234fe0

Browse files
committed
Address review
1 parent f8b461a commit 3234fe0

1 file changed

Lines changed: 18 additions & 21 deletions

File tree

src/sourceview.rs

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -174,17 +174,19 @@ impl SourceView {
174174
let idx = idx as usize;
175175

176176
let get_from_line_ends = |line_ends: &[LineEndOffset]| {
177-
line_ends.get(idx).map(|&end_offset| {
178-
let start_offset = if idx == 0 {
179-
0
180-
} else {
181-
line_ends[idx - 1].to_start_index()
182-
};
183-
&self.source[start_offset..end_offset.to_end_index()]
184-
})
177+
let end = line_ends.get(idx)?.to_end_index();
178+
let start = if idx == 0 {
179+
0
180+
} else {
181+
line_ends[idx - 1].to_start_index()
182+
};
183+
Some(&self.source[start..end])
185184
};
186185

187-
let mut line_ends = self.line_end_offsets.lock().unwrap();
186+
let mut line_ends = self
187+
.line_end_offsets
188+
.lock()
189+
.unwrap_or_else(|e| e.into_inner());
188190

189191
if let Some(line) = get_from_line_ends(&line_ends) {
190192
return Some(line);
@@ -329,8 +331,10 @@ impl SourceView {
329331
}
330332

331333
/// A wrapper around an index that stores a [`LineTerminator`] in its 2 lowest bits.
334+
// We use `u64` instead of `usize` in order to not lose data when bit-packing
335+
// on 32-bit targets.
332336
#[derive(Clone, Copy)]
333-
struct LineEndOffset(usize);
337+
struct LineEndOffset(u64);
334338

335339
#[derive(Clone, Copy)]
336340
enum LineTerminator {
@@ -341,26 +345,19 @@ enum LineTerminator {
341345

342346
impl LineEndOffset {
343347
fn new(index: usize, line_end: LineTerminator) -> Self {
344-
let shifted = index << 2;
345-
346-
// check for overflow - on 64-bit, this isn't a concern, since you'd have to
347-
// have a source string longer than 4 exabytes
348-
#[cfg(any(target_pointer_width = "16", target_pointer_width = "32"))]
349-
if shifted >> 2 != index {
350-
panic!("index too large!")
351-
}
348+
let shifted = (index as u64) << 2;
352349

353-
Self(shifted | line_end as usize)
350+
Self(shifted | line_end as u64)
354351
}
355352

356353
/// Return the index of the end of this line.
357354
fn to_end_index(self) -> usize {
358-
self.0 >> 2
355+
(self.0 >> 2) as usize
359356
}
360357

361358
/// Return the index of the start of the next line.
362359
fn to_start_index(self) -> usize {
363-
self.to_end_index() + (self.0 & 0b11)
360+
self.to_end_index() + (self.0 & 0b11) as usize
364361
}
365362
}
366363

0 commit comments

Comments
 (0)