Skip to content

Commit 1a969cf

Browse files
committed
Remove FIXME about duplicated code
1 parent d718865 commit 1a969cf

File tree

1 file changed

+25
-29
lines changed

1 file changed

+25
-29
lines changed

src/macros.rs

Lines changed: 25 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,28 @@ pub fn rewrite_macro_def(
400400
Some(result)
401401
}
402402

403+
fn register_metavariable(
404+
map: &mut HashMap<String, String>,
405+
result: &mut String,
406+
name: &str,
407+
dollar_count: usize,
408+
) {
409+
let mut new_name = String::new();
410+
let mut old_name = String::new();
411+
412+
old_name.push('$');
413+
for _ in 0..(dollar_count - 1) {
414+
new_name.push('$');
415+
old_name.push('$');
416+
}
417+
new_name.push('z');
418+
new_name.push_str(&name);
419+
old_name.push_str(&name);
420+
421+
result.push_str(&new_name);
422+
map.insert(old_name, new_name);
423+
}
424+
403425
// Replaces `$foo` with `zfoo`. We must check for name overlap to ensure we
404426
// aren't causing problems.
405427
// This should also work for escaped `$` variables, where we leave earlier `$`s.
@@ -419,24 +441,11 @@ fn replace_names(input: &str) -> Option<(String, HashMap<String, String>)> {
419441
result.push(c);
420442
} else if !c.is_alphanumeric() && !cur_name.is_empty() {
421443
// Terminates a name following one or more dollars.
422-
let mut new_name = String::new();
423-
let mut old_name = String::new();
424-
old_name.push('$');
425-
for _ in 0..(dollar_count - 1) {
426-
new_name.push('$');
427-
old_name.push('$');
428-
}
429-
new_name.push('z');
430-
new_name.push_str(&cur_name);
431-
old_name.push_str(&cur_name);
432-
433-
result.push_str(&new_name);
434-
substs.insert(old_name, new_name);
444+
register_metavariable(&mut substs, &mut result, &cur_name, dollar_count);
435445

436446
result.push(c);
437-
438447
dollar_count = 0;
439-
cur_name = String::new();
448+
cur_name.clear();
440449
} else if c == '(' && cur_name.is_empty() {
441450
// FIXME: Support macro def with repeat.
442451
return None;
@@ -445,21 +454,8 @@ fn replace_names(input: &str) -> Option<(String, HashMap<String, String>)> {
445454
}
446455
}
447456

448-
// FIXME: duplicate code
449457
if !cur_name.is_empty() {
450-
let mut new_name = String::new();
451-
let mut old_name = String::new();
452-
old_name.push('$');
453-
for _ in 0..(dollar_count - 1) {
454-
new_name.push('$');
455-
old_name.push('$');
456-
}
457-
new_name.push('z');
458-
new_name.push_str(&cur_name);
459-
old_name.push_str(&cur_name);
460-
461-
result.push_str(&new_name);
462-
substs.insert(old_name, new_name);
458+
register_metavariable(&mut substs, &mut result, &cur_name, dollar_count);
463459
}
464460

465461
debug!("replace_names `{}` {:?}", result, substs);

0 commit comments

Comments
 (0)