Guest User

Untitled

a guest
Aug 21st, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.95 KB | None | 0 0
  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. }
Add Comment
Please, Sign In to add comment