1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
use super::super::super::HighLevelEmitter; impl HighLevelEmitter { /// Collapse `((expr))` to `(expr)` whenever the inner parens form a /// matched pair surrounding the entire content. pub(crate) fn reduce_double_parens(statements: &mut [String]) { for stmt in statements.iter_mut() { loop { let mut next: Option<String> = None; let bytes = stmt.as_bytes(); let mut i = 0; while i + 1 < bytes.len() { if bytes[i] == b'(' && bytes[i + 1] == b'(' { let inner_open = i + 1; let mut depth = 1usize; let mut j = inner_open + 1; while j < bytes.len() { match bytes[j] { b'(' => depth += 1, b')' => { depth -= 1; if depth == 0 { break; } } _ => {} } j += 1; } if depth != 0 { break; } if j + 1 < bytes.len() && bytes[j + 1] == b')' { let mut rebuilt = String::with_capacity(stmt.len() - 2); rebuilt.push_str(&stmt[..i]); rebuilt.push('('); rebuilt.push_str(&stmt[inner_open + 1..j]); rebuilt.push(')'); rebuilt.push_str(&stmt[j + 2..]); next = Some(rebuilt); break; } } i += 1; } match next { Some(rebuilt) => *stmt = rebuilt, None => break, } } } } }