Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

(no description)

File Size: 157 lines (5 kb)
Included or required:0 times
Referenced: 0 times
Includes or requires: 0 files

Defines 1 class

HTMLPurifier_Zipper:: (11 methods):
  __construct()
  fromArray()
  toArray()
  next()
  advance()
  prev()
  delete()
  done()
  insertBefore()
  insertAfter()
  splice()


Class: HTMLPurifier_Zipper  - X-Ref

A zipper is a purely-functional data structure which contains
a focus that can be efficiently manipulated.  It is known as
a "one-hole context".  This mutable variant implements a zipper
for a list as a pair of two arrays, laid out as follows:

Base list: 1 2 3 4 [ ] 6 7 8 9
Front list: 1 2 3 4
Back list: 9 8 7 6

User is expected to keep track of the "current element" and properly
fill it back in as necessary.  (ToDo: Maybe it's more user friendly
to implicitly track the current element?)

Nota bene: the current class gets confused if you try to store NULLs
in the list.
__construct($front, $back)   X-Ref
No description

fromArray($array)   X-Ref
Creates a zipper from an array, with a hole in the
0-index position.

param: Array to zipper-ify.
return: Tuple of zipper and element of first position.

toArray($t = NULL)   X-Ref
Convert zipper back into a normal array, optionally filling in
the hole with a value. (Usually you should supply a $t, unless you
are at the end of the array.)


next($t)   X-Ref
Move hole to the next element.

param: $t Element to fill hole with
return: Original contents of new hole.

advance($t, $n)   X-Ref
Iterated hole advancement.

param: $t Element to fill hole with
param: $i How many forward to advance hole
return: Original contents of new hole, i away

prev($t)   X-Ref
Move hole to the previous element

param: $t Element to fill hole with
return: Original contents of new hole.

delete()   X-Ref
Delete contents of current hole, shifting hole to
next element.

return: Original contents of new hole.

done()   X-Ref
Returns true if we are at the end of the list.

return: bool

insertBefore($t)   X-Ref
Insert element before hole.

param: Element to insert

insertAfter($t)   X-Ref
Insert element after hole.

param: Element to insert

splice($t, $delete, $replacement)   X-Ref
Splice in multiple elements at hole.  Functional specification
in terms of array_splice:

$arr1 = $arr;
$old1 = array_splice($arr1, $i, $delete, $replacement);

list($z, $t) = HTMLPurifier_Zipper::fromArray($arr);
$t = $z->advance($t, $i);
list($old2, $t) = $z->splice($t, $delete, $replacement);
$arr2 = $z->toArray($t);

assert($old1 === $old2);
assert($arr1 === $arr2);

NB: the absolute index location after this operation is
*unchanged!*

param: Current contents of hole.