Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- extern crate unicode_segmentation; // 1.2.1
- extern crate unicode_width; // 0.1.5
- pub fn substr<'a>(s: &'a str, from_index: usize, to_index: usize) -> Option<&'a str>{
- use unicode_segmentation::UnicodeSegmentation;
- use unicode_width::UnicodeWidthStr;
- assert!(!(from_index > to_index));
- //println!("{}", UnicodeWidthStr::width(s));
- let begin = match s.grapheme_indices(true).nth(from_index).map(|(i, _)| i){
- Some(i) => i,
- None => return None,
- };
- if to_index == UnicodeWidthStr::width(s){
- return Some(&s[begin..]);
- }
- else{
- match s.char_indices().nth(to_index).map(|(i, _)| i){
- Some(i) => Some(&s[begin..i]),
- None => return None,
- }
- }
- }
- fn main() {
- substr("๐๐ ๐ ๐คฃ ๐ ๐ ๐ ๐ ๐ ๐ ๐ ๐ ๐ ๐ ๐ ๐ ๐๐ ๐ ๐ ๐คฃ ๐ ๐ ๐ ๐ ๐ ๐ ๐ ๐ ๐ ๐๐ ๐ ๐", 89, 98).expect("Cant split");
- }
Add Comment
Please, Sign In to add comment