Skip to content

Commit

Permalink
Merge pull request #2600 from lavafroth/offset-shortcircuit
Browse files Browse the repository at this point in the history
refactor: short circuit for known constraints in position lookups
  • Loading branch information
amaanq committed Sep 23, 2023
2 parents 6bbb50b + 0baf1e3 commit 2cbfcbc
Showing 1 changed file with 25 additions and 10 deletions.
35 changes: 25 additions & 10 deletions cli/src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -370,10 +370,22 @@ fn parse_edit_flag(source_code: &Vec<u8>, flag: &str) -> Result<Edit> {
})
}

fn offset_for_position(input: &Vec<u8>, position: Point) -> usize {
fn offset_for_position(input: &[u8], position: Point) -> usize {
let mut current_position = Point { row: 0, column: 0 };
for (i, c) in input.iter().enumerate() {
if *c as char == '\n' {
let mut it = input.iter().enumerate();

// Skip to where row equals position.row
for (_, &c) in &mut it {
if c == b'\n' {
current_position.row += 1;
}
if current_position.row == position.row {
break;
}
}

for (i, &c) in &mut it {
if c == b'\n' {
current_position.row += 1;
current_position.column = 0;
} else {
Expand All @@ -383,18 +395,21 @@ fn offset_for_position(input: &Vec<u8>, position: Point) -> usize {
return i;
}
}
return input.len();

input.len()
}

fn position_for_offset(input: &Vec<u8>, offset: usize) -> Point {
fn position_for_offset(input: &[u8], offset: usize) -> Point {
let mut result = Point { row: 0, column: 0 };
for c in &input[0..offset] {
if *c as char == '\n' {
result.row += 1;
result.column = 0;
} else {

for &c in input[0..offset].iter().rev() {
if c != b'\n' && result.row == 0 {
result.column += 1;
}
if c == b'\n' {
result.row += 1;
}
}

result
}

0 comments on commit 2cbfcbc

Please sign in to comment.