1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868
//! Rope slices //! //! While we could just have the slicing methods on `Rope`s return `&str`s, //! this would have some serious drawbacks: to slice a rope as `&str`, we'd have //! to collect all the `Rope`'s characters into a `String` and then slice that //! `String`. Creating an intermediate `String` would be slow, would cause //! unnecessary allocations, and, in the case of `Rope::slice_mut()`, would //! return a mutable slice of a _new `String`_ –- mutating the slice would _not_ //! mutate the underlying `Rope`. // TODO: implement Borrow<RopeSlice> for Rope? use std::fmt; use std::cmp; use std::convert; #[cfg(feature = "unstable")] use collections::range::RangeArgument; #[cfg(not(feature = "unstable"))] use std::ops::Range; use super::Rope; use super::internals::Node; /// An immutable borrowed slice of a `Rope`. /// /// A `RopeSlice` represents an immutable borrowed slice of some or all the /// characters in a `Rope`. pub struct RopeSlice<'a> { node: &'a Node , offset: usize , len: usize } impl<'a> fmt::Display for RopeSlice<'a> { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { // TODO: don't create an intermediate string? write!(f, "{}", self.chars().collect::<String>()) } } impl<'a> fmt::Debug for RopeSlice<'a> { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { // TODO: don't create an intermediate string? write!( f, "RopeSlice {{ offset: {}, len {} }} [{:?}]" , self.offset, self.len, self.chars().collect::<String>()) } } // // /// An mutable borrowed slice of a `Rope`. // /// // /// A `RopeSliceMut` represents a mutable borrowed slice of some or all the // /// characters in a `Rope`. // #[derive(Debug)] // pub struct RopeSliceMut<'a> { node: &'a mut Node // , offset: usize // , len: usize // } // // impl<'a> RopeSliceMut<'a> { // // // TODO: share duplicate functionality with non-mutable RopeSlice in a less // // ugly way. Maybe an added generic? // // - eliza, 12/23/16 // // // TODO: add mutable iterators // // #[cfg(feature = "unstable")] // pub fn new<R>(node: &'a mut Node, range: R) -> Self // where R: RangeArgument<usize> { // let len = node.len(); // // // if the RangeArgument doesn't have a defined start index, // // the slice begins at the 0th index. // let start = *range.start().unwrap_or(&0); // // similarly, if there's no defined end, then the end index // // is the last index in the Rope. // let end = *range.end().unwrap_or(&len); // // let slice_len = end - start; // // // find the lowest node that contains both the slice start index and // // the end index // let (node, offset) = if start == 0 && end == len { // // if the slice contains the entire rope, then the spanning node // // is the root node // (node, 0) // } else { // node.spanning_mut(start, slice_len) // }; // // RopeSliceMut { node: node // , offset: offset // , len: slice_len } // } // #[cfg(not(feature = "unstable"))] // pub fn new(node: &'a mut Node, range: Range<usize>) -> Self { // let len = node.len(); // let slice_len = range.end - range.start; // // // find the lowest node that contains both the slice start index and // // the end index // let (node, offset) = if range.start == 0 && range.end == len { // // if the slice contains the entire rope, then the spanning node // // is the root node // (node, 0) // } else { // node.spanning_mut(range.start, slice_len) // }; // // RopeSliceMut { node: node // , offset: offset // , len: slice_len } // } // // #[cfg(feature = "unstable")] // #[inline] // fn slice_char_iter<I, T>(&'a self, i: I) -> impl Iterator<Item=T> + 'a // where I: Iterator<Item=T> // , I: 'a // , T: Copy { // i.skip(self.offset).take(self.len) // } // // #[cfg(feature = "unstable")] // fn slice_strings_iter<I>(&'a self, i: I) -> impl Iterator<Item=&'a str> +'a // where I: Iterator<Item=&'a str> // , I: 'a { // i.scan((self.offset, self.len), |curr, s| { // match *curr { // (0, 0) => None // , (0, ref mut remaining) if *remaining < s.len() => { // let r = *remaining; // *remaining = 0; // Some(&s[..r]) // } // , (0, ref mut remaining) => { // *remaining -= s.len(); // Some(s) // } // , (ref mut offset, _) if *offset > s.len() => { // *offset -= s.len(); // Some("") // } // , (ref mut offset, _) => { // let c = *offset; // *offset -= s.len(); // Some(&s[c..]) // } // } // }) // .skip_while(|&s| s == "") // } // #[cfg(not(feature = "unstable"))] // #[inline] // fn slice_char_iter<I, T>(&'a self, i: I) -> Box<Iterator<Item=T> + 'a> // where I: Iterator<Item=T> // , I: 'a // , T: Copy { // Box::new(i.skip(self.offset) // .take(self.len)) // } // // #[cfg(not(feature = "unstable"))] // fn slice_strings_iter<I>(&'a self, i: I) -> Box<Iterator<Item=&'a str> + 'a> // where I: Iterator<Item=&'a str> // , I: 'a { // Box::new(i.scan((self.offset, self.len), |curr, s| { // match *curr { // (0, 0) => None // , (0, ref mut remaining) if *remaining < s.len() => { // let r = *remaining; // *remaining = 0; // Some(&s[..r]) // } // , (0, ref mut remaining) => { // *remaining -= s.len(); // Some(s) // } // , (ref mut offset, _) if *offset > s.len() => { // *offset -= s.len(); // Some("") // } // , (ref mut offset, _) => { // let c = *offset; // *offset -= s.len(); // Some(&s[c..]) // } // } // }) // .skip_while(|&s| s == "")) // } // // unstable_iters! { // #[inline] // pub fn chars(&'a self) -> impl Iterator<Item=char> + 'a { // self.slice_char_iter(self.node.chars()) // } // #[inline] // pub fn char_indices(&'a self) -> impl Iterator<Item=(usize, char)> + 'a { // self.chars().enumerate() // } // #[inline] // pub fn bytes(&'a self) -> impl Iterator<Item=u8> + 'a { // self.slice_char_iter(self.node.bytes()) // } // #[inline] // pub fn split_whitespace(&'a self) -> impl Iterator<Item=&'a str> + 'a { // self.slice_strings_iter(self.node.split_whitespace()) // } // } // // /// Returns true if the bytes in `self` equal the bytes in `other` // #[inline] // fn bytes_eq<I>(&self, other: I) -> bool // where I: Iterator<Item=u8> { // self.bytes().zip(other).all(|(a, b)| a == b) // } // // /// Returns the length of `self.` // /// // /// This length is in bytes, not chars or graphemes. In other words, it may // /// not be what a human considers the length of the string. // #[inline] // pub fn len(&self) -> usize { self.len } // // /// Returns `true` if this `RopeSliceMut` is empty. // /// // /// # Examples // /// // /// A `RopeSliceMut` into an empty rope should be empty: // /// // /// ``` // /// use an_rope::Rope; // /// let mut an_empty_rope = Rope::new(); // /// assert!(an_empty_rope.slice_mut(0..0).is_empty()); // /// ``` // /// // /// // /// A `RopeSliceMut` that contains no characters should be empty, // /// even fi the sliced `Rope` is not empty: // /// // /// ``` // /// use an_rope::Rope; // /// let mut an_rope = Rope::from("a string that is not empty"); // /// assert!(an_rope.slice_mut(0..0).is_empty()); // /// ``` // /// // /// A `RopeSliceMut` with characters should not be empty: // /// // /// ``` // /// use an_rope::Rope; // /// let mut an_rope = Rope::from("a string"); // /// assert!(!an_rope.slice_mut(0..5).is_empty()); // /// ``` // #[inline] pub fn is_empty(&self) -> bool { self.len() == 0 } // // // // #[inline] // // fn take_node(&mut self) -> Node { // // use std::mem::replace; // // replace(self.node, Node::empty()) // // } // // /// Insert `rope` into `index` in this mutable `RopeSlice`. // /// // /// Note that the index to insert into is relative to the beginning of this // /// _slice_, not to the beginning of the sliced `Rope`. // /// // /// Consumes `rope`. // /// // /// # Panics // /// * If `index` is greater than the length of this `RopeSlice` // /// // /// # Time Complexity // /// O(log _n_) // /// // /// # Examples // /// If built with `--features unstable`: // /// ``` // /// #![feature(collections)] // /// #![feature(collections_range)] // /// ##[cfg(feature = "unstable")] // /// extern crate collections; // /// extern crate an_rope; // /// ##[cfg(feature = "unstable")] // /// # fn main() { // /// // /// use collections::range::RangeArgument; // /// use an_rope::Rope; // /// // /// let mut rope = Rope::from("this is a string"); // /// { // we have to create a new block here so that the mutable borrow // /// // on `Rope` will end // /// let mut slice = rope.slice_mut(8..); // /// slice.insert_rope(1, Rope::from("n example")); // /// } // /// assert_eq!(&rope, "this is an example string"); // /// # } // /// ``` // #[inline] // pub fn insert_rope<M>(&self, index: M, rope: Rope) -> Rope // where M: Metric // , Node: Measured<M> // , BranchNode: Measured<M> // , String: Measured<M> { // // assert!( index <= self.len() // // , "RopeSliceMut::insert_rope: index {} was > length {}" // // , index, self.len()); // if !rope.is_empty() { // // split the rope at the given index // let (left, right) = self.split(index + self.offset); // // // construct the new root node with `Rope` inserted // (left + rope.root + right).rebalance() // } else { // self.clone() // } // } // // /// Insert `ch` into `index` in this mutable `RopeSlice`. // /// // /// Note that the index to insert into is relative to the beginning of this // /// _slice_, not to the beginning of the sliced `Rope`. // /// // /// Consumes `ch`. // /// // /// # Panics // /// * If `index` is greater than the length of this `RopeSlice` // /// // /// # Time Complexity // /// O(log _n_) // /// // /// # Examples // /// If built with `--features unstable`: // /// ``` // /// #![feature(collections)] // /// #![feature(collections_range)] // /// ##[cfg(feature = "unstable")] // /// extern crate collections; // /// extern crate an_rope; // /// ##[cfg(feature = "unstable")] // /// # fn main() { // /// // /// use collections::range::RangeArgument; // /// use an_rope::Rope; // /// // /// let mut rope = Rope::from("this is a string"); // /// { // we have to create a new block here so that the mutable borrow // /// // on `Rope` will end // /// let mut slice = rope.slice_mut(8..); // /// slice.insert(1, 'n'); // /// } // /// assert_eq!(&rope, "this is an string"); // /// # } // /// ``` // pub fn insert(&mut self, index: usize, ch: char) { // assert!( index <= self.len() // , "RopeSliceMut::insert: index {} was > length {}" // , index, self.len()); // // TODO: this is gross... // let mut s = String::new(); // s.push(ch); // self.insert_rope(index, Rope::from(s)) // } // // // /// Insert `s` into `index` in this mutable `RopeSlice`. // /// // /// Note that the index to insert into is relative to the beginning of this // /// _slice_, not to the beginning of the sliced `Rope`. // /// // /// # Panics // /// * If `index` is greater than the length of this `RopeSlice` // /// // /// # Time Complexity // /// O(log _n_) // /// // /// # Examples // /// If built with `--features unstable`: // /// ``` // /// #![feature(collections)] // /// #![feature(collections_range)] // /// ##[cfg(feature = "unstable")] // /// extern crate collections; // /// extern crate an_rope; // /// ##[cfg(feature = "unstable")] // /// # fn main() { // /// // /// use collections::range::RangeArgument; // /// use an_rope::Rope; // /// // /// let mut rope = Rope::from("this is a string"); // /// { // we have to create a new block here so that the mutable borrow // /// // on `Rope` will end // /// let mut slice = rope.slice_mut(8..); // /// slice.insert_str(1, "n example"); // /// } // /// assert_eq!(&rope, "this is an example string"); // /// # } // /// ``` // pub fn insert_str(&mut self, index: usize, s: &str) { // assert!( index <= self.len() // , "RopeSliceMut::insert_str: index {} was > length {}" // , index, self.len()); // self.insert_rope(index, Rope::from(s)) // } // // } impl<'a> RopeSlice<'a> { unstable_iters! { #[inline] pub fn chars(&'a self) -> impl Iterator<Item=char> + 'a { self.slice_char_iter(self.node.chars()) } #[inline] pub fn char_indices(&'a self) -> impl Iterator<Item=(usize, char)> + 'a { self.chars().enumerate() } #[inline] pub fn bytes(&'a self) -> impl Iterator<Item=u8> + 'a { self.slice_char_iter(self.node.bytes()) } #[inline] pub fn split_whitespace(&'a self) -> impl Iterator<Item=&'a str> + 'a { self.slice_strings_iter(self.node.split_whitespace()) } } #[cfg(feature = "unstable")] pub fn new<R>(node: &'a Node, range: R) -> Self where R: RangeArgument<usize> { let len = node.len(); // if the RangeArgument doesn't have a defined start index, // the slice begins at the 0th index. let start = *range.start().unwrap_or(&0); // similarly, if there's no defined end, then the end index // is the last index in the Rope. let end = *range.end().unwrap_or(&node.len()); let slice_len = end - start; // find the lowest node that contains both the slice start index and // the end index let (node, offset) = if start == 0 && end == len { // if the slice contains the entire rope, then the spanning node // is the root node (node, 0) } else { node.spanning(start, slice_len) }; RopeSlice { node: node , offset: offset , len: slice_len } } #[cfg(not(feature = "unstable"))] pub fn new(node: &'a Node, range: Range<usize>) -> Self { let len = node.len(); let slice_len = range.end - range.start; // find the lowest node that contains both the slice start index and // the end index let (node, offset) = if range.start == 0 && range.end == len { // if the slice contains the entire rope, then the spanning node // is the root node (node, 0) } else { node.spanning(range.start, slice_len) }; RopeSlice { node: node , offset: offset , len: slice_len } } #[cfg(feature = "unstable")] #[inline] fn slice_char_iter<I, T>(&'a self, i: I) -> impl Iterator<Item=T> + 'a where I: Iterator<Item=T> , I: 'a , T: Copy { i.skip(self.offset).take(self.len) } #[cfg(feature = "unstable")] fn slice_strings_iter<I>(&'a self, i: I) -> impl Iterator<Item=&'a str> + 'a where I: Iterator<Item=&'a str> , I: 'a { i.scan((self.offset, self.len), |curr, s| { match *curr { (0, 0) => None , (0, ref mut remaining) if *remaining < s.len() => { let r = *remaining; *remaining = 0; Some(&s[..r]) } , (0, ref mut remaining) => { *remaining -= s.len(); Some(s) } , (ref mut offset, _) if *offset > s.len() => { *offset -= s.len(); Some("") } , (ref mut offset, _) => { let c = *offset; *offset -= s.len(); Some(&s[c..]) } } }) .skip_while(|&s| s == "") } #[cfg(not(feature = "unstable"))] #[inline] fn slice_char_iter<I, T>(&'a self, i: I) -> Box<Iterator<Item=T> + 'a> where I: Iterator<Item=T> , I: 'a , T: Copy { Box::new(i.skip(self.offset) .take(self.len)) } #[cfg(not(feature = "unstable"))] fn slice_strings_iter<I>(&'a self, i: I) -> Box<Iterator<Item=&'a str> + 'a> where I: Iterator<Item=&'a str> , I: 'a { Box::new(i.scan((self.offset, self.len), |curr, s| { match *curr { (0, 0) => None , (0, ref mut remaining) if *remaining < s.len() => { let r = *remaining; *remaining = 0; Some(&s[..r]) } , (0, ref mut remaining) => { *remaining -= s.len(); Some(s) } , (ref mut offset, _) if *offset > s.len() => { *offset -= s.len(); Some("") } , (ref mut offset, _) => { let c = *offset; *offset -= s.len(); Some(&s[c..]) } } }) .skip_while(|&s| s == "")) } /// Returns true if the bytes in `self` equal the bytes in `other` #[inline] fn bytes_eq<I>(&self, other: I) -> bool where I: Iterator<Item=u8> { self.bytes().zip(other).all(|(a, b)| a == b) } #[inline] pub fn len(&self) -> usize { self.len } /// Returns `true` if this `RopeSlice` is empty. /// /// # Examples /// /// A `RopeSlice` into an empty rope should be empty: /// /// ``` /// use an_rope::Rope; /// let an_empty_rope = Rope::new(); /// assert!(an_empty_rope.slice(0..0).is_empty()); /// ``` /// /// /// A `RopeSlice` that contains no characters should be empty, /// even fi the sliced `Rope` is not empty: /// /// ``` /// use an_rope::Rope; /// let an_rope = Rope::from("a string that is not empty"); /// assert!(an_rope.slice(0..0).is_empty()); /// ``` /// /// A `RopeSlice` with characters should not be empty: /// /// ``` /// use an_rope::Rope; /// let an_rope = Rope::from("a string"); /// assert!(!an_rope.slice(0..5).is_empty()); /// ``` #[inline] pub fn is_empty(&self) -> bool { self.len() == 0 } } //-- comparisons ---------------------------------------------------- impl<'a> cmp::Eq for RopeSlice<'a> {} impl<'a> cmp::PartialEq for RopeSlice<'a> { /// A rope equals another rope if all the bytes in both are equal. #[inline] fn eq(&self, other: &RopeSlice<'a>) -> bool { if self.len() == other.len() { self.bytes_eq(other.bytes()) } else { false } } } impl<'a> cmp::PartialEq<str> for RopeSlice<'a> { /// A rope equals another rope if all the bytes in both are equal. #[inline] fn eq(&self, other: &str) -> bool { if self.len() == other.len() { self.bytes_eq(other.bytes()) } else { false } } } impl<'a> cmp::PartialEq<&'a str> for RopeSlice<'a> { /// A rope equals another rope if all the bytes in both are equal. #[inline] fn eq(&self, other: &&'a str) -> bool { if self.len() == other.len() { self.bytes_eq((*other).bytes()) } else { false } } } // // impl<'a> cmp::Eq for RopeSliceMut<'a> {} // impl<'a> cmp::PartialEq for RopeSliceMut<'a> { // /// A rope equals another rope if all the bytes in both are equal. // #[inline] // fn eq(&self, other: &RopeSliceMut<'a>) -> bool { // if self.len() == other.len() { // self.bytes_eq(other.bytes()) // } else { // false // } // } // } // // impl<'a, 'b> cmp::PartialEq<RopeSlice<'b>> for RopeSliceMut<'a> { // /// A rope equals another rope if all the bytes in both are equal. // // #[inline] // fn eq(&self, other: &RopeSlice<'b>) -> bool { // if self.len() == other.len() { // self.bytes_eq(other.bytes()) // } else { // false // } // } // } // // impl<'a> cmp::PartialEq<str> for RopeSliceMut<'a> { // /// A rope equals another rope if all the bytes in both are equal. // // #[inline] // fn eq(&self, other: &str) -> bool { // if self.len() == other.len() { // self.bytes_eq(other.bytes()) // } else { // false // } // } // } impl<'a> convert::Into<Rope> for RopeSlice<'a> { /// Converts this `RopeSlice` into a new `Rope` fn into(self) -> Rope { Rope::from(self.chars().collect::<String>()) } } impl<'a> convert::Into<String> for RopeSlice<'a> { /// Converts this `RopeSlice` into a new `String` fn into(self) -> String { self.chars().collect::<String>() } } // // impl<'a> convert::Into<Rope> for RopeSliceMut<'a> { // /// Converts this `RopeSliceMut` into a new `Rope` // fn into(self) -> Rope { // Rope::from(self.chars().collect::<String>()) // } // } // // impl<'a> convert::Into<String> for RopeSliceMut<'a> { // /// Converts this `RopeSliceMut` into a new `String` // fn into(self) -> String { // self.chars().collect::<String>() // } // } #[cfg(test)] mod tests { use super::*; use ::Rope; #[test] fn char_indices() { let string = "aaaaabbbbbbccccccccccccdefgdefgaabababab"; let rope = Rope::from(string); let rope_slice = rope.slice(4..8); let string_slice = &string[4..8]; let indices = rope_slice.char_indices().zip(string_slice.char_indices()); for ((ridx, rch), (sidx, sch)) in indices { assert_eq!(rch, sch); assert_eq!(ridx, sidx); } } #[test] fn to() { let string = "aaaaabbbbbbccccccccccccdefgdefgaabababab"; let rope = Rope::from(string); let rope_slice = rope.slice(1..10); let string_slice = &string[1..10]; assert_eq!(&rope_slice, string_slice) } // #[test] // fn between() { // let string = "aaaaabbbbbbccccccccccccdefgdefgaabababab"; // let rope = Rope::from(string); // let rope_slice = rope.slice(1...10usize); // let string_slice = &string[1...10]; // assert_eq!(&rope_slice, string_slice) // } #[cfg(feature = "unstable")] #[test] fn until() { let string = "aaaaabbbbbbccccccccccdefgdefgaabababab"; let rope = Rope::from(string); let rope_slice = rope.slice(..10); let string_slice = &string[..10]; assert_eq!(&rope_slice, string_slice) } #[cfg(feature = "unstable")] #[test] fn from() { let mut string = "aaaaabbbbbbccccccccccccdefgdefgaabababab"; let mut rope = Rope::from(string); let rope_slice = rope.slice(5..); let string_slice = &string[5..]; assert_eq!(&rope_slice, string_slice) } #[cfg(feature = "unstable")] #[test] fn full() { let string = "aaaaabbbbbbccccccccccccdefgdefgaabababab"; let rope = Rope::from(string); let rope_slice = rope.slice(..); let string_slice = &string[..]; assert_eq!(&rope_slice, string_slice) } // #[test] // fn mut_char_indices() { // let mut string = // String::from("aaaaabbbbbbccccccccccccdefgdefgaabababab"); // let mut rope = Rope::from(string.clone()); // let rope_slice = rope.slice_mut(4..8); // let string_slice = &mut string[4..8]; // let indices = rope_slice.char_indices().zip(string_slice.char_indices()); // for ((ridx, rch), (sidx, sch)) in indices { // assert_eq!(rch, sch); // assert_eq!(ridx, sidx); // } // } // // #[test] // fn mut_to() { // let mut string = // String::from("aaaaabbbbbbccccccccccccdefgdefgaabababab"); // let mut rope = Rope::from(string.clone()); // let rope_slice = rope.slice_mut(1..10); // let string_slice = &mut string[1..10]; // assert_eq!(&rope_slice, string_slice) // } // #[cfg(feature = "unstable")] // #[test] // fn mut_until() { // let mut string = // String::from("aaaaabbbbbbccccccccccccdefgdefgaabababab"); // let mut rope = Rope::from(string.clone()); // let rope_slice = rope.slice_mut(..10); // let string_slice = &mut string[..10]; // assert_eq!(&rope_slice, string_slice) // } // #[cfg(feature = "unstable")] // #[test] // fn mut_from() { // let mut string = // String::from("aaaaabbbbbbccccccccccccdefgdefgaabababab"); // let mut rope = Rope::from(string.clone()); // let rope_slice = rope.slice_mut(5..); // let string_slice = &mut string[5..]; // assert_eq!(&rope_slice, string_slice) // } // // #[cfg(feature = "unstable")] // #[test] // fn mut_full() { // let mut string = // String::from("aaaaabbbbbbccccccccccccdefgdefgaabababab"); // let mut rope = Rope::from(string.clone()); // let rope_slice = rope.slice_mut(..); // let string_slice = &mut string[..]; // assert_eq!(&rope_slice, string_slice) // } // #[cfg(feature = "unstable")] // #[test] // fn mut_insert_rope() { // let mut rope = Rope::from("this is a string"); // { // let slice = rope.slice_mut(8..); // assert_eq!(&slice, "a string"); // } // { // let mut slice = rope.slice_mut(8..); // slice.insert_rope(1, Rope::from("n example")); // } // assert_eq!(&rope, "this is an example string"); // } // #[cfg(feature = "unstable")] // #[test] // fn mut_insert_str() { // let mut rope = Rope::from("this is a string"); // { // let slice = rope.slice_mut(8..); // assert_eq!(&slice, "a string"); // } // { // let mut slice = rope.slice_mut(8..); // slice.insert_str(1, "n example"); // } // assert_eq!(&rope, "this is an example string"); // } // #[cfg(feature = "unstable")] // #[test] // fn mut_insert_char() { // let mut rope = Rope::from("this is a string"); // { // let slice = rope.slice_mut(8..); // assert_eq!(&slice, "a string"); // } // { // let mut slice = rope.slice_mut(8..); // slice.insert(1, 'n'); // } // assert_eq!(&rope, "this is an string"); // } }