an-cabal / an-rope

an rope data structure
https://docs.rs/an-rope
MIT License
11 stars 2 forks source link

Implement Rope slicing #29

Closed hawkw closed 7 years ago

hawkw commented 7 years ago

This PR implements Rope slicing. We use a custom RopeSlice type to represent slices of a Rope, rather than constructing an &str, so that you can slice a rope cheaply, and slice a rope mutably but still modify the underlying Rope.

RopeSlices may be converted into new Ropes or into Strings. Mutable RopeSlices expose the same set of insert_* methods as Ropes, which mutate the sliced Rope in place.

I'd like for Rope to implement Borrow<RopeSlice<'a>>, so that we can return RopeSlices from ops::Index, but due to limitations in Rust's type system, this is currently not possible. Currently, RopeSlices are produced using the Rope.slice() method, which is slightly less ergonomic, but allows us to return a non-reference type as the borrowed form. Rope.slice() takes a RangeArgument<usize>, so it can be called with range syntax like .., a.., ..b or c..d.

This PR also includes an implementation for Rope::char_indices(). Closes #26.

Closes #19.

hawkw commented 7 years ago

Of course, Rope slices aren't done, they don't implement all of the methods we'd eventually like them to, but the same is true of Rope, and a majority of these methods on RopeSlice are waiting until the same methods are implemented for Ropes.

hawkw commented 7 years ago

@cjm00, when you've got a spare minute, I'd love it if you'd glance over this before I go ahead and merge it.

hawkw commented 7 years ago

@cjm00, since you're currently without internet access, I'm gonna go ahead and merge this.