#grapheme #string #gstring #g-string #301

gstring

String with support for Unicode graphemes

23 releases (8 breaking)

new 0.9.1 Apr 16, 2025
0.9.0 Mar 5, 2025
0.8.1 Apr 16, 2025
0.8.0 Feb 23, 2025
0.3.1 Dec 14, 2024

#1014 in Text processing

Download history 126/week @ 2025-01-30 61/week @ 2025-02-06 84/week @ 2025-02-13 520/week @ 2025-02-20 144/week @ 2025-02-27 108/week @ 2025-03-06 7/week @ 2025-03-13 1/week @ 2025-03-20 134/week @ 2025-04-10

145 downloads per month
Used in unicode-matching

MIT license

28KB
269 lines

String with support for Unicode graphemes

use gstring::*;

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

// Create a GString
let mut s = GString::from(S);
assert_eq!(s, S);
assert_eq!(s.graphemes(), &["a\u{310}", "e\u{301}", "o\u{308}\u{332}"]);
assert_eq!(s.len(), 3);
assert!(!s.is_empty());
assert_eq!(s.chars(), &['a', '\u{310}', 'e', '\u{301}', 'o', '\u{308}', '\u{332}']);
assert_eq!(s.bytes(), &[0x61, 0xcc, 0x90, 0x65, 0xcc, 0x81, 0x6f, 0xcc, 0x88, 0xcc, 0xb2]);

// Insert a &str
s.insert(0, "i\u{301}u\u{301}");
assert_eq!(s, "i\u{301}u\u{301}a\u{310}e\u{301}o\u{308}\u{332}");

// Remove a grapheme at an index
assert_eq!(s.remove(1), "u\u{301}");
assert_eq!(s, "i\u{301}a\u{310}e\u{301}o\u{308}\u{332}");

// Push a &str
s.push("i\u{301}u\u{301}");
assert_eq!(s, "i\u{301}a\u{310}e\u{301}o\u{308}\u{332}i\u{301}u\u{301}");

// Pop last grapheme
assert_eq!(s.pop(), Some("u\u{301}".gstring()));
assert_eq!(s, "i\u{301}a\u{310}e\u{301}o\u{308}\u{332}i\u{301}");

// Slice
assert_eq!(s.slice(1..4), "a\u{310}e\u{301}o\u{308}\u{332}");

// Splice
s.splice(1..4, "");
assert_eq!(s, "i\u{301}i\u{301}");

// Drain
assert_eq!(s.drain(..), "i\u{301}i\u{301}");
assert_eq!(s, "");

Dependencies

~0.6–1.2MB
~24K SLoC