Expand description
Extended Grapheme Cluster Utils
Handy Grapheme Helper Utils
use grapheme_utils::*;
fn main() {
let st = "हिन्दीH🧑🌾e‘︀o‘︁réé".to_string();
println!("num_graphemes {}", num_graphemes(&st)); // Prints 12, the string has 12 grapheme clusters total
println!("string_width {}", string_width(&st)); // Prints 18, the string uses 18 columns
println!("nth_grapheme_idx {}", nth_grapheme_idx(&st, 1)); // Prints 6 (index 6)
println!("nth_grapheme_idx {}", nth_grapheme_idx(&st, 2)); // Prints 18 (index 18)
println!("nth_grapheme {}", nth_grapheme(&st, 1)); // Prints न्दी, the 2nd byte in the string, base 0
println!("nth_grapheme {}", nth_grapheme(&st, 2)); // Prints H, the 3rd bytes in the string, base 0
println!("nth grapheme_width {}", nth_grapheme_width(&st, 1)); // Prints 3
println!("nth grapheme_width {}", nth_grapheme_width(&st, 2)); // Prints 1
// Grapheme Visual Column Width
println!("grapheme_width_at_idx {}", grapheme_width_at_idx(&st, 18)); // Prints 1 (H is 1 column wide)
let num = 7; // Anything between 6 and 17 inclusive
println!(
"grapheme_width_at_idx {}",
grapheme_width_at_idx(&st, num)
); // Prints 3 (न्दी is 3 columns wide)
// Grapheme utf8 byte count
println!("grapheme_len {}", grapheme_len(&st, num)); // Prints 12 (न्दी uses 12 utf8 bytes)
println!("grapheme_len {}", grapheme_len(&st, 18)); // Prints 1 (H uses 1 utf8 byte)
// Matrix of grapheme functions:
// [previous, current or next] grapheme given an index
//
// Outputing either the
// [extended grapheme cluster string, or starting string index]
//
// Take special note of the 2 characters before and after the H.
// The characer before न्दी starts at string index 6, H is at 18, and 🧑 is at index 19
//
// Output string index
println!(
"prev_grapheme_idx_from_idx {}",
prev_grapheme_idx_from_idx(&st, 18)
); // Prints 6
println!("grapheme_idx_at_idx {}", grapheme_idx_at_idx(&st, 18)); // Prints 18
println!(
"next_grapheme_idx_from_idx {}",
next_grapheme_idx_from_idx(&st, 18)
); // Prints 19
// Output extended grapheme cluster
println!("prev_grapheme_from_idx {}", prev_grapheme_from_idx(&st, 18)); // Prints न्दी
println!("grapheme_at_idx {}", grapheme_at_idx(&st, 18)); // Prints H
println!("next_grapheme_from_idx {}", next_grapheme_from_idx(&st, 18)); // Prints 🧑
// Note, all of the above matrix of functions work with num, the range of inputs
// instead of requiring the exact start to each grapheme.
// Examining the testing matrix may be instructive
}Functions§
- grapheme_
at_ idx - Return the grapheme at the given string idx
- grapheme_
idx_ at_ idx - Starting idx of Grapheme
- grapheme_
len - Grapheme length in Bytes
- grapheme_
width_ at_ idx - Return the grapheme starting at or after the given byte index in a string.
- next_
grapheme_ from_ idx - Next Grapheme from Current Index
- next_
grapheme_ idx_ from_ idx - Byte Index of the Next Extended Grapheme from Current Index
- nth_
grapheme - nth Grapheme
- nth_
grapheme_ idx - nth Grapheme Index from nth
- nth_
grapheme_ width - nth Grapheme Width
- num_
graphemes - Num Graphemes In &str
- prev_
grapheme_ from_ idx - Previoius Grapheme from current idx
- prev_
grapheme_ idx_ from_ idx - Byte Index of the Previous Extended Grapheme from Current Idx
- string_
width - Return the string_width