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 with_, such as with_push() and with_append().

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());

Insert char into index in this Rope,

Panics

  • If index is greater than the length of this Rope

Time Complexity

O(log n)

Examples

Inserting at index 0 prepends char to this Rope:

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

Inserting at index len prepends char to this Rope:

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

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

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

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.with_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.with_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.with_insert(1, 'b');
assert_eq!(new_rope, Rope::from("abcd"));
assert_eq!(an_rope, Rope::from("acd"));

Insert rope into index in this Rope,

Consumes rope.

Panics

  • If index is greater than the length of this Rope

Time Complexity

O(log n)

Examples

Inserting at index 0 prepends rope to this Rope:

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

Inserting at index len prepends rope to this Rope:

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

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

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

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

Consumes 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.with_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.with_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.with_insert_rope(1, Rope::from("bc"));
assert_eq!(new_rope, Rope::from("abcd"));
assert_eq!(an_rope, Rope::from("ad"))

Insert string slice s into index in this Rope,

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 mut an_rope = Rope::from("cd");
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 mut an_rope = Rope::from("ab");
an_rope.insert_str(2, "cd");
assert_eq!(an_rope, Rope::from("abcd"));

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

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

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.with_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.with_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.with_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, updating it in place.

Note that this is equivalent to using the += operator.

Examples

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

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

Consumes other.

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.with_append(Rope::from("efgh"));
assert_eq!(&another_rope, "abcdefgh");
assert_eq!(&an_rope, "abcd");

Prepends a Rope to the front of this Rope, modifying it in place.

Consumes other.

Examples

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

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

Consumes other.

Examples

use an_rope::Rope;
let an_rope = Rope::from("efgh");
let another_rope = an_rope.with_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.with_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.with_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.

Consumes this rope.

Examples

use an_rope::Rope;
let mut 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 a move iterator over all the strings in this Rope

Consumes self.

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 this Rope

This is the iterator returned by Node::into_iter.

Returns an immutable slice of this Rope between the given indices.

Arguments

  • range: A RangeArgument specifying the range to slice. This can be produced by range syntax like .., a.., ..b or c..d.

Panics

If the start or end indices of the range to slice exceed the length of this Rope.

Examples

#![feature(collections)]
#![feature(collections_range)]

extern crate collections;
extern crate an_rope;
use collections::range::RangeArgument;
use an_rope::Rope;

let rope = Rope::from("this is an example string");
assert_eq!(&rope.slice(4..6), "is");

Returns an mutable slice of this Rope between the given indices.

Arguments

  • range: A RangeArgument specifying the range to slice. This can be produced by range syntax like .., a.., ..b or c..d.

Panics

If the start or end indices of the range to slice exceed the length of this Rope.

Examples

#![feature(collections)]
#![feature(collections_range)]

extern crate collections;
extern crate an_rope;
use collections::range::RangeArgument;
use an_rope::Rope;

let mut rope = Rope::from("this is an example string");
assert_eq!(&mut rope.slice_mut(4..6), "is");

Trait Implementations

impl Clone for Rope
[src]

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

impl Debug for Rope
[src]

Formats the value using the given formatter.

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

Performs the conversion.

impl From<String> for Rope
[src]

Performs the conversion.

impl<'a> From<&'a str> 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<'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 AddAssign for Rope
[src]

Concatenate two Ropes mutably.

Examples

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

impl AddAssign<String> for Rope
[src]

Concatenate a String onto a Rope mutably.

Examples

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

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

Concatenate an &str onto a Rope mutably.

Examples

use an_rope::Rope;
let mut rope = Rope::from(String::from("ab"));
rope += String::from("cd");
assert_eq!(rope, 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<'a> Borrow<RopeSlice<'a>> for &'a Rope
[src]

Immutably borrows from an owned value. Read more

impl Extend<char> for Rope
[src]

Extends a collection with the contents of an iterator. Read more

impl Extend<String> for Rope
[src]

Extends a collection with the contents of an iterator. Read more

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

Extends a collection with the contents of an iterator. Read more

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

Extends a collection with the contents of an iterator. Read more

impl Extend<Rope> for Rope
[src]

Extends a collection with the contents of an iterator. Read more