From 7e74af32268f9b0783ca94107b0b10d52e4ebe5e Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 28 Aug 2018 14:06:30 +0300 Subject: Avoid materializing strings --- crates/libsyntax2/src/yellow/green.rs | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) (limited to 'crates/libsyntax2/src/yellow/green.rs') diff --git a/crates/libsyntax2/src/yellow/green.rs b/crates/libsyntax2/src/yellow/green.rs index 700f2704f..59aefb0de 100644 --- a/crates/libsyntax2/src/yellow/green.rs +++ b/crates/libsyntax2/src/yellow/green.rs @@ -43,21 +43,13 @@ impl GreenNode { } } - pub fn text(&self) -> String { - let mut buff = String::new(); - go(self, &mut buff); - return buff; - fn go(node: &GreenNode, buff: &mut String) { - match node { - GreenNode::Leaf { text, .. } => buff.push_str(text.as_str()), - GreenNode::Branch(b) => b.children().iter().for_each(|child| go(child, buff)), - } - } + pub fn leaf_text(&self) -> Option { + self.leaf_text_ref().map(Clone::clone) } - pub fn leaf_text(&self) -> Option { + pub fn leaf_text_ref(&self) -> Option<&SmolStr> { match self { - GreenNode::Leaf { text, .. } => Some(text.clone()), + GreenNode::Leaf { text, .. } => Some(text), GreenNode::Branch(_) => None, } } -- cgit v1.2.3