asciidork_ast/
source_string.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
use std::ops::Deref;

use crate::internal::*;

#[derive(PartialEq, Eq, Clone, Hash)]
pub struct SourceString<'arena> {
  pub src: BumpString<'arena>,
  pub loc: SourceLocation,
}

impl<'arena> SourceString<'arena> {
  pub const fn new(src: BumpString<'arena>, loc: SourceLocation) -> Self {
    Self { src, loc }
  }

  pub fn split_once(self, separator: &str, bump: &'arena Bump) -> (Self, Option<Self>) {
    match self.src.split_once(separator) {
      Some((left, right)) => (
        Self::new(
          BumpString::from_str_in(left, bump),
          SourceLocation::new_depth(
            self.loc.start,
            self.loc.start + left.len() as u32,
            self.loc.include_depth,
          ),
        ),
        Some(Self::new(
          BumpString::from_str_in(right, bump),
          SourceLocation::new_depth(
            self.loc.start + left.len() as u32 + separator.len() as u32,
            self.loc.end,
            self.loc.include_depth,
          ),
        )),
      ),
      None => (self, None),
    }
  }

  pub fn drop_first(&mut self) {
    self.src.drain(..1);
    self.loc.start += 1;
  }
}

impl Json for SourceString<'_> {
  fn to_json_in(&self, buf: &mut JsonBuf) {
    self.src.to_json_in(buf);
  }
}

impl<'arena> Deref for SourceString<'arena> {
  type Target = str;

  fn deref(&self) -> &Self::Target {
    &self.src
  }
}

impl<'arena> AsRef<str> for SourceString<'arena> {
  fn as_ref(&self) -> &str {
    &self.src
  }
}

impl<'arena> std::cmp::PartialEq<str> for SourceString<'arena> {
  fn eq(&self, other: &str) -> bool {
    self.src == other
  }
}

impl<'arena> std::fmt::Debug for SourceString<'arena> {
  fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
    write!(f, "SourceString{{\"{}\",{:?}}}", self.src, self.loc)
  }
}