gstring

Struct GString

Source
pub struct GString { /* private fields */ }
Expand description

String with support for Unicode graphemes

Implementations§

Source§

impl GString

Source

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);
Source

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}"]);
Source

pub fn into_graphemes(self) -> Vec<String>

Consume the GString and convert to a Vec of graphemes

use gstring::*;

let g = GString::from("a\u{310}e\u{301}o\u{308}\u{332}")
    .into_graphemes();

assert_eq!(g.len(), 3);
assert_eq!(g, ["a\u{310}", "e\u{301}", "o\u{308}\u{332}"]);
Source

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);
Source

pub fn is_empty(&self) -> bool

Return true if the GString has zero graphemes otherwise return false

use gstring::*;

let s = GString::from("");

assert!(s.is_empty());
Source

pub fn chars(&self) -> Vec<char>

Return a Vec of chars

use gstring::*;

let c = GString::from("a\u{310}e\u{301}o\u{308}\u{332}").chars();

assert_eq!(c.len(), 7);
assert_eq!(c, ['a', '\u{310}', 'e', '\u{301}', 'o', '\u{308}', '\u{332}']);
Source

pub fn bytes(&self) -> Vec<u8>

Return a Vec of u8s

use gstring::*;

let b = GString::from("a\u{310}e\u{301}o\u{308}\u{332}").bytes();

assert_eq!(b.len(), 11);
assert_eq!(
    b,
    [0x61, 0xcc, 0x90, 0x65, 0xcc, 0x81, 0x6f, 0xcc, 0x88, 0xcc, 0xb2],
);
Source

pub fn insert(&mut self, idx: usize, string: &str)

Source

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}");
Source

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}");
Source

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}");
Source

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, "");
Source

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, "");
Source

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);
Source

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);
Source

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 Clone for GString

Source§

fn clone(&self) -> GString

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for GString

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

use gstring::*;

const S: &str = "a\u{310}e\u{301}o\u{308}\u{332}";

assert_eq!(
    format!("{:?}", GString::from(S)),
    format!("{:?}", S),
);
Source§

impl Display for GString

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

use gstring::*;

const S: &str = "a\u{310}e\u{301}o\u{308}\u{332}";

assert_eq!(format!("{}", GString::from(S)), S);
assert_eq!(GString::from(S).to_string(), S);
Source§

impl<I> Index<I> for GString
where I: SliceIndex<[String]>,

Source§

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§

type Output = <I as SliceIndex<[String]>>::Output

The returned type after indexing.
Source§

impl PartialEq<&str> for GString

Source§

fn eq(&self, other: &&str) -> bool

use gstring::*;

const S: &str = "a\u{310}e\u{301}o\u{308}\u{332}";

assert_eq!(GString::from(S), S);
assert_ne!(GString::from(S), "");
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialEq<GString> for &GString

Source§

fn eq(&self, other: &GString) -> bool

use gstring::*;

const S: &str = "a\u{310}e\u{301}o\u{308}\u{332}";

let s = GString::from(S);

assert_eq!(&s, GString::from(S));
assert_ne!(&s, GString::from(""));
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialEq<String> for GString

Source§

fn eq(&self, other: &String) -> bool

use gstring::*;

const S: &str = "a\u{310}e\u{301}o\u{308}\u{332}";

assert_eq!(GString::from(S), S.to_string());
assert_ne!(GString::from(S), String::new());
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialEq for GString

Source§

fn eq(&self, other: &GString) -> bool

use gstring::*;

const S: &str = "a\u{310}e\u{301}o\u{308}\u{332}";

assert_eq!(GString::from(S), GString::from(S));
assert_ne!(GString::from(S), GString::from(""));
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Serialize for GString

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dst: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.