Deal with old libxml incompatibilities.
[htmlpurifier.git] / library / HTMLPurifier / Zipper.php
blob6e21ea070397ff280e5cfae8c51b8f1b47177244
1 <?php
3 /**
4 * A zipper is a purely-functional data structure which contains
5 * a focus that can be efficiently manipulated. It is known as
6 * a "one-hole context". This mutable variant implements a zipper
7 * for a list as a pair of two arrays, laid out as follows:
9 * Base list: 1 2 3 4 [ ] 6 7 8 9
10 * Front list: 1 2 3 4
11 * Back list: 9 8 7 6
13 * User is expected to keep track of the "current element" and properly
14 * fill it back in as necessary. (ToDo: Maybe it's more user friendly
15 * to implicitly track the current element?)
17 * Nota bene: the current class gets confused if you try to store NULLs
18 * in the list.
21 class HTMLPurifier_Zipper
23 public $front, $back;
25 public function __construct($front, $back) {
26 $this->front = $front;
27 $this->back = $back;
30 /**
31 * Creates a zipper from an array, with a hole in the
32 * 0-index position.
33 * @param Array to zipper-ify.
34 * @return Tuple of zipper and element of first position.
36 static public function fromArray($array) {
37 $z = new self(array(), array_reverse($array));
38 $t = $z->delete(); // delete the "dummy hole"
39 return array($z, $t);
42 /**
43 * Convert zipper back into a normal array, optionally filling in
44 * the hole with a value. (Usually you should supply a $t, unless you
45 * are at the end of the array.)
47 public function toArray($t = NULL) {
48 $a = $this->front;
49 if ($t !== NULL) $a[] = $t;
50 for ($i = count($this->back)-1; $i >= 0; $i--) {
51 $a[] = $this->back[$i];
53 return $a;
56 /**
57 * Move hole to the next element.
58 * @param $t Element to fill hole with
59 * @return Original contents of new hole.
61 public function next($t) {
62 if ($t !== NULL) array_push($this->front, $t);
63 return empty($this->back) ? NULL : array_pop($this->back);
66 /**
67 * Iterated hole advancement.
68 * @param $t Element to fill hole with
69 * @param $i How many forward to advance hole
70 * @return Original contents of new hole, i away
72 public function advance($t, $n) {
73 for ($i = 0; $i < $n; $i++) {
74 $t = $this->next($t);
76 return $t;
79 /**
80 * Move hole to the previous element
81 * @param $t Element to fill hole with
82 * @return Original contents of new hole.
84 public function prev($t) {
85 if ($t !== NULL) array_push($this->back, $t);
86 return empty($this->front) ? NULL : array_pop($this->front);
89 /**
90 * Delete contents of current hole, shifting hole to
91 * next element.
92 * @return Original contents of new hole.
94 public function delete() {
95 return empty($this->back) ? NULL : array_pop($this->back);
98 /**
99 * Returns true if we are at the end of the list.
100 * @return bool
102 public function done() {
103 return empty($this->back);
107 * Insert element before hole.
108 * @param Element to insert
110 public function insertBefore($t) {
111 if ($t !== NULL) array_push($this->front, $t);
115 * Insert element after hole.
116 * @param Element to insert
118 public function insertAfter($t) {
119 if ($t !== NULL) array_push($this->back, $t);
123 * Splice in multiple elements at hole. Functional specification
124 * in terms of array_splice:
126 * $arr1 = $arr;
127 * $old1 = array_splice($arr1, $i, $delete, $replacement);
129 * list($z, $t) = HTMLPurifier_Zipper::fromArray($arr);
130 * $t = $z->advance($t, $i);
131 * list($old2, $t) = $z->splice($t, $delete, $replacement);
132 * $arr2 = $z->toArray($t);
134 * assert($old1 === $old2);
135 * assert($arr1 === $arr2);
137 * NB: the absolute index location after this operation is
138 * *unchanged!*
140 * @param Current contents of hole.
142 public function splice($t, $delete, $replacement) {
143 // delete
144 $old = array();
145 $r = $t;
146 for ($i = $delete; $i > 0; $i--) {
147 $old[] = $r;
148 $r = $this->delete();
150 // insert
151 for ($i = count($replacement)-1; $i >= 0; $i--) {
152 $this->insertAfter($r);
153 $r = $replacement[$i];
155 return array($old, $r);