use lark_debug_with::DebugWith;
use std::hash::Hash;
use std::hash::Hasher;
use std::ops::Range;
use std::sync::Arc;
mod test;
#[derive(Clone)]
pub struct Text {
text: Arc<String>,
start: usize,
end: usize,
}
impl Text {
pub fn select(&mut self, range: Range<usize>) {
let len = range.end - range.start;
let new_start = self.start + range.start;
let new_end = new_start + len;
assert!(new_end <= self.end);
self.start = new_start;
self.end = new_end;
}
pub fn extract(&self, range: Range<usize>) -> Self {
let mut result = self.clone();
result.select(range);
result
}
}
impl From<Arc<String>> for Text {
fn from(text: Arc<String>) -> Self {
let end = text.len();
Self {
text,
start: 0,
end,
}
}
}
impl AsRef<str> for Text {
fn as_ref(&self) -> &str {
&*self
}
}
impl From<String> for Text {
fn from(text: String) -> Self {
Text::from(Arc::new(text))
}
}
impl From<&String> for Text {
fn from(text: &String) -> Self {
Text::from(text.to_string())
}
}
impl From<&str> for Text {
fn from(text: &str) -> Self {
Text::from(text.to_string())
}
}
impl std::borrow::Borrow<str> for Text {
fn borrow(&self) -> &str {
&*self
}
}
impl std::ops::Deref for Text {
type Target = str;
fn deref(&self) -> &str {
&self.text[self.start..self.end]
}
}
impl std::fmt::Display for Text {
fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
<str as std::fmt::Display>::fmt(self, fmt)
}
}
impl std::fmt::Debug for Text {
fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
<str as std::fmt::Debug>::fmt(self, fmt)
}
}
impl DebugWith for Text {
fn fmt_with<Cx: ?Sized>(&self, cx: &Cx, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
<str as DebugWith>::fmt_with(self, cx, fmt)
}
}
impl PartialEq<Text> for Text {
fn eq(&self, other: &Text) -> bool {
let this: &str = self;
let other: &str = other;
this == other
}
}
impl Eq for Text {}
impl PartialEq<str> for Text {
fn eq(&self, other: &str) -> bool {
let this: &str = self;
this == other
}
}
impl PartialEq<String> for Text {
fn eq(&self, other: &String) -> bool {
let this: &str = self;
let other: &str = other;
this == other
}
}
impl PartialEq<Text> for str {
fn eq(&self, other: &Text) -> bool {
other == self
}
}
impl PartialEq<Text> for String {
fn eq(&self, other: &Text) -> bool {
other == self
}
}
impl<T: ?Sized> PartialEq<&T> for Text
where
Text: PartialEq<T>,
{
fn eq(&self, other: &&T) -> bool {
self == *other
}
}
impl Hash for Text {
fn hash<H: Hasher>(&self, state: &mut H) {
<str as Hash>::hash(self, state)
}
}