Struct an_rope::Rope [] [src]

pub struct Rope { /* fields omitted */ }

A Rope

This Rope implementation aims to eventually function as a superset of String, providing the same API plus additional methods. Therefore, code which uses String can easily be ported to use Rope.

Rope provides two APIs for editing a Rope: a destructive, append-in-place API whose methods match those of String, and a non-destructive, persistant API. The persistant API's methods have names prefixed with `, such aspush()andappend()`.

Methods

impl Rope
[src]

Converts a vector of bytes to a Rope.

If you are sure that the byte slice is valid UTF-8, and you don't want to incur the overhead of the validity check, there is an unsafe version of this function, from_utf8_unchecked(),` which has the same behavior but skips the check.

This method will take care to not copy the vector, for efficiency's sake.

Errors

Returns Err if the slice is not UTF-8 with a description as to why the provided bytes are not UTF-8. The vector you moved in is also included.

Examples

Basic usage:

use an_rope::Rope;

// some bytes, in a vector
let sparkle_heart = vec![240, 159, 146, 150];

// We know these bytes are valid, so we'll use `unwrap()`.
let sparkle_heart = Rope::from_utf8(sparkle_heart).unwrap();

assert_eq!(&sparkle_heart, "💖");

Incorrect bytes:

use an_rope::Rope;

// some invalid bytes, in a vector
let sparkle_heart = vec![0, 159, 146, 150];

assert!(Rope::from_utf8(sparkle_heart).is_err());

Decode a UTF-16 encoded vector v into a Rope, returning Err if v contains any invalid data.

Converts a vector of bytes to a Rope without checking that the vector contains valid UTF-8.

See the safe version, from_utf8(), for more details.

Safety

This function is unsafe because it does not check that the bytes passed to it are valid UTF-8. If this constraint is violated, it may cause memory unsafety issues with future users of the Rope, as the rest of the standard library assumes that Ropes are valid UTF-8.

Examples

Basic usage:

use an_rope::Rope;

// some bytes, in a vector
let sparkle_heart = vec![240, 159, 146, 150];

let sparkle_heart = unsafe {
    Rope::from_utf8_unchecked(sparkle_heart)
};

assert_eq!(&sparkle_heart, "💖");

Returns a new empty Rope

Examples

use an_rope::Rope;
let mut an_rope = Rope::new();
assert_eq!(an_rope.len(), 0);

Returns the length of this Rope

Examples

An empty Rope should have length 0.

use an_rope::Rope;
let mut an_empty_rope = Rope::new();
assert_eq!(an_empty_rope.len(), 0);
use an_rope::Rope;
let mut an_empty_rope = Rope::from(String::from(""));
assert_eq!(an_empty_rope.len(), 0);

A Rope with text should have length equal to the number of characters in the Rope.

use an_rope::Rope;
let mut an_rope = Rope::from(String::from("a string"));
assert_eq!(an_rope.len(), "a string".len());

Returns true if this Rope is empty.

Examples

A Rope with no characters should be empty:

use an_rope::Rope;
let an_empty_rope = Rope::new();
assert!(an_empty_rope.is_empty());
use an_rope::Rope;
let an_empty_rope = Rope::from(String::from(""));
assert!(an_empty_rope.is_empty());

A Rope with characters should not be empty:

use an_rope::Rope;
let an_rope = Rope::from("a string");
assert!(!an_rope.is_empty());

Insert ch into index in this Rope, returning a new Rope.

Returns

  • A new Rope with ch inserted at index

Time Complexity

O(log n)

Panics

  • If index is greater than the length of this Rope

Examples

Inserting at index 0 prepends rope to this Rope:

use an_rope::Rope;
let an_rope = Rope::from("bcd");
let new_rope = an_rope.insert(0, 'a');
assert_eq!(new_rope, Rope::from("abcd"));
assert_eq!(an_rope, Rope::from("bcd"));

Inserting at index len prepends char to this Rope:

use an_rope::Rope;
let an_rope = Rope::from("abc");
let new_rope = an_rope.insert(an_rope.len(), 'd');
assert_eq!(new_rope, Rope::from("abcd"));
assert_eq!(an_rope, Rope::from("abc"));

Inserting at an index in the middle inserts char at that index:

use an_rope::Rope;
let an_rope = Rope::from("acd");
let new_rope = an_rope.insert(1, 'b');
assert_eq!(new_rope, Rope::from("abcd"));
assert_eq!(an_rope, Rope::from("acd"));

Insert rope into index in this Rope, returning a new Rope.

Returns

  • A new Rope with rope inserted at index

Time Complexity

O(log n)

Panics

  • If index is greater than the length of this Rope

Examples

Inserting at index 0 prepends rope to this Rope:

use an_rope::Rope;
let an_rope = Rope::from("cd");
let new_rope = an_rope.insert_rope(0, &Rope::from("ab"));
assert_eq!(new_rope, Rope::from("abcd"));
assert_eq!(an_rope, Rope::from("cd"));

Inserting at index len prepends rope to this Rope:

use an_rope::Rope;
let an_rope = Rope::from("ab");
let new_rope = an_rope.insert_rope(an_rope.len(), &Rope::from("cd"));
assert_eq!(new_rope, Rope::from("abcd"));
assert_eq!(an_rope, Rope::from("ab"));

Inserting at an index in the middle inserts rope at that index:

use an_rope::Rope;
let an_rope = Rope::from("ad");
let new_rope = an_rope.insert_rope(1, &Rope::from("bc"));
assert_eq!(new_rope, Rope::from("abcd"));
assert_eq!(an_rope, Rope::from("ad"))

Insert s into index in this Rope, returning a new Rope.

Returns

  • A new Rope with s inserted at index

Panics

  • If index is greater than the length of this Rope

Time Complexity

O(log n)

Examples

Inserting at index 0 prepends s to this Rope:

use an_rope::Rope;
let an_rope = Rope::from("cd");
let an_rope = an_rope.insert_str(0, "ab");
assert_eq!(an_rope, Rope::from("abcd"));

Inserting at index len prepends s to this Rope:

use an_rope::Rope;
let an_rope = Rope::from("ab");
let new_rope = an_rope.insert_str(an_rope.len(), "cd");
assert_eq!(new_rope, Rope::from("abcd"));
assert_eq!(an_rope, Rope::from("ab"));

Inserting at an index in the middle inserts s at that index:

use an_rope::Rope;
let an_rope = Rope::from("ad");
let new_rope = an_rope.insert_str(1, "bc");
assert_eq!(an_rope, Rope::from("ad"));
assert_eq!(new_rope, Rope::from("abcd"));

Appends a Rope to the end of this Rope, returning a new Rope

Note that this is equivalent to using the + operator.

Examples

use an_rope::Rope;
let an_rope = Rope::from("abcd");
let another_rope = an_rope.append(&Rope::from("efgh"));
assert_eq!(&another_rope, "abcdefgh");
assert_eq!(&an_rope, "abcd");

Prepends a Rope to the end of this Rope, returning a new Rope

Examples

use an_rope::Rope;
let an_rope = Rope::from("efgh");
let another_rope = an_rope.prepend(&Rope::from("abcd"));
assert_eq!(&an_rope, "efgh");
assert_eq!(&another_rope, "abcdefgh");
use an_rope::Rope;
let an_rope = Rope::from("");
let another_rope = an_rope.prepend(&Rope::from("abcd"));
assert_eq!(&an_rope, "");
assert_eq!(&another_rope, "abcd");
use an_rope::Rope;
let an_rope = Rope::from("abcd");
let another_rope = an_rope.prepend(&Rope::from(""));
assert_eq!(&an_rope, "abcd");
assert_eq!(&another_rope, &an_rope);
assert_eq!(&another_rope, "abcd");

Splits the rope into two ropes at the given index.

Examples

use an_rope::Rope;
let an_rope = Rope::from(String::from("abcd"));
let (ab, cd) = an_rope.split(2);
assert_eq!(ab, Rope::from(String::from("ab")));
assert_eq!(cd, Rope::from(String::from("cd")));

Returns an iterator over all the strings in this Rope

Returns an iterator over all the lines of text in this Rope.

Returns an iterator over all the bytes in this Rope.

As a Rope consists of a sequence of bytes, we can iterate through a rope by byte. This method returns such an iterator.

Returns an iterator over all the characters in this Rope.

As a Rope consists of valid UTF-8, we can iterate through a Rope by char. This method returns such an iterator.

It's important to remember that char represents a Unicode Scalar Value, and may not match your idea of what a 'character' is. Iteration over grapheme clusters may be what you actually want.

Returns an iterator over the grapheme clusters of self.

The iterator is over the extended grapheme clusters; as UAX#29recommends extended grapheme cluster boundaries for general processing.

Returns an iterator over the words of self, separated on UAX#29 word boundaries.

Here, "words" are just those substrings which, after splitting onUAX#29 word boundaries, contain any alphanumeric characters. That is, the substring must contain at least one character with the Alphabetic property, or with General_Category=Number.

Returns an iterator over substrings of self separated on UAX#29 word boundaries.

The concatenation of the substrings returned by this function is just the original string.

Returns an iterator over the grapheme clusters of self and their byte offsets. See graphemes() for more information.

Examples

let rope = Rope::from("a̐éö̲\r\n");
let gr_inds = rope.grapheme_indices()
                  .collect::<Vec<(usize, &str)>>();
let b: &[_] = &[(0, "a̐"), (3, "é"), (6, "ö̲"), (11, "\r\n")];

assert_eq!(&gr_inds[..], b);

Returns an iterator over substrings of self, split on UAX#29 word boundaries, and their offsets. See split_word_bounds() for more information.

Example

let rope = Rope::from("Brr, it's 29.3°F!");
let swi1 = rope.split_word_bound_indices()
               .collect::<Vec<(usize, &str)>>();
let b: &[_] = &[ (0, "Brr"), (3, ","), (4, " "), (5, "it's")
               , (9, " "), (10, "29.3"),  (14, "°"), (16, "F")
               , (17, "!")];

assert_eq!(&swi1[..], b);

Trait Implementations

impl<T> From<T> for Rope where
    T: Into<NodeLink>, 
[src]

Performs the conversion.

impl Clone for Rope
[src]

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

impl Default for Rope
[src]

Returns the "default value" for a type. Read more

impl<M> Measured<M> for Rope where
    M: Metric,
    NodeLink: Measured<M>,
    String: Measured<M>, 
[src]

Convert the Metric into a byte index into the given Node Read more

Apply Metric to Self Read more

Measure the weight of Node by this metric.

impl Debug for Rope
[src]

Formats the value using the given formatter.

impl Display for Rope
[src]

Formats the value using the given formatter. Read more

impl Into<Vec<u8>> for Rope
[src]

Performs the conversion.

impl Eq for Rope
[src]

impl PartialEq for Rope
[src]

A rope equals another rope if all the bytes in both are equal.

Examples

use an_rope::Rope;
assert!(Rope::from("abcd") == Rope::from("abcd"));
use an_rope::Rope;
assert!(Rope::from("abcd") != Rope::from("ab"));
use an_rope::Rope;
assert!(Rope::from("abcd") != Rope::from("dcab"))

This method tests for !=.

impl PartialEq<str> for Rope
[src]

A rope equals a string if all the bytes in the string equal the rope's.

Examples

use an_rope::Rope;
assert!(&Rope::from("abcd") == "abcd");
use an_rope::Rope;
assert!(&Rope::from("abcd") != "ab");
use an_rope::Rope;
assert!(&Rope::from("abcd") != "dcab");

This method tests for !=.

impl PartialEq<String> for Rope
[src]

A rope equals a string if all the bytes in the string equal the rope's.

Examples

use an_rope::Rope;
assert!(Rope::from("abcd") == String::from("abcd"));
use an_rope::Rope;
assert!(Rope::from("abcd") != String::from("ab"));
use an_rope::Rope;
assert!(Rope::from("abcd") != String::from("dcab"));

This method tests for !=.

impl<'a> Add for &'a Rope
[src]

The resulting type after applying the + operator

Non-destructively concatenate two Ropes, returning a new Rope.

Examples

use an_rope::Rope;
let rope = Rope::from(String::from("ab"));
assert_eq!( &rope + &Rope::from(String::from("cd"))
          , Rope::from(String::from("abcd")) );

impl Add for Rope
[src]

The resulting type after applying the + operator

Non-destructively concatenate two Ropes, returning a new Rope.

Examples

use an_rope::Rope;
let rope = Rope::from(String::from("ab"));
assert_eq!( rope + Rope::from(String::from("cd"))
          , Rope::from(String::from("abcd")) );

impl Add<String> for Rope
[src]

The resulting type after applying the + operator

Non-destructively concatenate a Rope and a String.

Returns a new Rope

Examples

use an_rope::Rope;
let rope = Rope::from(String::from("ab"));
assert_eq!( rope + String::from("cd")
          , Rope::from(String::from("abcd")));

impl<'a, 'b> Add<&'b str> for &'a Rope
[src]

The resulting type after applying the + operator

Non-destructively concatenate a Rope and an &str.

Returns a new Rope

Examples

use an_rope::Rope;
let rope = Rope::from(String::from("ab"));
assert_eq!( &rope + "cd"
          , Rope::from(String::from("abcd")));

impl<'a> Add<&'a str> for Rope
[src]

The resulting type after applying the + operator

Non-destructively concatenate a Rope and an &str.

Returns a new Rope

Examples

use an_rope::Rope;
let rope = Rope::from(String::from("ab"));
assert_eq!( rope + "cd"
          , Rope::from(String::from("abcd")));

impl Index<usize> for Rope
[src]

The returned type after indexing

Recursively index the Rope to return the i th character.

Examples

use an_rope::Rope;
let an_rope = Rope::from(String::from("abcd"));
assert_eq!(&an_rope[0], "a");
assert_eq!(&an_rope[1], "b");
assert_eq!(&an_rope[2], "c");
assert_eq!(&an_rope[3], "d");

Time complexity

O(log n)

impl Index<Range<usize>> for Rope
[src]

The returned type after indexing

The method for the indexing (container[index]) operation

impl Index<RangeTo<usize>> for Rope
[src]

The returned type after indexing

The method for the indexing (container[index]) operation

impl Index<RangeFrom<usize>> for Rope
[src]

The returned type after indexing

The method for the indexing (container[index]) operation

impl IndexMut<Range<usize>> for Rope
[src]

The method for the mutable indexing (container[index]) operation

impl IndexMut<RangeTo<usize>> for Rope
[src]

The method for the mutable indexing (container[index]) operation

impl IndexMut<RangeFrom<usize>> for Rope
[src]

The method for the mutable indexing (container[index]) operation

impl FromIterator<char> for Rope
[src]

Creates a value from an iterator. Read more

impl FromIterator<String> for Rope
[src]

Creates a value from an iterator. Read more

impl FromIterator<Rope> for Rope
[src]

Creates a value from an iterator. Read more

impl<'a> FromIterator<&'a char> for Rope
[src]

Creates a value from an iterator. Read more

impl<'a> FromIterator<&'a str> for Rope
[src]

Creates a value from an iterator. Read more