Skip to content

Commit

Permalink
Change binary search to linear search (temporary)
Browse files Browse the repository at this point in the history
  • Loading branch information
evanrittenhouse committed Apr 3, 2023
1 parent e02e9ec commit dca4605
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 23 deletions.
4 changes: 2 additions & 2 deletions crates/ruff/resources/test/fixtures/pycodestyle/W19.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,13 +97,13 @@ def long_function_name(
if os.path.exists(os.path.join(path, PEP8_BIN)):
cmd = ([os.path.join(path, PEP8_BIN)] +
self._pep8_options(targetfile))
#: W191
#: W191 - okay
''' multiline string with tab in it, same lines'''
""" here we're using '''different delimiters'''"""
'''
multiline string with tab in it, different lines
'''
#: E101 W191
#: E101
'''multiline string
with tabs
and spaces
Expand Down
25 changes: 5 additions & 20 deletions crates/ruff/src/rules/pycodestyle/rules/tab_indentation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,29 +19,14 @@ impl Violation for TabIndentation {
/// strings aren't overlapping (otherwise there'd only be one string). This function performs a
/// binary search on string_lines to find the string that contains (or starts just before) lineno
fn find_closest_string<'a>(lineno: &usize, string_lines: &'a [Range]) -> Option<&'a Range> {
if string_lines.is_empty() {
return None;
}

let mut left = 0;
let mut right = string_lines.len();

while left < right {
let mid = (left + right) / 2;
let curr_range = &string_lines[mid];
let start_row = &curr_range.location.row();
let end_row = &curr_range.end_location.row();

if start_row <= lineno && end_row >= lineno {
return Some(curr_range);
} else if start_row > lineno {
right = mid;
} else if end_row < lineno {
left = mid + 1;
// TODO: change this to a binary search
for range in string_lines {
if &range.location.row() <= lineno && &range.end_location.row() >= lineno {
return Some(range);
}
}

return Some(&string_lines[right - 1]);
return None;
}

/// W191
Expand Down
2 changes: 1 addition & 1 deletion crates/ruff_python_ast/src/source_code/indexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ impl From<&[LexResult]> for Indexer {
Tok::String {
value: _,
kind: _,
triple_quoted: _
triple_quoted: true
}
) {
string_lines.push(Range::new(start.clone(), end.clone()));
Expand Down

0 comments on commit dca4605

Please sign in to comment.