A C++ Rope-String is a string with efficient insert/erase/replace in middle. It provides following classes
- Rope::Node - the node of tree (Rope.Node module, Node.h header)
- Rope::Tree - the Tree with core integration API (Rope.Tree module, Tree.h header)
- Rope::String - The String class (Rope.String module, String.h header)
- Non-contiguous storage (rope) consisting of leaf chunks connected by a balanced tree.
- Many std::basic_string-like constructors and member functions for familiarity.
- Forward and reverse iterators over characters.
- Efficient insert/erase/replace operations in the middle of a large string.
- Works with multiple character types (char, wchar_t, char8_t, char16_t, char32_t).
You can use this library in three common ways.
- add_subdirectory (recommended for vendoring)
- Copy or add this repository as a subdirectory.
- In your CMakeLists.txt:
add_subdirectory(external/RopeString) target_link_libraries(your_target PRIVATE Rope) target_include_directories(your_target PRIVATE external/RopeString/include)
- FetchContent
- Fetch content:
include(FetchContent) FetchContent_Declare( RopeString GIT_REPOSITORY https://github.com/Sinfolke/Rope-String # GIT_TAG <commit-or-tag> )
- Make available:
FetchContent_MakeAvailable(RopeString) target_link_libraries(your_target PRIVATE Rope)
- find_package (if you have installed it)
- Configure and install the library somewhere first:
cmake -S . -B build cmake --build build --target install - Then in your project:
find_package(Rope CONFIG REQUIRED) target_link_libraries(your_target PRIVATE Rope)
Include the header in your sources:
#include <RopeString.h>Or import C++20 module
import Rope.String;#include <RopeString.h>
#include <iostream>
int main() {
Rope::String s = "Hello";
s.append(", World!");
s.insert(5, 1, '!');
s.erase(0, 1);
auto sub = s.substr(1, 5);
// Iteration (forward)
for (auto ch : s) std::cout << ch;
std::cout << "\n";
// Obtain a contiguous C string view (allocates)
auto c = s.c_str(); // unique_ptr<CharT[]>; zero-terminated copy
std::cout << c.get() << "\n";
// or print directly to buffer
s.print(std::cout);
}The API aims to be familiar to users of std::basic_string, but due to rope storage there are important differences. Below I've listed all methods with information you to note. For exact signatures please see include/BasicString.h.
- at()
- operator[]
- back()
- front()
- data()
- c_str()
- data() returns hold Rope::Tree class
- c_str() returns std::unique_ptr<CharT[]>, holding C like string on heap. Note that it is more expensive than std::string::c_str() as it requires concat entire string
- begin()
- end()
- cbegin()
- cend()
- rbegin()
- rend()
- rcbegin()
- rcend()
- Iteration is linear but may step across leaves
- iterator category is forward, not random-access.
- empty()
- size()
- length()
- clear()
- insert()
- insert_range()
- erase()
- push_back()
- pop_back()
- append
- append_range
- opeartor+=
- replace
- replace_with_range
- copy
- resize
- resize_and_overwrite
- swap
- find()
- rfind()
- find_first_of()
- find_first_not_of()
- find_last_of() 6 find_last_not_of()
- compare()
- starts_with()
- ends_with()
- contains()
- substr()
This repository includes small test executables in tests/ driven by CMake targets:
- access_test
- assignment_test
- capacity_test
- iterators_test
- modifiers_test
- operations_test
- search_test
Generic CMake usage:
cmake -S . -B build cmake --build build --target iterators_test && ./build/iterators_test
In CLion, simply build and run the desired test target from the IDE.