|
| 1 | +use crate::{ |
| 2 | + builtins::{PyList, PyStrRef}, |
| 3 | + exceptions::types::PyBaseExceptionRef, |
| 4 | + sliceable::PySliceableSequence, |
| 5 | + IdProtocol, PyObjectRef, TryFromObject, TypeProtocol, VirtualMachine, |
| 6 | +}; |
| 7 | +use std::cell::RefCell; |
| 8 | +use std::thread_local; |
| 9 | + |
| 10 | +const MAX_CANDIDATE_ITEMS: usize = 750; |
| 11 | +const MAX_STRING_SIZE: usize = 40; |
| 12 | + |
| 13 | +const MOVE_COST: usize = 2; |
| 14 | +const CASE_COST: usize = 1; |
| 15 | + |
| 16 | +fn substitution_cost(mut a: u8, mut b: u8) -> usize { |
| 17 | + if (a & 31) != (b & 31) { |
| 18 | + return MOVE_COST; |
| 19 | + } |
| 20 | + if a == b { |
| 21 | + return 0usize; |
| 22 | + } |
| 23 | + if (b'A'..=b'Z').contains(&a) { |
| 24 | + a += b'a' - b'A'; |
| 25 | + } |
| 26 | + if (b'A'..=b'Z').contains(&b) { |
| 27 | + b += b'a' - b'A'; |
| 28 | + } |
| 29 | + if a == b { |
| 30 | + CASE_COST |
| 31 | + } else { |
| 32 | + MOVE_COST |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +fn levenshtein_distance(a: &str, b: &str, max_cost: usize) -> usize { |
| 37 | + thread_local! { |
| 38 | + static BUFFER: RefCell<[usize; MAX_STRING_SIZE]> = RefCell::new([0usize; MAX_STRING_SIZE]); |
| 39 | + } |
| 40 | + |
| 41 | + if a == b { |
| 42 | + return 0; |
| 43 | + } |
| 44 | + |
| 45 | + let (mut a_bytes, mut b_bytes) = (a.as_bytes(), b.as_bytes()); |
| 46 | + let (mut a_begin, mut a_end) = (0usize, a.len()); |
| 47 | + let (mut b_begin, mut b_end) = (0usize, b.len()); |
| 48 | + |
| 49 | + while a_end > 0 && b_end > 0 && (a_bytes[a_begin] == b_bytes[b_begin]) { |
| 50 | + a_begin += 1; |
| 51 | + b_begin += 1; |
| 52 | + a_end -= 1; |
| 53 | + b_end -= 1; |
| 54 | + } |
| 55 | + while a_end > 0 && b_end > 0 && (a_bytes[a_begin + a_end - 1] == b_bytes[b_begin + b_end - 1]) { |
| 56 | + a_end -= 1; |
| 57 | + b_end -= 1; |
| 58 | + } |
| 59 | + if a_end == 0 || b_end == 0 { |
| 60 | + return (a_end + b_end) * MOVE_COST; |
| 61 | + } |
| 62 | + if a_end > MAX_STRING_SIZE || b_end > MAX_STRING_SIZE { |
| 63 | + return max_cost + 1; |
| 64 | + } |
| 65 | + |
| 66 | + if b_end < a_end { |
| 67 | + std::mem::swap(&mut a_bytes, &mut b_bytes); |
| 68 | + std::mem::swap(&mut a_begin, &mut b_begin); |
| 69 | + std::mem::swap(&mut a_end, &mut b_end); |
| 70 | + } |
| 71 | + |
| 72 | + if (b_end - a_end) * MOVE_COST > max_cost { |
| 73 | + return max_cost + 1; |
| 74 | + } |
| 75 | + |
| 76 | + BUFFER.with(|buffer| { |
| 77 | + let mut buffer = buffer.borrow_mut(); |
| 78 | + for i in 0..a_end { |
| 79 | + buffer[i] = (i + 1) * MOVE_COST; |
| 80 | + } |
| 81 | + |
| 82 | + let mut result = 0usize; |
| 83 | + for (b_index, b_code) in b_bytes[b_begin..(b_begin + b_end)].iter().enumerate() { |
| 84 | + result = b_index * MOVE_COST; |
| 85 | + let mut distance = result; |
| 86 | + let mut minimum = usize::MAX; |
| 87 | + for (a_index, a_code) in a_bytes[a_begin..(a_begin + a_end)].iter().enumerate() { |
| 88 | + let substitute = distance + substitution_cost(*b_code, *a_code); |
| 89 | + distance = buffer[a_index]; |
| 90 | + let insert_delete = usize::min(result, distance) + MOVE_COST; |
| 91 | + result = usize::min(insert_delete, substitute); |
| 92 | + |
| 93 | + buffer[a_index] = result; |
| 94 | + if result < minimum { |
| 95 | + minimum = result; |
| 96 | + } |
| 97 | + } |
| 98 | + if minimum > max_cost { |
| 99 | + return max_cost + 1; |
| 100 | + } |
| 101 | + } |
| 102 | + result |
| 103 | + }) |
| 104 | +} |
| 105 | + |
| 106 | +fn calculate_suggestions(dir: PyList, name: &PyObjectRef, vm: &VirtualMachine) -> Option<PyStrRef> { |
| 107 | + let dir = dir.borrow_vec(); |
| 108 | + if dir.len() >= MAX_CANDIDATE_ITEMS { |
| 109 | + return None; |
| 110 | + } |
| 111 | + |
| 112 | + let mut suggestion: Option<PyStrRef> = None; |
| 113 | + let mut suggestion_distance = usize::MAX; |
| 114 | + let name = PyStrRef::try_from_object(vm, name.clone()).ok()?; |
| 115 | + |
| 116 | + for item in dir.iter() { |
| 117 | + let item_name = PyStrRef::try_from_object(vm, item.clone()).ok()?; |
| 118 | + if name.to_string() == item_name.to_string() { |
| 119 | + continue; |
| 120 | + } |
| 121 | + let max_distance = usize::min( |
| 122 | + (name.len() + item_name.len() + 3) * MOVE_COST / 6, |
| 123 | + suggestion_distance - 1, |
| 124 | + ); |
| 125 | + let current_distance = |
| 126 | + levenshtein_distance(name.as_str(), item_name.as_str(), max_distance); |
| 127 | + if current_distance > max_distance { |
| 128 | + continue; |
| 129 | + } |
| 130 | + if suggestion.is_none() || current_distance < suggestion_distance { |
| 131 | + suggestion = Some(item_name); |
| 132 | + suggestion_distance = current_distance; |
| 133 | + } |
| 134 | + } |
| 135 | + suggestion |
| 136 | +} |
| 137 | + |
| 138 | +pub fn offer_suggestions(exc: &PyBaseExceptionRef, vm: &VirtualMachine) -> Option<PyStrRef> { |
| 139 | + if exc.class().is(&vm.ctx.exceptions.attribute_error) { |
| 140 | + let name = vm.get_attribute(exc.as_object().clone(), "name").unwrap(); |
| 141 | + let obj = vm.get_attribute(exc.as_object().clone(), "obj").unwrap(); |
| 142 | + |
| 143 | + calculate_suggestions(vm.dir(Some(obj)).ok()?, &name, vm) |
| 144 | + } else if exc.class().is(&vm.ctx.exceptions.name_error) { |
| 145 | + let name = vm.get_attribute(exc.as_object().clone(), "name").unwrap(); |
| 146 | + let mut tb = exc.traceback().unwrap(); |
| 147 | + while let Some(traceback) = tb.next.clone() { |
| 148 | + tb = traceback; |
| 149 | + } |
| 150 | + |
| 151 | + let frame = tb.frame.clone(); |
| 152 | + let code = frame.code.clone(); |
| 153 | + let varnames = PyList::from(code.co_varnames(vm).as_slice().to_vec()); |
| 154 | + if let Some(suggestions) = calculate_suggestions(varnames, &name, vm) { |
| 155 | + return Some(suggestions); |
| 156 | + }; |
| 157 | + |
| 158 | + let globals = PyList::from(vm.extract_elements(frame.globals.as_object()).unwrap()); |
| 159 | + if let Some(suggestions) = calculate_suggestions(globals, &name, vm) { |
| 160 | + return Some(suggestions); |
| 161 | + }; |
| 162 | + |
| 163 | + let builtins = PyList::from(vm.extract_elements(frame.builtins.as_object()).unwrap()); |
| 164 | + calculate_suggestions(builtins, &name, vm) |
| 165 | + } else { |
| 166 | + None |
| 167 | + } |
| 168 | +} |
0 commit comments