summaryrefslogtreecommitdiff
path: root/grammar/src/label.rs
diff options
context:
space:
mode:
authorJSDurand <mmemmew@gmail.com>2023-01-22 11:49:47 +0800
committerJSDurand <mmemmew@gmail.com>2023-01-22 11:49:47 +0800
commite8ea01319b3a9032a3f4f69f65e9ca96562b87b9 (patch)
tree674e7337dce0b9433b9ddfe745b0cf82f528d3ec /grammar/src/label.rs
parent973c789dae479dd8383b0f7f9cfa5f167fdf3d38 (diff)
forest: clone correctly
Now the forest can detect if a node is packed or cloned, and correctly clones a node in those circumstances. But it still needs to be tested.
Diffstat (limited to 'grammar/src/label.rs')
-rw-r--r--grammar/src/label.rs22
1 files changed, 18 insertions, 4 deletions
diff --git a/grammar/src/label.rs b/grammar/src/label.rs
index 58eaddc..05b0b1e 100644
--- a/grammar/src/label.rs
+++ b/grammar/src/label.rs
@@ -93,10 +93,24 @@ impl GrammarLabel {
/// Return a string description with the help of the associated
/// grammar.
pub fn to_string(&self, grammar: &Grammar) -> Result<String, Error> {
- // REVIEW: It needs at least 34 bytes, so we just double it.
- // Of course we can also calculate the length exactly, but
- // this can be postponed till later.
- let mut s = String::with_capacity(68);
+ // First calculate the length of the resulting string.
+
+ let mut num = 2 + 7 + 14 + 6;
+
+ num += self.label().name(grammar)?.len();
+
+ num += format!("{} ", self.start()).len();
+
+ if let Some(end) = self.end() {
+ num += format!("to {end}").len();
+ } else {
+ num += 7;
+ }
+
+ let num = num;
+
+ let mut s = String::with_capacity(num);
+
s.push_str("a ");
if self.is_packed() {