Ropey is a utf8 text buffer for Rust, designed to be the backing text buffer for applications such as text editors. Ropey is fast, Unicode-safe, has low memory overhead, and can handle huge texts and memory-incoherent edits without trouble.
Internally it's implemented as a b-tree rope.
```rust // Load a text file. let mut text = ropey::Rope::fromreader( File::open("mygreat_book.txt")? )?;
// Print the 516th line (zero-indexed). println!("{}", text.line(515));
// Get the start/end char indices of the line. let startidx = text.linetochar(515); let endidx = text.linetochar(516);
// Remove the line... text.remove(startidx..endidx);
// ...and replace it with something better. text.insert(start_idx, "The flowers are... so... dunno.\n");
// Print the changes, along with the previous few lines for context. let startidx = text.linetochar(511); let endidx = text.linetochar(516); println!("{}", text.slice(startidx..endidx));
// Write the file back out to disk. text.writeto( BufWriter::new(File::create("mygreat_book.txt")?) )?; ```
Ropey is fast and minimizes memory usage:
Ropey's atomic unit of text is
Unicode scalar values
(or char
s in Rust)
encoded as utf8. All of Ropey's editing and slicing operations are done
in terms of char indices, which prevents accidental creation of invalid
utf8 data.
Ropey knows about line breaks, allowing you to index into and iterate over lines of text.
Ropey also recognizes all eight Unicode-specified line breaks: line feed, carriage return, carriage return + line feed, vertical tab, form feed, next line, line separator, and paragraph separator.
Ropey has rope slices that allow you to work with just parts of a rope, using any of the read-only operations of a full rope including iterators and making sub-slices.
Although Ropey is intentionally limited in scope, it also provides APIs for efficiently accessing and working with its internal text chunk representation, allowing additional functionality to be efficiently implemented by client code with minimal overhead.
Ropey ensures that even though clones share memory, everything is thread-safe. Clones can be sent to other threads for both reading and writing.
Ropey does use unsafe code to help achieve some of its space and performance characteristics. Although effort has been put into keeping the unsafe code compartmentalized and making it correct, it is nevertheless not recommended at this time to use Ropey in software that may face adversarial conditions.
Auditing, fuzzing, etc. of the unsafe code in Ropey is extremely welcome. If you find any unsoundness, please file an issue! Also welcome are recommendations for how to remove any of the unsafe code without introducing significant space or performance regressions, or how to compartmentalize the unsafe code even better.
Ropey is licensed under the MIT license (LICENSE.md or http://opensource.org/licenses/MIT)
Contributions are absolutely welcome! However, please open an issue or email me to discuss larger changes, to avoid doing a lot of work that may get rejected.
An overview of Ropey's design can be found here.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in Ropey by you will be licensed as above, without any additional terms or conditions.