-
Notifications
You must be signed in to change notification settings - Fork 1
Rope
This page details the current features of the rope
To create a rope, much like the binary tree, the following is needed
use PhpTrees\Rope;
$r = new PhpTrees\Rope();
//or, with a default value
$r = new PhpTrees\Rope("Hello World!");The length of the rope can be determined with the length functions
$r = new PhpTrees\Rope("Hello World!");
$r->length(); //in this case 12in a similar fashion, the character at a specific index can also be found
$r = new PhpTrees\Rope("Hello World!");
$r->index(0); //'H'
$r->index(5); //' '
$r->index(11); //'!'To insert new characters in the rope, at the given position
$r = new PhpTrees\Rope("Hello World!");
r->insert("More test", 4);and to remove characters,
$r = new PhpTrees\Rope("Hello World!");
$r->removeSubstr(4, 2); //removes the characters from possition 4 through 6There are several functions provided to the rope through the RopeFunctions File (included by composer by default) to assist with common rope actions listed below.
2 Ropes can also be concatenated through the use of the concatRope function
$r = new PhpTrees\Rope("Hello");
$r2 = new PhpTrees\Rope(" World!");
$r3 = concatRope($r, $r2); //rope now contains, 'Hello World!'and can be split via the following, note that the result will be an array of Ropes. If the split point is greater then the rope's length or at the end, the result will be an array of the original rope
$r = new PhpTrees\Rope("Hello");
$split = splitRope($r, 2);
//$split[0] = 'He'
//$split[1] = 'llo'Ropes also implement the ArrayAccess interface, so all array operations should be valid
$r = new PhpTrees\Rope("hello");
$r[] = " World!";
$r[0] = 'H';
echo $r[0];
is_set($r[50]);You can also echo out the rope as well
$r = new PhpTrees\Rope("hello")
echo $r;
//outputs hello