Skip to content
Closed
21 changes: 14 additions & 7 deletions src/sourcemap/InternalSourceMap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -628,9 +628,9 @@ impl InternalSourceMap {
}
}

/// Stateful forward cursor. `move_to` is cheap when successive targets are
/// monotonically non-decreasing in generated position; otherwise it reseeks via
/// the sync index.
/// Stateful forward cursor. `move_to` is cheapest for nearby non-decreasing
/// targets; a backward jump or a jump past the next sync entry reseeks, so each
/// call is bounded by one `locate_window` plus at most `SYNC_INTERVAL` deltas.
Comment thread
robobun marked this conversation as resolved.
Resolved
///
/// Invariant: when `has_state`, `reader` is positioned such that calling
/// `advance_one()` produces the mapping immediately after `peek orelse state`.
Expand Down Expand Up @@ -659,10 +659,17 @@ impl Cursor {
let target_line = line.zero_based();
let target_col = column.zero_based();

if !self.has_state || !self.state.less_or_equal(target_line, target_col) {
if !self.reseek(target_line, target_col) {
return None;
}
let must_reseek =
!self.has_state || !self.state.less_or_equal(target_line, target_col) || {
let next = self.sync_idx as usize + 1;
next < self.map.sync_count() as usize
&& self
.map
.sync_entry(next)
.less_or_equal(target_line, target_col)
};
if must_reseek && !self.reseek(target_line, target_col) {
return None;
}

loop {
Expand Down
31 changes: 0 additions & 31 deletions src/sourcemap/LineOffsetTable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,37 +118,6 @@ impl LineOffsetTable {
Self::find_line(offsets, loc)
}

pub fn find_index(byte_offsets_to_start_of_line: &[u32], loc: Loc) -> Option<usize> {
debug_assert!(loc.start > -1); // checked by caller
let mut original_line: usize = 0;
let loc_start = usize::try_from(loc.start).expect("int cast");

let mut count = byte_offsets_to_start_of_line.len();
while count > 0 {
let step = count / 2;
let i = original_line + step;
let byte_offset = byte_offsets_to_start_of_line[i] as usize;
if byte_offset == loc_start {
return Some(i);
}
if i + 1 < byte_offsets_to_start_of_line.len() {
let next_byte_offset = byte_offsets_to_start_of_line[i + 1] as usize;
if byte_offset < loc_start && loc_start < next_byte_offset {
return Some(i);
}
}

if byte_offset < loc_start {
original_line = i + 1;
count = count - step - 1;
} else {
count = step;
}
}

None
}

/// `Global`-allocator convenience wrapper around [`generate_in`].
pub fn generate(contents: &[u8], approximate_line_count: i32) -> Result<List, AllocError> {
Self::generate_in::<Global>(contents, approximate_line_count)
Expand Down
Loading
Loading