test: add emoji/astral-plane correctness tests and bounds-check tests
New tests verify the six fixed bugs:
- emoji insert/remove/slice are char-aware (not UTF-16-unit-aware)
- UTF-8 byte counts are correct for emoji (4 bytes each)
- Valid boundary indices (0, len_chars) are accepted
- RopeBuilder with multiple pushes produces same result as from_str
Co-Authored-By: Claude Sonnet 4.6 noreply@anthropic.com
版权所有:中国计算机学会技术支持:开源发展技术委员会
京ICP备13000930号-9
京公网安备 11010802047560号
Rope-MoonBit
A B-tree rope data structure library for MoonBit, designed for efficient text editing operations.
A rope is a tree-based data structure that represents text as a sequence of small strings, enabling O(log n) insertions and deletions even for very large documents. This makes ropes ideal for text editors, language servers, and any application that performs frequent edits on large texts.
Features
RopeBuilderInstallation
Add to your
moon.mod:Then run:
Quick Start
API Reference
Construction
Rope::new() -> RopeRope::from_str(s: String) -> RopeRopeBuilder::new() -> RopeBuilderRopeBuilder::push(self, chunk: String) -> UnitRopeBuilder::build(self) -> RopeQueries
Rope::len_chars(self) -> IntRope::len_bytes(self) -> IntRope::len_lines(self) -> IntRope::is_empty(self) -> BoolEditing
Rope::insert(self, char_idx: Int, text: String) -> RopeRope::remove(self, start: Int, end_: Int) -> Rope[start, end_)Rope::append(self, text: String) -> RopeRope::split(self, char_idx: Int) -> (Rope, Rope)Rope::concat(self, other: Rope) -> RopeIndex Conversion
Rope::char_at(self, char_idx: Int) -> CharRope::line_to_char(self, line_idx: Int) -> IntRope::char_to_line(self, char_idx: Int) -> IntRope::char_to_byte(self, char_idx: Int) -> IntRope::byte_to_char(self, byte_idx: Int) -> IntSlices
Rope::slice(self, start: Int, end_: Int) -> RopeSlice[start, end_)Rope::line(self, line_idx: Int) -> RopeSliceRopeSlice::to_string(self) -> StringRopeSlice::len_chars(self) -> IntIterators
Rope::chunks(self) -> Iter[String]Rope::chars(self) -> Iter[Char]Rope::lines(self) -> Iter[RopeSlice]Equality
Rope::op_equal(self, other: Rope) -> BoolRopeBuilder Example
Use
RopeBuilderwhen constructing a rope from many pieces — it builds the B-tree once atbuild()time, avoiding repeated tree reconstruction:Unicode Example
Char indices are Unicode code points, not bytes:
Running the Example
Running Tests
Design
The rope is a B-tree where:
This design gives O(log n) worst-case for insert/remove and O(n) for construction.
License
Apache-2.0. See LICENSE.