daily pastebin goal
46%
SHARE
TWEET

Untitled

a guest Aug 21st, 2018 58 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. extern crate unicode_segmentation; // 1.2.1
  2. extern crate unicode_width; // 0.1.5
  3.  
  4. pub fn substr<'a>(s: &'a str, from_index: usize, to_index: usize) -> Option<&'a str>{
  5.     use unicode_segmentation::UnicodeSegmentation;
  6.     use unicode_width::UnicodeWidthStr;
  7.  
  8.     assert!(!(from_index > to_index));
  9.    
  10.     //println!("{}", UnicodeWidthStr::width(s));
  11.  
  12.     let begin = match s.grapheme_indices(true).nth(from_index).map(|(i, _)| i){
  13.         Some(i) => i,
  14.         None => return None,
  15.     };
  16.     if to_index == UnicodeWidthStr::width(s){
  17.         return Some(&s[begin..]);
  18.     }
  19.     else{
  20.         match s.char_indices().nth(to_index).map(|(i, _)| i){
  21.             Some(i) => Some(&s[begin..i]),
  22.             None => return None,
  23.         }
  24.     }
  25. }
  26.  
  27.  
  28. fn main() {
  29.     substr("😀😁 😂 🤣 😃 😄 😅 😆 😉 😊 😋 😎 😍 😘 😗 😙 😚😀 😁 😂 🤣 😃 😄 😅 😆 😉 😊 😋 😎 😍 😘😗 😙 😚", 89, 98).expect("Cant split");
  30. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top