ropey.Rope

class ropey.Rope

A utf8 text rope.

The time complexity of nearly all edit and query operations on Rope are worst-case O(log N) in the length of the rope. Rope is designed to work efficiently even for huge (in the gigabytes) and pathological (all on one line) texts.

Attributes
text

Content of the Rope as a string.

Methods

append(other, /)

Appends a Rope to the end of this one, consuming the other Rope.

byte_to_char(byte_idx, /)

Returns the char index of the given byte.

byte_to_line(byte_idx, /)

Returns the line index of the given byte.

capacity

Total size of the Rope’s text buffer space, in bytes.

char_to_byte(char_idx, /)

Returns the byte index of the given char.

char_to_line(char_idx, /)

Returns the line index of the given char.

from_file(f, /)

Creates a Rope from a file.

from_reader(reader, /)

Creates a Rope from the output of a reader.

from_str(s, /)

Creates a Rope from a string slice.

insert(char_idx, text, /)

Inserts text at char index char_idx.

insert_char(char_idx, ch, /)

Inserts a single char ch at char index char_idx.

len_bytes

Total number of bytes in the Rope.

len_chars

Total number of chars in the Rope.

len_lines

Total number of lines in the Rope.

len_utf16_cu

Total number of utf16 code units that would be in Rope if it were encoded as utf16.

remove(char_range, /)

Removes the text in the given char index range.

shrink_to_fit

Shrinks the Rope’s capacity to the minimum possible.

split_off(char_idx, /)

Splits the Rope at char_idx, returning the right part of the split.

write_to(writer, /)

Writes to content of the Rope to a writer.

write_to_file(f, /)

Writes to content of the Rope to a file.

remove_range

remove_range_from

remove_range_full

remove_range_to

append(other, /)

Appends a Rope to the end of this one, consuming the other Rope.

Note In Python, the other Rope is cloned due to PyO3 restrictions.

Parameters

other – other Rope Rope

byte_to_char(byte_idx, /)

Returns the char index of the given byte.

Parameters

byte_idx (int) – byte index

Returns

char index int

byte_to_line(byte_idx, /)

Returns the line index of the given byte.

Parameters

byte_idx (int) – byte index

Returns

line index int

capacity()

Total size of the Rope’s text buffer space, in bytes.

Returns

capacity int

char_to_byte(char_idx, /)

Returns the byte index of the given char.

Parameters

char_idx (int) – char index

Returns

byte index int

char_to_line(char_idx, /)

Returns the line index of the given char.

Parameters

byte_idx (int) – char index

Returns

line index int

static from_file(f, /)

Creates a Rope from a file.

Parameters

f (str) – filename

Returns

Rope Rope

static from_reader(reader, /)

Creates a Rope from the output of a reader.

Parameters

reader – reader

Returns

Rope Rope

static from_str(s, /)

Creates a Rope from a string slice.

Parameters

s (str) – string

Returns

Rope Rope

insert(char_idx, text, /)

Inserts text at char index char_idx.

Parameters
  • char_idx (int) – char index

  • text (str) – text

insert_char(char_idx, ch, /)

Inserts a single char ch at char index char_idx.

Parameters
  • char_idx (int) – char index

  • ch (str) – char

len_bytes()

Total number of bytes in the Rope.

Returns

number of bytes int

len_chars()

Total number of chars in the Rope.

Returns

number of chars int

len_lines()

Total number of lines in the Rope.

Returns

number of lines int

len_utf16_cu()

Total number of utf16 code units that would be in Rope if it were encoded as utf16.

Returns

number of utf16 code units int

remove(char_range, /)

Removes the text in the given char index range.

In the Python slice:
  • start: int or None, None sets start to 0

  • stop: int or None, None sets stop to self.len_chars()

  • step: unused

Parameters

char_range (slice) – range

shrink_to_fit()

Shrinks the Rope’s capacity to the minimum possible.

split_off(char_idx, /)

Splits the Rope at char_idx, returning the right part of the split.

Parameters

char_idx (int) – char index

Returns

right part of Rope Rope

text

Content of the Rope as a string.

Returns

string str

write_to(writer, /)

Writes to content of the Rope to a writer.

Parameters

writer – writer

write_to_file(f, /)

Writes to content of the Rope to a file.

Parameters

f (str) – filename