diff options
author | Adolfo OchagavĂa <[email protected]> | 2018-11-07 09:40:58 +0000 |
---|---|---|
committer | Adolfo OchagavĂa <[email protected]> | 2018-11-07 09:40:58 +0000 |
commit | fdb9f06880ddcf29513a8c2855c3c576cc461014 (patch) | |
tree | 6488720c7f76604b1d58f7139337c6c483eb3522 /crates/ra_syntax/src/utils.rs | |
parent | c56db92d1f9b1a24de24cefd996c43c7b988b4c3 (diff) |
Store hex digits in a stack-allocated buffer
Diffstat (limited to 'crates/ra_syntax/src/utils.rs')
-rw-r--r-- | crates/ra_syntax/src/utils.rs | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/crates/ra_syntax/src/utils.rs b/crates/ra_syntax/src/utils.rs index 288d7edd4..5bef4a639 100644 --- a/crates/ra_syntax/src/utils.rs +++ b/crates/ra_syntax/src/utils.rs | |||
@@ -1,5 +1,7 @@ | |||
1 | use crate::{File, SyntaxKind, SyntaxNodeRef, WalkEvent}; | 1 | use crate::{File, SyntaxKind, SyntaxNodeRef, WalkEvent}; |
2 | use std::fmt::Write; | 2 | use std::fmt::Write; |
3 | use std::ops::Deref; | ||
4 | use std::str; | ||
3 | 5 | ||
4 | /// Parse a file and create a string representation of the resulting parse tree. | 6 | /// Parse a file and create a string representation of the resulting parse tree. |
5 | pub fn dump_tree(syntax: SyntaxNodeRef) -> String { | 7 | pub fn dump_tree(syntax: SyntaxNodeRef) -> String { |
@@ -78,3 +80,38 @@ pub(crate) fn validate_block_structure(root: SyntaxNodeRef) { | |||
78 | } | 80 | } |
79 | } | 81 | } |
80 | } | 82 | } |
83 | |||
84 | #[derive(Debug)] | ||
85 | pub struct MutAsciiString<'a> { | ||
86 | buf: &'a mut [u8], | ||
87 | len: usize, | ||
88 | } | ||
89 | |||
90 | impl<'a> MutAsciiString<'a> { | ||
91 | pub fn new(buf: &'a mut [u8]) -> MutAsciiString<'a> { | ||
92 | MutAsciiString { buf, len: 0 } | ||
93 | } | ||
94 | |||
95 | pub fn as_str(&self) -> &str { | ||
96 | str::from_utf8(&self.buf[..self.len]).unwrap() | ||
97 | } | ||
98 | |||
99 | pub fn len(&self) -> usize { | ||
100 | self.len | ||
101 | } | ||
102 | |||
103 | pub fn push(&mut self, c: char) { | ||
104 | assert!(self.len() < self.buf.len()); | ||
105 | assert!(c.is_ascii()); | ||
106 | |||
107 | self.buf[self.len] = c as u8; | ||
108 | self.len += 1; | ||
109 | } | ||
110 | } | ||
111 | |||
112 | impl<'a> Deref for MutAsciiString<'a> { | ||
113 | type Target = str; | ||
114 | fn deref(&self) -> &str { | ||
115 | self.as_str() | ||
116 | } | ||
117 | } | ||