protected function skip($chars) {
$this->pos += strspn($this->doc, $chars, $this->pos);
$this->char = ($this->pos<$this->size) ? $this->doc[$this->pos] : null; // next
}
protected function copy_skip($chars) {
$pos = $this->pos;
$len = strspn($this->doc, $chars, $pos);
$this->pos += $len;
$this->char = ($this->pos<$this->size) ? $this->doc[$this->pos] : null; // next
if ($len===0) return '';
return substr($this->doc, $pos, $len);
}
protected function copy_until($chars) {
$pos = $this->pos;
$len = strcspn($this->doc, $chars, $pos);
$this->pos += $len;
$this->char = ($this->pos<$this->size) ? $this->doc[$this->pos] : null; // next
return substr($this->doc, $pos, $len);
}
protected function copy_until_char($char) {
if ($this->char===null) return '';
if (($pos = strpos($this->doc, $char, $this->pos))===false) {
$ret = substr($this->doc, $this->pos, $this->size-$this->pos);
$this->char = null;
$this->pos = $this->size;
return $ret;
}
if ($pos===$this->pos) return '';
$pos_old = $this->pos;
$this->char = $this->doc[$pos];
$this->pos = $pos;
return substr($this->doc, $pos_old, $pos-$pos_old);
}
protected function copy_until_char_escape($char) {
if ($this->char===null) return '';
$start = $this->pos;
while(1) {
if (($pos = strpos($this->doc, $char, $start))===false) {
$ret = substr($this->doc, $this->pos, $this->size-$this->pos);
$this->char = null;
$this->pos = $this->size;
return $ret;
}
if ($pos===$this->pos) return '';
if ($this->doc[$pos-1]==='\\') {
$start = $pos+1;
continue;
}
$pos_old = $this->pos;
$this->char = $this->doc[$pos];
$this->pos = $pos;
return substr($this->doc, $pos_old, $pos-$pos_old);
}
}
// remove noise from html content
protected function remove_noise($pattern, $remove_tag=false) {
$count = preg_match_all($pattern, $this->doc, $matches, PREG_SET_ORDER|PREG_OFFSET_CAPTURE);
for ($i=$count-1; $i>-1; --$i) {
$key = '___noise___'.sprintf('% 3d', count($this->noise)+100);
$idx = ($remove_tag) ? 0 : 1;
$this->noise[$key] = $matches[$i][$idx][0];
$this->doc = substr_replace($this->doc, $key, $matches[$i][$idx][1], strlen($matches[$i][$idx][0]));
}