cessen / ropey

A utf8 text rope for manipulating and editing large texts.
MIT License
1.04k stars 46 forks source link

Add get_byte_slice/byte_slice #50

Closed archseer closed 2 years ago

archseer commented 2 years ago

Should probably state in the docs that this is marginally faster than slice() if you already have byte indices but that slice() should be preferred for safety (index is always aligned).

Fixes #49

cessen commented 2 years ago

Sorry for taking so long to get to this!

The code mostly looks good to me. However, at the moment this will always panic when trying to slice at a non-char boundary. And this is a problem for a couple of reasons:

  1. It's possible for get_slice_byte() to panic, whereas the intention of the get_*() methods is that they never panic, they just return None.
  2. Ideally I'd like panic error messages to be more informative, and also specific to the actual public method called. With the code as-is, the panic error message will just be a generic &str slicing error message.

Nevertheless, I'm fine merging this as-is and cleaning that up myself when I (eventually) get around to it. But if you have the time and would like to tackle that yourself, let me know, and I'll hold off on merging.

cessen commented 2 years ago

On second thought, I think I'd rather tackle the clean up myself, because I'm realizing that to do it "properly" I'll want to rework some other things in Ropey as well. So I'll merge this as-is, and work on that myself when I get the time.