pub struct GString { /* private fields */ }Expand description
String with support for Unicode graphemes
Implementations§
Source§impl GString
impl GString
Sourcepub fn from(s: &str) -> GString
pub fn from(s: &str) -> GString
Create a new GString from a &str
use gstring::*;
const S: &str = "a\u{310}e\u{301}o\u{308}\u{332}";
let s = GString::from(S);
assert_eq!(s, S);Sourcepub fn graphemes(&self) -> &[String]
pub fn graphemes(&self) -> &[String]
Return a slice reference to the internal graphemes
use gstring::*;
let g = GString::from("a\u{310}e\u{301}o\u{308}\u{332}")
.graphemes()
.to_vec();
assert_eq!(g.len(), 3);
assert_eq!(g, ["a\u{310}", "e\u{301}", "o\u{308}\u{332}"]);Sourcepub fn into_graphemes(self) -> Vec<String>
pub fn into_graphemes(self) -> Vec<String>
Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Return the count of graphemes
use gstring::*;
const S: &str = "a\u{310}e\u{301}o\u{308}\u{332}";
let s = GString::from(S);
assert_eq!(s.len(), 3);
let s = GString::from("");
assert_eq!(s.len(), 0);pub fn insert(&mut self, idx: usize, string: &str)
Sourcepub fn remove(&mut self, idx: usize) -> GString
pub fn remove(&mut self, idx: usize) -> GString
Remove a grapheme at an index
use gstring::*;
let mut s = GString::from("a\u{310}e\u{301}o\u{308}\u{332}");
assert_eq!(s.remove(1), "e\u{301}");
assert_eq!(s, "a\u{310}o\u{308}\u{332}");Sourcepub fn push(&mut self, string: &str)
pub fn push(&mut self, string: &str)
Append a string
use gstring::*;
let mut s = GString::from("a\u{310}e\u{301}");
s.push("o\u{308}\u{332}");
assert_eq!(s, "a\u{310}e\u{301}o\u{308}\u{332}");Sourcepub fn pop(&mut self) -> Option<GString>
pub fn pop(&mut self) -> Option<GString>
Remove the last grapheme
use gstring::*;
let mut s = GString::from("a\u{310}e\u{301}o\u{308}\u{332}");
assert_eq!(s.pop().unwrap(), "o\u{308}\u{332}");
assert_eq!(s, "a\u{310}e\u{301}");Sourcepub fn splice<R: RangeBounds<usize>>(
&mut self,
range: R,
replace_with: &str,
) -> GString
pub fn splice<R: RangeBounds<usize>>( &mut self, range: R, replace_with: &str, ) -> GString
Replace a range with the given string
use gstring::*;
let mut s = GString::from("a\u{310}e\u{301}o\u{308}\u{332}");
s.splice(0..2, "e\u{301}a\u{310}");
assert_eq!(s, "e\u{301}a\u{310}o\u{308}\u{332}");
s.splice(1.., "o\u{308}\u{332}a\u{310}");
assert_eq!(s, "e\u{301}o\u{308}\u{332}a\u{310}");
s.splice(..1, "");
assert_eq!(s, "o\u{308}\u{332}a\u{310}");
s.splice(.., "");
assert_eq!(s, "");Sourcepub fn drain<R: RangeBounds<usize>>(&mut self, range: R) -> GString
pub fn drain<R: RangeBounds<usize>>(&mut self, range: R) -> GString
Delete and return a range
use gstring::*;
let mut s = GString::from("a\u{310}e\u{301}o\u{308}\u{332}a\u{310}e\u{301}");
assert_eq!(s.drain(0..2), "a\u{310}e\u{301}");
assert_eq!(s, "o\u{308}\u{332}a\u{310}e\u{301}");
assert_eq!(s.drain(2..), "e\u{301}");
assert_eq!(s, "o\u{308}\u{332}a\u{310}");
assert_eq!(s.drain(..1), "o\u{308}\u{332}");
assert_eq!(s, "a\u{310}");
assert_eq!(s.drain(..), "a\u{310}");
assert_eq!(s, "");Sourcepub fn slice(&self, range: Range<usize>) -> GString
pub fn slice(&self, range: Range<usize>) -> GString
Create a new GString from a given range
use gstring::*;
const S: &str = "a\u{310}e\u{301}o\u{308}\u{332}";
let s = GString::from(S);
assert_eq!(s.slice(0..1), "a\u{310}");
assert_eq!(s.slice(1..2), "e\u{301}");
assert_eq!(s.slice(2..3), "o\u{308}\u{332}");
assert_eq!(s.slice(0..2), "a\u{310}e\u{301}");
assert_eq!(s.slice(1..3), "e\u{301}o\u{308}\u{332}");
assert_eq!(s.slice(0..3), S);
assert_eq!(s, S);Sourcepub fn iter(&self) -> GStringRefIter<'_> ⓘ
pub fn iter(&self) -> GStringRefIter<'_> ⓘ
Create a GStringRefIter for iterating graphemes by reference
use gstring::*;
let s = GString::from("a\u{310}e\u{301}o\u{308}\u{332}");
let mut i = s.iter();
assert_eq!(i.next().unwrap(), "a\u{310}");
assert_eq!(i.next().unwrap(), "e\u{301}");
assert_eq!(i.next().unwrap(), "o\u{308}\u{332}");
assert_eq!(i.next(), None);Sourcepub fn into_iter(self) -> GStringIter ⓘ
pub fn into_iter(self) -> GStringIter ⓘ
Consume the GString and convert into a GStringIter for iterating graphemes
use gstring::*;
let mut i = GString::from("a\u{310}e\u{301}o\u{308}\u{332}").into_iter();
assert_eq!(i.next().unwrap(), "a\u{310}");
assert_eq!(i.next().unwrap(), "e\u{301}");
assert_eq!(i.next().unwrap(), "o\u{308}\u{332}");
assert_eq!(i.next(), None);Trait Implementations§
Source§impl<I> Index<I> for GStringwhere
I: SliceIndex<[String]>,
impl<I> Index<I> for GStringwhere
I: SliceIndex<[String]>,
Source§fn index(&self, index: I) -> &Self::Output
fn index(&self, index: I) -> &Self::Output
use gstring::*;
let s = GString::from("a\u{310}e\u{301}o\u{308}\u{332}");
const GRAPHEMES: &[&str] = &["a\u{310}", "e\u{301}", "o\u{308}\u{332}"];
assert_eq!(&s[0], GRAPHEMES[0]);
assert_eq!(&s[1], GRAPHEMES[1]);
assert_eq!(&s[2], GRAPHEMES[2]);
for start in 0..3 {
for stop in 1..4 {
if stop > start {
assert_eq!(&s[start..stop], GRAPHEMES[start..stop].to_vec());
assert_eq!(&s[..stop], GRAPHEMES[..stop].to_vec());
}
}
assert_eq!(&s[start..], GRAPHEMES[start..].to_vec());
}
assert_eq!(&s[..], GRAPHEMES);Source§impl PartialEq<&str> for GString
impl PartialEq<&str> for GString
Source§impl PartialEq<GString> for &GString
impl PartialEq<GString> for &GString
Source§impl PartialEq<String> for GString
impl PartialEq<String> for GString
Auto Trait Implementations§
impl Freeze for GString
impl RefUnwindSafe for GString
impl Send for GString
impl Sync for GString
impl Unpin for GString
impl UnwindSafe for GString
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
Mutably borrows from an owned value. Read more