#[cfg(test)]
thread_local! {
static WORK: std::cell::Cell<usize> = const { std::cell::Cell::new(0) };
static TOUCHED: std::cell::Cell<usize> = const { std::cell::Cell::new(0) };
}
#[cfg(all(test, feature = "visitor"))]
pub(super) type Counted<T> = (&'static str, fn(&mut T));
#[derive(Default)]
pub(super) struct OutputText {
text: String,
}
impl OutputText {
pub(super) fn with_capacity(capacity: usize) -> Self {
let mut output = Self::default();
output.text.reserve(capacity);
output
}
pub(super) const fn len(&self) -> usize {
self.text.len()
}
pub(super) fn into_string(self) -> String {
#[cfg(test)]
Self::count(self.text.len());
self.text
}
pub(super) fn push_str(&mut self, text: &str) {
#[cfg(test)]
Self::count(text.len());
self.text.push_str(text);
}
pub(super) fn insert_str(&mut self, at: usize, text: &str) {
#[cfg(test)]
Self::count(self.text.len() - at + text.len());
self.text.insert_str(at, text);
}
#[cfg(feature = "visitor")]
pub(super) fn remove(&mut self, range: std::ops::Range<usize>) {
#[cfg(test)]
Self::count(self.text.len() - range.end);
self.text.drain(range);
}
pub(super) fn truncate(&mut self, len: usize) {
#[cfg(test)]
Self::count(1);
self.text.truncate(len);
}
pub(super) fn char_before(&self, end: usize) -> &str {
#[cfg(test)]
Self::count(1);
let head = &self.text[..end];
head.char_indices().next_back().map_or("", |(start, _)| &head[start..])
}
pub(super) fn char_at(&self, at: usize) -> &str {
#[cfg(test)]
Self::count(1);
let tail = &self.text[at..];
tail.chars().next().map_or("", |first| &tail[..first.len_utf8()])
}
#[cfg(feature = "visitor")]
pub(super) fn from(&self, at: usize) -> &str {
#[cfg(test)]
Self::count(self.text.len() - at);
&self.text[at..]
}
#[cfg(test)]
fn count(bytes: usize) {
WORK.with(|work| work.set(work.get() + bytes));
}
#[cfg(all(test, feature = "visitor"))]
pub(super) fn work() -> usize {
WORK.with(std::cell::Cell::get)
}
#[cfg(all(test, feature = "visitor"))]
pub(super) fn whole(&self) -> &str {
&self.text
}
#[cfg(all(test, feature = "visitor"))]
pub(super) const COUNTED: [Counted<Self>; 8] = [
("into_string", |text: &mut Self| {
std::mem::take(text).into_string();
}),
("push_str", |text: &mut Self| text.push_str("a")),
("insert_str", |text: &mut Self| text.insert_str(0, "a")),
("remove", |text: &mut Self| text.remove(0..1)),
("truncate", |text: &mut Self| text.truncate(1)),
("char_before", |text: &mut Self| {
text.char_before(1);
}),
("char_at", |text: &mut Self| {
text.char_at(0);
}),
("from", |text: &mut Self| {
text.from(0);
}),
];
}
#[derive(Default)]
pub(super) struct ControlEnds {
ends: Vec<usize>,
}
impl ControlEnds {
pub(super) const fn len(&self) -> usize {
self.ends.len()
}
pub(super) fn last(&self) -> Option<usize> {
#[cfg(test)]
Self::count(1);
self.ends.last().copied()
}
pub(super) fn push(&mut self, end: usize) {
#[cfg(test)]
Self::count(1);
self.ends.push(end);
}
pub(super) fn get(&self, index: usize) -> usize {
#[cfg(test)]
Self::count(1);
self.ends[index]
}
pub(super) fn first_after(&self, place: usize) -> usize {
let (mut low, mut high) = (0, self.len());
while low < high {
let middle = low + (high - low) / 2;
if self.get(middle) <= place {
low = middle + 1;
} else {
high = middle;
}
}
low
}
pub(super) fn truncate(&mut self, count: usize) {
#[cfg(test)]
Self::count(1);
self.ends.truncate(count);
}
pub(super) fn raise_from(&mut self, first: usize, by: usize) {
#[cfg(test)]
Self::count(self.len() - first);
for end in &mut self.ends[first..] {
*end += by;
}
}
#[cfg(feature = "visitor")]
pub(super) fn lower_from(&mut self, first: usize, by: usize) {
#[cfg(test)]
Self::count(self.len() - first);
for end in &mut self.ends[first..] {
*end -= by;
}
}
#[cfg(test)]
fn count(ends: usize) {
TOUCHED.with(|touched| touched.set(touched.get() + ends));
}
#[cfg(all(test, feature = "visitor"))]
pub(super) fn touched() -> usize {
TOUCHED.with(std::cell::Cell::get)
}
#[cfg(all(test, feature = "visitor"))]
pub(super) fn as_slice(&self) -> &[usize] {
&self.ends
}
#[cfg(all(test, feature = "visitor"))]
pub(super) const COUNTED: [Counted<Self>; 7] = [
("last", |ends: &mut Self| {
ends.last();
}),
("push", |ends: &mut Self| ends.push(9)),
("get", |ends: &mut Self| {
ends.get(0);
}),
("first_after", |ends: &mut Self| {
ends.first_after(0);
}),
("truncate", |ends: &mut Self| ends.truncate(1)),
("raise_from", |ends: &mut Self| ends.raise_from(0, 1)),
("lower_from", |ends: &mut Self| ends.lower_from(0, 1)),
];
}