123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157 |
- <?php
- class HTMLPurifier_Zipper
- {
- public $front, $back;
- public function __construct($front, $back) {
- $this->front = $front;
- $this->back = $back;
- }
-
- static public function fromArray($array) {
- $z = new self(array(), array_reverse($array));
- $t = $z->delete();
- return array($z, $t);
- }
-
- public function toArray($t = NULL) {
- $a = $this->front;
- if ($t !== NULL) $a[] = $t;
- for ($i = count($this->back)-1; $i >= 0; $i--) {
- $a[] = $this->back[$i];
- }
- return $a;
- }
-
- public function next($t) {
- if ($t !== NULL) array_push($this->front, $t);
- return empty($this->back) ? NULL : array_pop($this->back);
- }
-
- public function advance($t, $n) {
- for ($i = 0; $i < $n; $i++) {
- $t = $this->next($t);
- }
- return $t;
- }
-
- public function prev($t) {
- if ($t !== NULL) array_push($this->back, $t);
- return empty($this->front) ? NULL : array_pop($this->front);
- }
-
- public function delete() {
- return empty($this->back) ? NULL : array_pop($this->back);
- }
-
- public function done() {
- return empty($this->back);
- }
-
- public function insertBefore($t) {
- if ($t !== NULL) array_push($this->front, $t);
- }
-
- public function insertAfter($t) {
- if ($t !== NULL) array_push($this->back, $t);
- }
-
- public function splice($t, $delete, $replacement) {
-
- $old = array();
- $r = $t;
- for ($i = $delete; $i > 0; $i--) {
- $old[] = $r;
- $r = $this->delete();
- }
-
- for ($i = count($replacement)-1; $i >= 0; $i--) {
- $this->insertAfter($r);
- $r = $replacement[$i];
- }
- return array($old, $r);
- }
- }
|