open_vaf/
literals.rs

1use bitflags::_core::fmt::Formatter;
2use parking_lot::{MappedRwLockReadGuard, RwLock, RwLockReadGuard};
3use std::fmt;
4use std::fmt::Display;
5use std::ops::{Index, IndexMut, Range};
6
7lazy_static! {
8    static ref BUFFER: RwLock<StringLiteralBuffer> =
9        RwLock::new(StringLiteralBuffer(String::with_capacity(8192)));
10}
11
12#[derive(Clone, Default, Debug)]
13pub struct StringLiteralBuffer(pub String);
14
15impl StringLiteralBuffer {
16    pub fn get_str(&self, range: StringLiteral) -> &str {
17        &self.0[range.as_usize_range()]
18    }
19
20    pub fn get_str_mut(&mut self, range: StringLiteral) -> &mut str {
21        &mut self.0[range.as_usize_range()]
22    }
23
24    pub fn add_str_literal(&mut self, string: &str) -> Result<StringLiteral, ()> {
25        if string.len() > u16::MAX as usize {
26            Err(())
27        } else {
28            let start = self.0.len();
29            self.0.push_str(string);
30            Ok((start..self.0.len()).into())
31        }
32    }
33}
34
35impl Index<StringLiteral> for StringLiteralBuffer {
36    type Output = str;
37
38    fn index(&self, range: StringLiteral) -> &Self::Output {
39        self.get_str(range)
40    }
41}
42
43impl IndexMut<StringLiteral> for StringLiteralBuffer {
44    fn index_mut(&mut self, range: StringLiteral) -> &mut Self::Output {
45        self.get_str_mut(range)
46    }
47}
48
49#[derive(Copy, Clone, Debug)]
50pub struct StringLiteral {
51    pub start: u16,
52    pub end: u16,
53}
54
55impl StringLiteral {
56    pub fn new(src: &str) -> Result<Self, ()> {
57        BUFFER.write().add_str_literal(src)
58    }
59
60    pub fn as_usize_range(self) -> Range<usize> {
61        (self.start as usize)..(self.end as usize)
62    }
63    pub fn as_raw_range(self) -> Range<u16> {
64        self.start..self.end
65    }
66    pub fn as_str<'lt>(self) -> MappedRwLockReadGuard<'lt, str> {
67        RwLockReadGuard::map(BUFFER.read(), |buff| &buff[self])
68    }
69
70    pub const fn empty() -> Self {
71        Self { start: 0, end: 0 }
72    }
73}
74
75impl From<Range<u16>> for StringLiteral {
76    fn from(range: Range<u16>) -> Self {
77        Self {
78            start: range.start,
79            end: range.end,
80        }
81    }
82}
83
84impl From<Range<usize>> for StringLiteral {
85    fn from(range: Range<usize>) -> Self {
86        Self {
87            start: range.start as u16,
88            end: range.end as u16,
89        }
90    }
91}
92
93impl Into<Range<u16>> for StringLiteral {
94    fn into(self) -> Range<u16> {
95        self.start..self.end
96    }
97}
98
99impl Into<Range<usize>> for StringLiteral {
100    fn into(self) -> Range<usize> {
101        self.as_usize_range()
102    }
103}
104
105impl PartialEq for StringLiteral {
106    fn eq(&self, other: &Self) -> bool {
107        let buffer = BUFFER.read();
108        buffer[*self] == buffer[*other]
109    }
110}
111
112impl Eq for StringLiteral {}
113
114impl Display for StringLiteral {
115    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
116        f.write_str(&BUFFER.read()[*self])
117    }
118}