pub struct SmtString(/* private fields */);Expand description
An SMT-LIB string is a sequence of characters with unicode code points in the range 0x0000 to 0x2FFFF (first two planes of Unicode).
The characters are represented by the SmtChar type.
ยงExamples
use smt_str::{SmtString, SmtChar};
// Create a new SmtString from a string literal
let s: SmtString = "foo".into();
// Obtain the length of the string
assert_eq!(s.len(), 3);
// SmtStrings have the correct length even for multi-byte characters
let s: SmtString = "๐ฆ".into();
assert_eq!(s.len(), 1);
// In Rust, the length of a string is counted in bytes, not characters
assert_eq!("๐ฆ".len(), 4);
assert_eq!("๐ฆ".chars().count(), 1);
// SmtString can be parsed from a string with escape sequences
let s: SmtString = SmtString::parse(r#"foo\u{61}bar"#);
assert_eq!(s, SmtString::from("fooabar"));
// Printing the string escapes non-printable characters
let s: SmtString = SmtString::from("foo\nbar");
assert_eq!(s.to_string(), r#"foo\u{A}bar"#);
Implementationsยง
Sourceยงimpl SmtString
impl SmtString
Sourcepub fn parse(input: &str) -> Self
pub fn parse(input: &str) -> Self
Parse a string into an SmtString.
The input string can contain escaped characters. The only valid escape sequences are:
\uDDDD\u{D},\u{DD},\u{DDD},\u{DDDD},\u{DDDDD}
where D is a hexadecimal digit such that the resulting code point is in the range 0x0000 to 0x2FFFF.
If the string contains valid escape sequences, they are replaced with the corresponding SmtChar.
If the string contains invalid escape sequences, they are treated as literals.
For example, the string "foo\u{61}bar" is parsed as the string "fooabar".
But the string "foo\u{61bar" is parsed as the string "foo\u{61bar".
The holds for syntactically valid escape sequences that result in code points outside the valid range.
For example, the string "foo\u{3000A}bar" is parsed as the string "foo\u{3000A}bar", since the code point 0x3000A is outside the valid range.
ยงExamples
use smt_str::{SmtString};
let s: SmtString = SmtString::parse(r#"foo\u{61}bar"#);
assert_eq!(s, SmtString::from("fooabar"));
// Invalid escape sequence, treated as literal
let s: SmtString = SmtString::parse(r#"foo\u{61bar"#);
assert_eq!(s, SmtString::from(r#"foo\u{61bar"#));Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Returns whether this string is empty.
ยงExamples
use smt_str::{SmtString};
assert!(SmtString::empty().is_empty());
let s: SmtString = "foo".into();
assert!(!s.is_empty());Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns the length of this string.
ยงExamples
use smt_str::{SmtString};
assert_eq!(SmtString::empty().len(), 0);
let s: SmtString = "foo".into();
assert_eq!(s.len(), 3);Sourcepub fn clear(&mut self)
pub fn clear(&mut self)
Empties this string, removing all characters. After calling this method, the string will be empty.
ยงExamples
use smt_str::{SmtString};
let mut s: SmtString = "foo".into();
s.clear();
assert!(s.is_empty());Sourcepub fn append(&mut self, other: &SmtString)
pub fn append(&mut self, other: &SmtString)
Appends the characters of other to this string.
The characters are appended in order.
ยงExamples
use smt_str::{SmtString, SmtChar};
let mut s: SmtString = "foo".into();
let other: SmtString = "bar".into();
s.append(&other);
let mut iter = s.iter();
assert_eq!(iter.next(), Some(&SmtChar::from('f')));
assert_eq!(iter.next(), Some(&SmtChar::from('o')));
assert_eq!(iter.next(), Some(&SmtChar::from('o')));
assert_eq!(iter.next(), Some(&SmtChar::from('b')));
assert_eq!(iter.next(), Some(&SmtChar::from('a')));
assert_eq!(iter.next(), Some(&SmtChar::from('r')));
assert_eq!(iter.next(), None);Sourcepub fn push(&mut self, c: impl Into<SmtChar>)
pub fn push(&mut self, c: impl Into<SmtChar>)
Pushes a character to the end of this string.
ยงExamples
use smt_str::{SmtString, SmtChar};
let mut s = SmtString::empty();
s.push(SmtChar::from('f'));
s.push(SmtChar::from('o'));
s.push(SmtChar::from('o'));
assert_eq!(s, SmtString::from("foo")); Sourcepub fn concat(&self, other: &SmtString) -> SmtString
pub fn concat(&self, other: &SmtString) -> SmtString
Concatenates this string with other and returns the result.
This is a convenience method that does not modify this string.
The characters of other are appended to the characters of this string, see [append].
ยงExamples
use smt_str::{SmtString};
let s1 = SmtString::from("foo");
let s2 = SmtString::from("bar");
let s3 = s1.concat(&s2);
assert_eq!(s3, SmtString::from("foobar"));Sourcepub fn contains_char(&self, c: impl Into<SmtChar>) -> bool
pub fn contains_char(&self, c: impl Into<SmtChar>) -> bool
Checks if this string contains a character.
ยงExamples
use smt_str::{SmtString, SmtChar};
let s: SmtString = "foobar".into();
assert!(s.contains_char('f'));
assert!(s.contains_char('o'));
assert!(s.contains_char('b'));
assert!(!s.contains_char('z'));Sourcepub fn contains(&self, factor: &SmtString) -> bool
pub fn contains(&self, factor: &SmtString) -> bool
Return whether this string contains another string as a factor. This is a naive implementation that checks all possible factors of this string, leading to O(n^2) complexity.
ยงExamples
use smt_str::{SmtString};
let s: SmtString = "foobar".into();
assert!(s.contains(&SmtString::empty()));
assert!(s.contains(&SmtString::from("foo")));
assert!(s.contains(&SmtString::from("bar")));
assert!(s.contains(&SmtString::from("oba")));
assert!(!s.contains(&SmtString::from("baz")));
// The empty string contains only has the empty string as a factor
let empty: SmtString = SmtString::empty();
assert!(empty.contains(&SmtString::empty()));
assert!(!empty.contains(&SmtString::from("foo")));Sourcepub fn index_of(&self, factor: &SmtString, start: usize) -> Option<usize>
pub fn index_of(&self, factor: &SmtString, start: usize) -> Option<usize>
Find the index of the first occurrence of a factor in the suffix of this string starting at start.
Returns None if the factor is not found.
The empty string is a factor of every string and will always return Some(0).
ยงExamples
use smt_str::{SmtString};
let s: SmtString = "foobar".into();
assert_eq!(s.index_of(&SmtString::empty(),0), Some(0));
assert_eq!(s.index_of(&SmtString::from("foo"),0), Some(0));
assert_eq!(s.index_of(&SmtString::from("foo"),1), None);
assert_eq!(s.index_of(&SmtString::from("bar"),0), Some(3));
assert_eq!(s.index_of(&SmtString::from("oba"),0), Some(2));
assert_eq!(s.index_of(&SmtString::from("baz"),0), None);
// If the string is empty, the only factor is the empty string
let empty: SmtString = SmtString::empty();
assert_eq!(empty.index_of(&SmtString::empty(),0), Some(0));
assert_eq!(empty.index_of(&SmtString::from("foo"),0), None);Sourcepub fn starts_with(&self, prefix: &SmtString) -> bool
pub fn starts_with(&self, prefix: &SmtString) -> bool
Returns whether this string starts with a prefix. The empty string is a prefix of every string.
ยงExamples
use smt_str::{SmtString};
let s: SmtString = "foobar".into();
assert!(s.starts_with(&SmtString::empty()));
assert!(s.starts_with(&SmtString::from("foo")));
assert!(!s.starts_with(&SmtString::from("bar")));Sourcepub fn ends_with(&self, suffix: &SmtString) -> bool
pub fn ends_with(&self, suffix: &SmtString) -> bool
Returns whether this string ends with a suffix. The empty string is a suffix of every string.
ยงExamples
use smt_str::{SmtString};
let s: SmtString = "foobar".into();
assert!(s.ends_with(&SmtString::empty()));
assert!(s.ends_with(&SmtString::from("bar")));
assert!(!s.ends_with(&SmtString::from("foo")));Sourcepub fn first(&self) -> Option<SmtChar>
pub fn first(&self) -> Option<SmtChar>
Returns the first character of this string, if it is not empty.
Returns None if this string is empty.
ยงExamples
use smt_str::{SmtString, SmtChar};
let s: SmtString = "foo".into();
assert_eq!(s.first(), Some('f'.into()));
assert_eq!(SmtString::empty().first(), None);Sourcepub fn last(&self) -> Option<SmtChar>
pub fn last(&self) -> Option<SmtChar>
Returns the last character of this string, if it is not empty.
Returns None if this string is empty.
ยงExamples
use smt_str::{SmtString, SmtChar};
let s: SmtString = "foo".into();
assert_eq!(s.last(), Some('o'.into()));
assert_eq!(SmtString::empty().last(), None);Sourcepub fn take(&self, n: usize) -> SmtString
pub fn take(&self, n: usize) -> SmtString
Returns the prefix of length n of this string.
If n is greater than the length of this string, the entire string is returned.
If n is zero, the empty string is returned.
ยงExamples
use smt_str::{SmtString, SmtChar};
let s: SmtString = "foo".into();
assert_eq!( s.take(2), SmtString::from("fo"));
assert!(s.take(10) == s);
assert!(s.take(0) == SmtString::empty());Sourcepub fn drop(&self, n: usize) -> SmtString
pub fn drop(&self, n: usize) -> SmtString
Returns the suffix of this string after removing the first n characters.
If n is greater than the length of this string, the empty string is returned.
If n is zero, the entire string is returned.
ยงExamples
use smt_str::{SmtString, SmtChar};
let s = SmtString::from("foo");
assert_eq!(s.drop(2), SmtString::from("o"));
assert_eq!(s.drop(10), SmtString::empty());
assert_eq!(s.drop(0), s);Sourcepub fn nth(&self, n: usize) -> Option<SmtChar>
pub fn nth(&self, n: usize) -> Option<SmtChar>
Returns the n-th character of this string.
Returns None if n is greater than or equal to the length of this string.
ยงExamples
use smt_str::{SmtString, SmtChar};
let s: SmtString = "foo".into();
assert_eq!(s.nth(0), Some(SmtChar::from('f')));
assert_eq!(s.nth(1), Some(SmtChar::from('o')));
assert_eq!(s.nth(2), Some(SmtChar::from('o')));
assert_eq!(s.nth(3), None);Sourcepub fn reversed(&self) -> Self
pub fn reversed(&self) -> Self
Returns the reverse of this string.
ยงExamples
use smt_str::{SmtString, SmtChar};
let s: SmtString = "foo".into();
let rev = s.reversed();
let mut iter = rev.iter();
assert_eq!(iter.next(), Some(&SmtChar::from('o')));
assert_eq!(iter.next(), Some(&SmtChar::from('o')));
assert_eq!(iter.next(), Some(&SmtChar::from('f')));
assert_eq!(iter.next(), None);Sourcepub fn repeat(&self, n: usize) -> Self
pub fn repeat(&self, n: usize) -> Self
Repeat this string n times.
If n is zero, the empty string is returned.
If this string is empty, the empty string is returned.
If n is one, this string is returned.
Otherwise, the string is repeated n times.
ยงExamples
use smt_str::{SmtString, SmtChar};
let s = SmtString::from("foo");
assert_eq!(s.repeat(0), SmtString::empty());
assert_eq!(s.repeat(1), s);
assert_eq!(s.repeat(2), SmtString::from("foofoo"));Sourcepub fn replace(&self, from: &SmtString, to: &SmtString) -> SmtString
pub fn replace(&self, from: &SmtString, to: &SmtString) -> SmtString
Replaces the first occurrence of from in this string with to.
If from is not found in this string, the string is returned unchanged.
ยงExamples
use smt_str::{SmtString};
let s: SmtString = "barbar".into();
let from: SmtString = "bar".into();
let to: SmtString = "foo".into();
assert_eq!(s.replace(&from, &to), SmtString::from("foobar"));Sourcepub fn replace_all(&self, from: &SmtString, to: &SmtString) -> SmtString
pub fn replace_all(&self, from: &SmtString, to: &SmtString) -> SmtString
Replaces all occurrences of from in this string with to.
If from is not found in this string, the string is returned unchanged.
If from is the empty string, the string is returned unchanged.
ยงExamples
use smt_str::{SmtString};
let s: SmtString = "barbar".into();
let from: SmtString = "bar".into();
let to: SmtString = "foo".into();
assert_eq!(s.replace_all(&from, &to), SmtString::from("foofoo"));Trait Implementationsยง
Sourceยงimpl FromIterator<SmtChar> for SmtString
impl FromIterator<SmtChar> for SmtString
Sourceยงimpl FromIterator<SmtString> for SmtString
impl FromIterator<SmtString> for SmtString
impl Eq for SmtString
impl StructuralPartialEq for SmtString
Auto Trait Implementationsยง
impl Freeze for SmtString
impl RefUnwindSafe for SmtString
impl Send for SmtString
impl Sync for SmtString
impl Unpin for SmtString
impl UnwindSafe for SmtString
Blanket Implementationsยง
Sourceยงimpl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Sourceยงfn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Sourceยงimpl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Sourceยงimpl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Sourceยงimpl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Sourceยงfn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.Sourceยงimpl<T> IntoEither for T
impl<T> IntoEither for T
Sourceยงfn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSourceยงfn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more