feat(gen_uplc::scope): use some assumptions to simplify replace
This commit is contained in:
parent
c8ac9aa165
commit
e5980c5a96
|
@ -10,44 +10,18 @@ impl Scope {
|
||||||
self.0.is_empty()
|
self.0.is_empty()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn replace(&mut self, pattern: &Scope, mut replacement: Scope) {
|
/// Find the common ancestor with the replacement,
|
||||||
if pattern.is_empty() {
|
/// remove it from `self`, and then prepend the
|
||||||
|
/// `replacement` to `self`.
|
||||||
|
pub fn replace(&mut self, mut replacement: Scope) {
|
||||||
|
let common = self.common_ancestor(&replacement);
|
||||||
|
|
||||||
|
// we know that common will always be in the front of the
|
||||||
|
// scope Vec so we can always drain `0..common.len()`.
|
||||||
|
self.0.drain(0..common.0.len());
|
||||||
|
|
||||||
replacement.0.extend(self.0.iter());
|
replacement.0.extend(self.0.iter());
|
||||||
self.0 = replacement.0;
|
self.0 = replacement.0;
|
||||||
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
let mut result = Vec::new();
|
|
||||||
|
|
||||||
let mut index = 0;
|
|
||||||
let mut pattern_index = 0;
|
|
||||||
|
|
||||||
let mut no_matches = true;
|
|
||||||
|
|
||||||
while index < self.0.len() {
|
|
||||||
if self.0[index] == pattern.0[pattern_index] {
|
|
||||||
if pattern_index == pattern.0.len() - 1 {
|
|
||||||
no_matches = false;
|
|
||||||
result.extend(replacement.0.clone());
|
|
||||||
pattern_index = 0;
|
|
||||||
} else {
|
|
||||||
pattern_index += 1;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
result.push(self.0[index]);
|
|
||||||
pattern_index = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
index += 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
if no_matches {
|
|
||||||
replacement.0.extend(self.0.iter());
|
|
||||||
self.0 = replacement.0;
|
|
||||||
} else {
|
|
||||||
self.0 = result;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn common_ancestor(&self, other: &Self) -> Scope {
|
pub fn common_ancestor(&self, other: &Self) -> Scope {
|
||||||
|
|
|
@ -54,10 +54,8 @@ impl AirStack {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn merge_child(&mut self, mut other: AirStack) {
|
pub fn merge_child(&mut self, mut other: AirStack) {
|
||||||
let pattern = self.scope.common_ancestor(&other.scope);
|
|
||||||
|
|
||||||
for ir in other.air.iter_mut() {
|
for ir in other.air.iter_mut() {
|
||||||
ir.scope_mut().replace(&pattern, self.scope.clone());
|
ir.scope_mut().replace(self.scope.clone());
|
||||||
}
|
}
|
||||||
|
|
||||||
self.merge(other);
|
self.merge(other);
|
||||||
|
|
Loading…
Reference in New Issue