pub struct SmtChar(/* private fields */);Expand description
A Unicode character used in SMT-LIB strings.
In the SMT-LIB string theory, a character is any Unicode code point in the inclusive range
0x0000 to 0x2FFFF.
SmtChar is a wrapper around a u32 and provides convenient methods to construct, inspect,
and manipulate SMT-LIB characters safely.
§Examples
use smt_str::SmtChar;
// Create a new `SmtChar` from a `u32` representing a Unicode code point.
let a = SmtChar::new(0x61); // 'a'
// Get the `u32` representation of the `SmtChar`.
assert_eq!(a.as_u32(), 0x61);
// Get the `char` representation of the `SmtChar`.
assert_eq!(a.as_char(), Some('a'));
// It is also possible to create an `SmtChar` from a `char`.
let b = SmtChar::from('b');
assert_eq!(b.as_u32(), 0x62);
assert_eq!(b.as_char(), Some('b'));
let surrogate = SmtChar::new(0xD800); // valid in SMT-LIB, invalid as Rust `char`
assert_eq!(surrogate.as_char(), None);
// Non-printable characters are escaped when displayed.
let newline = SmtChar::new(0x0A); // '\n'
assert_eq!(newline.to_string(), r#"\u{A}"#);Implementations§
Source§impl SmtChar
impl SmtChar
Sourcepub fn new(c: u32) -> Self
pub fn new(c: u32) -> Self
Creates a new SmtChar from a u32 code point.
Panics if c as u32 > 0x2FFFF.
Sourcepub fn as_char(self) -> Option<char>
pub fn as_char(self) -> Option<char>
Get the char representation of this SmtChar, if it can be represented as a char.
Returns None if this SmtChar is a surrogate code point.
§Examples
use smt_str::SmtChar;
let c = SmtChar::from('a');
assert_eq!(c.as_char(), Some('a'));
// This is a surrogate code point and cannot be represented as a `char`.
assert_eq!(SmtChar::new(55296).as_char(), None);Sourcepub fn as_u32(self) -> u32
pub fn as_u32(self) -> u32
Get the u32 representation of this SmtChar.
The u32 is the unicode code point of this SmtChar.
§Examples
use smt_str::SmtChar;
assert_eq!(SmtChar::from('a').as_u32(), 97);
assert_eq!(SmtChar::from('🦀').as_u32(), 129408);Sourcepub fn next(self) -> Self
pub fn next(self) -> Self
Returns the next SmtChar in the range 0x0000 to 0x2FFFF.
Panics if this SmtChar is the maximum SmtChar.
§Examples
use smt_str::SmtChar;
let c = SmtChar::from('a');
assert_eq!(c.next(), SmtChar::from('b'));Cannot get the next character after the maximum SmtChar:
use smt_str::SmtChar;
let c = SmtChar::MAX;
let _ = c.next(); // panicsSourcepub fn try_next(self) -> Option<Self>
pub fn try_next(self) -> Option<Self>
Returns the next SmtChar in the range 0x0000 to 0x2FFFF, if it exists.
Returns None if this SmtChar is the maximum SmtChar.
§Examples
use smt_str::SmtChar;
let c = SmtChar::from('a');
assert_eq!(c.try_next(), Some(SmtChar::from('b')));
assert_eq!(SmtChar::MAX.try_next(), None);Sourcepub fn saturating_next(self) -> Self
pub fn saturating_next(self) -> Self
Like next, but instead of panicking when this SmtChar is the maximum SmtChar, it returns the maximum SmtChar.
§Examples
use smt_str::SmtChar;
let c = SmtChar::from('a');
assert_eq!(c.saturating_next(), SmtChar::from('b'));
assert_eq!(SmtChar::MAX.saturating_next(), SmtChar::MAX);Sourcepub fn prev(self) -> Self
pub fn prev(self) -> Self
Returns the previous SmtChar in the range 0x0000 to 0x2FFFF.
Panics if this SmtChar is the minimum SmtChar.
§Examples
use smt_str::SmtChar;
let c = SmtChar::from('b');
assert_eq!(c.prev(), SmtChar::from('a'));Cannot get the previous character before the minimum SmtChar:
use smt_str::SmtChar;
let c = SmtChar::MIN;
let _ = c.prev(); // panicsSourcepub fn try_prev(self) -> Option<Self>
pub fn try_prev(self) -> Option<Self>
Returns the previous SmtChar in the range 0x0000 to 0x2FFFF, if it exists.
Returns None if this SmtChar is the minimum SmtChar.
§Examples
use smt_str::SmtChar;
let c = SmtChar::from('b');
assert_eq!(c.try_prev(), Some(SmtChar::from('a')));
assert_eq!(SmtChar::MIN.try_prev(), None);Sourcepub fn saturating_prev(self) -> Self
pub fn saturating_prev(self) -> Self
Like prev, but instead of panicking when this SmtChar is the minimum SmtChar, it returns the minimum SmtChar.
§Examples
use smt_str::SmtChar;
let c = SmtChar::from('b');
assert_eq!(c.saturating_prev(), SmtChar::from('a'));
assert_eq!(SmtChar::MIN.saturating_prev(), SmtChar::MIN);Sourcepub fn printable(self) -> bool
pub fn printable(self) -> bool
Returns true if this SmtChar is a printable ASCII character.
Printable ASCII characters are in the range 0x00020 to 0x0007E.
§Examples
use smt_str::SmtChar;
assert!(SmtChar::from('a').printable());
assert!(!SmtChar::from('\n').printable());Sourcepub fn escape(self) -> String
pub fn escape(self) -> String
Escape this SmtChar as a Unicode escape sequence.
The escape sequence is of the form \u{X} where X is the hexadecimal representation of the unicode code point.
The function always chooses the shortest escape sequence, i.e., it uses the smallest number of digits and does not pad with zeros.
§Examples
use smt_str::SmtChar;
assert_eq!(SmtChar::from('a').escape(), r#"\u{61}"#);
assert_eq!(SmtChar::from('\n').escape(), r#"\u{A}"#);
assert_eq!(SmtChar::from('🦀').escape(), r#"\u{1F980}"#);
assert_eq!(SmtChar::MAX.escape(), r#"\u{2FFFF}"#);
assert_eq!(SmtChar::MIN.escape(), r#"\u{0}"#);
Sourcepub fn unescape(escaped: &str) -> Option<Self>
pub fn unescape(escaped: &str) -> Option<Self>
Unescape a string that contains escaped characters. Escaped characters are of the following form:
\uDDDD\u{D},\u{DD},\u{DDD},\u{DDDD},\u{DDDDD}
where D is a hexadecimal digit. In the case \u{DDDDD}, the first digit must be in the range 0 to 2.
The function returns None if the input string is not a valid escaped character.
§Examples
use smt_str::SmtChar;
assert_eq!(SmtChar::unescape(r#"\u{61}"#), Some(SmtChar::from('a')));
assert_eq!(SmtChar::unescape(r#"\u{A}"#), Some(SmtChar::from('\n')));
assert_eq!(SmtChar::unescape(r#"\u{1F980}"#), Some(SmtChar::from('🦀')));
assert_eq!(SmtChar::unescape(r#"\u{2FFFF}"#), Some(SmtChar::MAX));
assert_eq!(SmtChar::unescape(r#"\u{0}"#), Some(SmtChar::MIN));
// Invalid escape sequences
assert_eq!(SmtChar::unescape(r#"\u{3000A}"#), None); // out of range
assert_eq!(SmtChar::unescape(r#"\u{61"#), None); // missing closing brace
assert_eq!(SmtChar::unescape(r#"\u{}"#), None); // empty digitsTrait Implementations§
Source§impl Add for SmtChar
impl Add for SmtChar
Source§fn add(self, rhs: SmtChar) -> Self::Output
fn add(self, rhs: SmtChar) -> Self::Output
Adds another SmtChar to this SmtChar, shifting the unicode code point.
The sum is the sum of the unicode code points of the two SmtChars.
Panics if the resulting code point is greater than SMT_MAX_CODEPOINT (= 0x2FFFF).
§Examples
use smt_str::SmtChar;
let c = SmtChar::from('a');
assert_eq!(c + SmtChar::new(1), SmtChar::from('b'));
assert_eq!(c + SmtChar::new(25), SmtChar::from('z'));Overflowing the maximum code point panics:
use smt_str::SmtChar;
let c = SmtChar::MAX;
let _ = c + SmtChar::new(1); // panicsSource§impl FromIterator<SmtChar> for Alphabet
impl FromIterator<SmtChar> for Alphabet
Source§impl FromIterator<SmtChar> for SmtString
impl FromIterator<SmtChar> for SmtString
Source§impl Ord for SmtChar
impl Ord for SmtChar
Source§impl PartialOrd for SmtChar
impl PartialOrd for SmtChar
Source§impl SaturatingAdd for SmtChar
impl SaturatingAdd for SmtChar
Source§fn saturating_add(&self, v: &Self) -> Self
fn saturating_add(&self, v: &Self) -> Self
Adds another SmtChar to this SmtChar, saturating at the maximum unicode code point.
§Examples
use smt_str::SmtChar;
use num_traits::ops::saturating::SaturatingAdd;
let c = SmtChar::from('a');
assert_eq!(c.saturating_add(&SmtChar::new(1)), SmtChar::from('b'));
assert_eq!(c.saturating_add(&SmtChar::new(25)), SmtChar::from('z'));
let c = SmtChar::MAX;
assert_eq!(c.saturating_add(&SmtChar::new(1)), SmtChar::MAX);Source§impl SaturatingSub for SmtChar
impl SaturatingSub for SmtChar
Source§fn saturating_sub(&self, v: &Self) -> Self
fn saturating_sub(&self, v: &Self) -> Self
Subtracts another SmtChar from this SmtChar, saturating at the minimum unicode code point.
§Examples
use smt_str::SmtChar;
let c = SmtChar::from('z');
use num_traits::ops::saturating::SaturatingSub;
assert_eq!(c.saturating_sub(&SmtChar::new(1)), SmtChar::from('y'));
assert_eq!(c.saturating_sub(&SmtChar::new(25)), SmtChar::from('a'));
let c = SmtChar::MIN;
assert_eq!(c.saturating_sub(&SmtChar::new(1)), SmtChar::MIN);Source§impl Sub for SmtChar
impl Sub for SmtChar
Source§fn sub(self, rhs: SmtChar) -> Self::Output
fn sub(self, rhs: SmtChar) -> Self::Output
Subtracts another SmtChar from this SmtChar, shifting the unicode code point.
The difference is the difference of the unicode code points of the two SmtChars.
Panics if the resulting code point is less than SMT_MIN_CODEPOINT (= 0).
§Examples
use smt_str::SmtChar;
let c = SmtChar::from('z');
assert_eq!(c - SmtChar::new(1), SmtChar::from('y'));
assert_eq!(c - SmtChar::new(25), SmtChar::from('a'));Underflowing the minimum code point panics:
use smt_str::SmtChar;
let c = SmtChar::MIN;
let _ = c - SmtChar::new(1); // panicsimpl Copy for SmtChar
impl Eq for SmtChar
impl StructuralPartialEq for SmtChar
Auto Trait Implementations§
impl Freeze for SmtChar
impl RefUnwindSafe for SmtChar
impl Send for SmtChar
impl Sync for SmtChar
impl Unpin for SmtChar
impl UnwindSafe for SmtChar
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> Comparable<K> for Q
impl<Q, K> Comparable<K> for Q
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