• Stars
    star
    254
  • Rank 154,628 (Top 4 %)
  • Language
    C
  • License
    Other
  • Created over 11 years ago
  • Updated over 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

UTF-8 rope library for C

librope

This is a little C library for heavyweight utf-8 strings (rope). Unlike regular C strings, ropes can do substring insertion and deletion in O(log n) time.

librope is implemented using skip lists, which have the same big-O time complexity as trees but don't require rebalancing.

librope is fast. It will happily perform ~15 million edit operations per second on a modern CPU. Inserts and deletes in librope outperform straight C strings for any document longer than a few hundred bytes.

Support

This library works (C code never dies). But I'm moving to rust for my newer projects. This library has been rewritten in rust as Jumprope. Jumprope is another 2-3x faster than this library on real world editing traces. Its obnoxiously fast.

Usage

Just add rope.c and rope.h to your project. Be sure to add rope.c to your compile line as well.

// Import rope library into project
#include "rope.h"

// Make a new empty rope
rope *r = rope_new();

// Put some content in it (at position 0)
rope_insert(r, 0, "Hi there!");

// Delete 6 characters at position 2
rope_del(r, 2, 6);

// Get the whole string back out of the rope
uint8_t *str = rope_create_cstr(r);

// str now contains "Hi!"! Test it out!:
_rope_print(r);

// Done with the rope?
rope_free(r);

Wide Character String Compatibility

String insertion / deletion positions in Javascript, Objective-C (NSString), Java, C# and others are wrong sometimes!!!

These languages store strings as wchar arrays (arrays of two byte characters). Some characters in the unicode character set require more than two bytes. These languages encode such characters using multiple wchars as per UTF-16. This works most of the time. However, insertion and deletion positions in these strings still refer to offsets in the underlying array. So unicode characters which take up 4 bytes in UTF-16 count as two characters for the purpose of deletion ranges, insertion positions and string length.

Even though these characters are exceptionally rare, I don't want my editor to go all funky if people start getting creative. About a quarter of librope's code is dedicated to fixing this mismatch. However, bookkeeping isn't free - librope performance drops by 35% when wchar conversion support is enabled.

For more information, read my blog post about it.

Long story short, if you need to interoperate with strings from any of these dodgy languages, here's what you do:

  • Compile with -DROPE_WCHAR=1. This macro enables the expensive wchar bookkeeping.
  • Use the alternate insert & delete functions rope_insert_at_wchar(...) and rope_del_at_wchar(...) when your index / size is specified in UTF-16 offsets.

Take a look at the header file for documentation.

Beware:

  • When using rope_insert_at_wchar you still need to convert the string you're inserting into UTF-8 before you pass it into librope.
  • The API lets you try to delete or insert halfway through a large character. You probably don't want to do that.
  • librope is 100% faithful when it comes to the characters you're inserting. If your string has byte order marks, you might want to remove them before passing the string into librope.

More Repositories

1

ShareJS

Collaborative editing in any app
JavaScript
4,929
star
2

noisejs

Javascript 2D Perlin & Simplex noise functions
JavaScript
1,569
star
3

diamond-types

The world's fastest CRDT. WIP.
Rust
1,194
star
4

Chipmunk-js

Port of slembcke/Chipmunk-Physics to Javascript
JavaScript
537
star
5

node-browserchannel

An implementation of a google browserchannel server in node.js
CoffeeScript
287
star
6

statecraft

Manage state with finesse
TypeScript
177
star
7

sephsplace

My own version of r/place, done in a weekend
JavaScript
130
star
8

jumprope-rs

Rust
104
star
9

reference-crdts

Simple, tiny spec-compliant reference implementations of Yjs and Automerge's list types.
TypeScript
98
star
10

jumprope

Fast string editing in Javascript using skip lists
JavaScript
86
star
11

steamdance

A multiplayer steam-based CPU simulator
JavaScript
45
star
12

editing-traces

Real world text editing traces for benchmarking CRDT and Rope data structures
JavaScript
38
star
13

schemaboi

Mergable, efficient data schema for intercompatible apps
TypeScript
33
star
14

textot.rs

Text operational transform library, for rust. Compatible with libot, ottypes/text.
Rust
26
star
15

sharedb

ShareJS server, in C.
C
24
star
16

appstate

CoffeeScript
24
star
17

replica

Local first application platform built using CRDTs
TypeScript
18
star
18

braid-protocol

TypeScript
18
star
19

wangjs

A javascript Wang tile implementation
CoffeeScript
17
star
20

mail-viewer

Pure browser email viewer
JavaScript
15
star
21

diamond-js

Javascript wrapper bindings for diamond types
14
star
22

fdb-tuple

Pure javascript FoundationDB tuple encoder and decoder
TypeScript
12
star
23

node-timerstub

Stubbed out timer objects for fast unit testing in nodejs
CoffeeScript
11
star
24

braid-db

Simple database prototype
Rust
10
star
25

unicount

JavaScript
9
star
26

react-static-example

JavaScript
9
star
27

gmail-jmap

Simple JMAP wrapper for gmail
TypeScript
9
star
28

mime-to-jmap

Tools to convert RFC2822 email messages to JMAP
C
9
star
29

unsplash

JavaScript
8
star
30

tanksalot

CoffeeScript
7
star
31

lightwave

Fork of Torben Weis's lightwave experimental branch - http://code.google.com/p/lightwave/
Go
7
star
32

TP2

An implementation of OT for text which supports TP2 (for p2p editing)
CoffeeScript
6
star
33

text-crdt-rust2

Rust
6
star
34

miniohm

JavaScript
6
star
35

simple-crdt-text

Simple typescript reference implementation for a simple RGA based plain text CRDT
TypeScript
6
star
36

axidraw

Python
5
star
37

livedb-foundation

CoffeeScript
5
star
38

dt-simple-wiki

World's simplest wiki on top of diamond types
JavaScript
5
star
39

crdt-examples

CRDT examples from a DWEB talk
JavaScript
5
star
40

glassbeadtimer

Svelte
4
star
41

killdinohitler

Stealth jam game
CoffeeScript
4
star
42

keykitten

Stores your public key.
4
star
43

lovehot

Lua
4
star
44

ARTIST

Michael Brough and Andi McClure's 10 second artist game, allowing a starting image
Lua
4
star
45

skiplistrs

Rust
4
star
46

map2

Tuple map type for javascript (a,b) -> x
TypeScript
3
star
47

tp2stuff

CoffeeScript
3
star
48

set2

Tuple set type for javascript
JavaScript
3
star
49

resolvable

TypeScript
2
star
50

libot-swift

Swift
2
star
51

roboname

A simple robot-themed name generator for nodejs
CoffeeScript
2
star
52

braid-explorer

UI experiment with braid for data first software
TypeScript
2
star
53

space

Spaaaaaaaace
JavaScript
2
star
54

robot-party

Robots run robots.
CoffeeScript
2
star
55

braid-kernel

TypeScript
2
star
56

streamtoiter

TypeScript
2
star
57

Attila

a little game prototype
CoffeeScript
2
star
58

wave-prototype

Simple prototype of a wave-like email service
TypeScript
2
star
59

canvasproxy

CoffeeScript
2
star
60

space-server

C
2
star
61

census-couch

A simple couchapp site for exploring census data. Way incomplete.
JavaScript
1
star
62

state-client

TypeScript
1
star
63

Clickboss

Click on the boss.
CoffeeScript
1
star
64

FactoryFactoryImpl

The code from my minecraft machine that makes anything
1
star
65

boilerplate-compiler

CoffeeScript
1
star
66

braid-cli

JavaScript
1
star
67

mars2

JavaScript
1
star
68

boilerplate-sim

Boilerplate simulator reference implementation
CoffeeScript
1
star
69

boilerplate-gif

CoffeeScript
1
star
70

ietf-sync-prototype

JavaScript
1
star
71

mtgox

CoffeeScript
1
star
72

boilerplate-jit

CoffeeScript
1
star
73

raider

Simple ECS-style game prototype for multiplayer dungeon raid type things. https://home.seph.codes/public/game/
Rust
1
star
74

state-server

TypeScript
1
star
75

Cocontroller

Userland USB controller for XBox360 controllers on a mac.
Objective-C
1
star
76

visbindiff

CoffeeScript
1
star
77

rle-utils

Javascript utilities for internal run-length encoding
TypeScript
1
star
78

mars

CSS
1
star
79

boilerbot

CoffeeScript
1
star