Skip to content

Commit 9ab2cfd

Browse files
committed
added utility function
1 parent bc2a44d commit 9ab2cfd

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

src/libsyntax/ast_util.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -985,13 +985,50 @@ pub fn getLast(arr: &~[Mrk]) -> uint {
985985
*arr.last()
986986
}
987987
988+
// are two paths equal when compared unhygienically?
989+
// since I'm using this to replace ==, it seems appropriate
990+
// to compare the span, global, etc. fields as well.
991+
pub fn path_name_eq(a : &ast::Path, b : &ast::Path) -> bool {
992+
(a.span == b.span)
993+
&& (a.global == b.global)
994+
// NOTE: ident->name in lifetimes!
995+
&& (a.rp == b.rp)
996+
// NOTE: can a type contain an ident?
997+
&& (a.types == b.types)
998+
&& (idents_name_eq(a.idents, b.idents))
999+
}
1000+
1001+
// are two arrays of idents equal when compared unhygienically?
1002+
pub fn idents_name_eq(a : &[ast::ident], b : &[ast::ident]) -> bool {
1003+
if (a.len() != b.len()) {
1004+
false
1005+
} else {
1006+
for a.iter().enumerate().advance |(idx,id)|{
1007+
if (id.name != b[idx].name) {
1008+
return false;
1009+
}
1010+
}
1011+
true
1012+
}
1013+
}
9881014
9891015
#[cfg(test)]
9901016
mod test {
9911017
use ast::*;
9921018
use super::*;
9931019
use std::io;
9941020
1021+
#[test] fn idents_name_eq_test() {
1022+
assert!(idents_name_eq(~[ident{name:3,ctxt:4},
1023+
ident{name:78,ctxt:82}],
1024+
~[ident{name:3,ctxt:104},
1025+
ident{name:78,ctxt:182}]));
1026+
assert!(!idents_name_eq(~[ident{name:3,ctxt:4},
1027+
ident{name:78,ctxt:82}],
1028+
~[ident{name:3,ctxt:104},
1029+
ident{name:77,ctxt:182}]));
1030+
}
1031+
9951032
#[test] fn xorpush_test () {
9961033
let mut s = ~[];
9971034
xorPush(&mut s,14);

0 commit comments

Comments
 (0)