From ed2ac1713326df6b926062efcc6109a20cdf7c37 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Mon, 13 Aug 2018 14:24:22 +0300 Subject: smol_str to a crate --- crates/libsyntax2/Cargo.toml | 1 + crates/libsyntax2/src/ast/mod.rs | 9 +++- crates/libsyntax2/src/lib.rs | 4 +- crates/libsyntax2/src/smol_str.rs | 83 ---------------------------------- crates/libsyntax2/src/yellow/green.rs | 17 +++++-- crates/libsyntax2/src/yellow/syntax.rs | 6 +++ 6 files changed, 28 insertions(+), 92 deletions(-) delete mode 100644 crates/libsyntax2/src/smol_str.rs (limited to 'crates/libsyntax2') diff --git a/crates/libsyntax2/Cargo.toml b/crates/libsyntax2/Cargo.toml index 4c4040fe5..810952a0f 100644 --- a/crates/libsyntax2/Cargo.toml +++ b/crates/libsyntax2/Cargo.toml @@ -10,6 +10,7 @@ text_unit = "0.1.2" itertools = "0.7.5" drop_bomb = "0.1.4" parking_lot = "0.6.0" +smol_str = { path = "../smol_str" } [dev-dependencies] assert_eq_text = { path = "../assert_eq_text" } diff --git a/crates/libsyntax2/src/ast/mod.rs b/crates/libsyntax2/src/ast/mod.rs index 56bc099fe..e9362d048 100644 --- a/crates/libsyntax2/src/ast/mod.rs +++ b/crates/libsyntax2/src/ast/mod.rs @@ -1,6 +1,9 @@ mod generated; use std::sync::Arc; + +use smol_str::SmolStr; + use { SyntaxNode, SyntaxRoot, TreeRoot, SyntaxError, SyntaxKind::*, @@ -64,7 +67,9 @@ impl Function { } impl Name { - pub fn text(&self) -> String { - self.syntax().text() + pub fn text(&self) -> SmolStr { + let ident = self.syntax().first_child() + .unwrap(); + ident.leaf_text().unwrap() } } diff --git a/crates/libsyntax2/src/lib.rs b/crates/libsyntax2/src/lib.rs index ca33618a0..feef542c4 100644 --- a/crates/libsyntax2/src/lib.rs +++ b/crates/libsyntax2/src/lib.rs @@ -21,10 +21,11 @@ //#![warn(unreachable_pub)] // rust-lang/rust#47816 extern crate itertools; -extern crate text_unit; extern crate unicode_xid; extern crate drop_bomb; extern crate parking_lot; +extern crate smol_str; +extern crate text_unit; pub mod algo; pub mod ast; @@ -35,7 +36,6 @@ mod grammar; mod parser_impl; mod syntax_kinds; -mod smol_str; mod yellow; /// Utilities for simple uses of the parser. pub mod utils; diff --git a/crates/libsyntax2/src/smol_str.rs b/crates/libsyntax2/src/smol_str.rs deleted file mode 100644 index abf69dce7..000000000 --- a/crates/libsyntax2/src/smol_str.rs +++ /dev/null @@ -1,83 +0,0 @@ -use std::{sync::Arc}; - -const INLINE_CAP: usize = 22; -const WS_TAG: u8 = (INLINE_CAP + 1) as u8; - -#[derive(Clone, Debug)] -pub(crate) enum SmolStr { - Heap(Arc), - Inline { - len: u8, - buf: [u8; INLINE_CAP], - }, -} - -impl SmolStr { - pub fn new(text: &str) -> SmolStr { - let len = text.len(); - if len <= INLINE_CAP { - let mut buf = [0; INLINE_CAP]; - buf[..len].copy_from_slice(text.as_bytes()); - return SmolStr::Inline { len: len as u8, buf }; - } - - let newlines = text.bytes().take_while(|&b| b == b'\n').count(); - let spaces = text[newlines..].bytes().take_while(|&b| b == b' ').count(); - if newlines + spaces == len && newlines <= N_NEWLINES && spaces <= N_SPACES { - let mut buf = [0; INLINE_CAP]; - buf[0] = newlines as u8; - buf[1] = spaces as u8; - return SmolStr::Inline { len: WS_TAG, buf }; - } - - SmolStr::Heap( - text.to_string().into_boxed_str().into() - ) - } - - pub fn as_str(&self) -> &str { - match self { - SmolStr::Heap(data) => &*data, - SmolStr::Inline { len, buf } => { - if *len == WS_TAG { - let newlines = buf[0] as usize; - let spaces = buf[1] as usize; - assert!(newlines <= N_NEWLINES && spaces <= N_SPACES); - return &WS[N_NEWLINES - newlines..N_NEWLINES + spaces] - } - - let len = *len as usize; - let buf = &buf[..len]; - unsafe { ::std::str::from_utf8_unchecked(buf) } - } - } - } -} - -const N_NEWLINES: usize = 32; -const N_SPACES: usize = 128; -const WS: &str = - "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n "; - - -#[cfg(test)] -mod tests { - use super::*; - - #[test] - #[cfg(target_pointer_width = "64")] - fn smol_str_is_smol() { - assert_eq!(::std::mem::size_of::(), 8 + 8 + 8) - } - - #[test] - fn test_round_trip() { - let mut text = String::new(); - for n in 0..256 { - let smol = SmolStr::new(&text); - assert_eq!(smol.as_str(), text.as_str()); - text.push_str(&n.to_string()); - } - } -} - diff --git a/crates/libsyntax2/src/yellow/green.rs b/crates/libsyntax2/src/yellow/green.rs index f505b26d7..700f2704f 100644 --- a/crates/libsyntax2/src/yellow/green.rs +++ b/crates/libsyntax2/src/yellow/green.rs @@ -1,8 +1,8 @@ use std::sync::Arc; -use { - SyntaxKind, TextUnit, - smol_str::SmolStr, -}; + +use smol_str::SmolStr; + +use {SyntaxKind, TextUnit}; #[derive(Clone, Debug)] pub(crate) enum GreenNode { @@ -31,7 +31,7 @@ impl GreenNode { pub fn text_len(&self) -> TextUnit { match self { - GreenNode::Leaf { text, ..} => TextUnit::of_str(text.as_str()), + GreenNode::Leaf { text, .. } => TextUnit::of_str(text.as_str()), GreenNode::Branch(b) => b.text_len(), } } @@ -54,6 +54,13 @@ impl GreenNode { } } } + + pub fn leaf_text(&self) -> Option { + match self { + GreenNode::Leaf { text, .. } => Some(text.clone()), + GreenNode::Branch(_) => None, + } + } } #[derive(Clone, Debug)] diff --git a/crates/libsyntax2/src/yellow/syntax.rs b/crates/libsyntax2/src/yellow/syntax.rs index 00f76e51c..b264e008a 100644 --- a/crates/libsyntax2/src/yellow/syntax.rs +++ b/crates/libsyntax2/src/yellow/syntax.rs @@ -1,5 +1,7 @@ use std::{fmt, sync::Arc}; +use smol_str::SmolStr; + use { yellow::{RedNode, TreeRoot, SyntaxRoot, RedPtr}, SyntaxKind::{self, *}, @@ -116,6 +118,10 @@ impl SyntaxNode { self.first_child().is_none() } + pub fn leaf_text(&self) -> Option { + self.red().green().leaf_text() + } + fn red(&self) -> &RedNode { unsafe { self.red.get(&self.root) } } -- cgit v1.2.3