Fixed substitution bug

pull/2/head
Mark 2023-07-28 15:01:43 -07:00
parent 241bb572a5
commit 401e877a2f
Signed by: Mark
GPG Key ID: AD62BB059C2AAEE4
1 changed files with 10 additions and 8 deletions

View File

@ -9,12 +9,12 @@ use super::super::{
pub fn find_subs( pub fn find_subs(
mut g: VecDeque<Token>, mut g: VecDeque<Token>,
) -> ( ) -> (
Vec<(LineLocation, String)>, VecDeque<(LineLocation, String)>,
VecDeque<Token> VecDeque<Token>
) { ) {
// Array of replacements // Array of replacements
let mut r: Vec<(LineLocation, String)> = Vec::with_capacity(8); let mut r: VecDeque<(LineLocation, String)> = VecDeque::with_capacity(8);
// New token array, with updated locations // New token array, with updated locations
let mut n: VecDeque<Token> = VecDeque::with_capacity(g.len()); let mut n: VecDeque<Token> = VecDeque::with_capacity(g.len());
@ -22,8 +22,7 @@ pub fn find_subs(
let mut offset: usize = 0; let mut offset: usize = 0;
while g.len() > 0 { while g.len() > 0 {
// Read in reverse. Very important! let mut t = g.pop_front().unwrap();
let mut t = g.pop_back().unwrap();
let target: Option<&str> = match &mut t { let target: Option<&str> = match &mut t {
Token::Operator(_, s) => { Token::Operator(_, s) => {
@ -85,11 +84,14 @@ pub fn find_subs(
} else { } else {
let target = target.unwrap(); let target = target.unwrap();
let l = t.get_mut_line_location(); let l = t.get_mut_line_location();
r.push((l.clone(), String::from(target))); r.push_front((l.clone(), String::from(target)));
*l = LineLocation{ pos: l.pos - offset, len: target.chars().count()};
offset += l.len - target.chars().count(); let old_len = l.len;
let new_len = target.chars().count();
*l = LineLocation{ pos: l.pos - offset, len: new_len};
offset += old_len - new_len;
} }
n.push_front(t); n.push_back(t);
} }
return (r, n); return (r, n);