3v4l.org

run code in 300+ PHP versions simultaneously
<?php namespace fema\utils; class CharSequence { private $linesBreakIndex; private $chars; private $offset; private $length; private function __construct(array $chars, int $offset, int $length, array $linesBreakIndex) { $this->chars = $chars; $this->offset = $offset; $this->length = $length; $this->linesBreakIndex = $linesBreakIndex; var_dump([$this->toString(), $linesBreakIndex]); if(count($linesBreakIndex) > 0){ echo "line 0"; $this->getLine(0); for($i=0;$i<count($linesBreakIndex); $i++){ echo "line " . ($i+1); $this->getLine($linesBreakIndex[$i]+1); } } } public function length() : int { return $this->length; } public function getLinesCount() : int { return count($this->linesBreakIndex) + 1; } public function getLinesBreakIndex() : int { return $this->linesBreakIndex; } private function getLineBreakIndexIndex(int $charIndex) : int { $arr = $this->linesBreakIndex; $start = 0; $end = count($arr); while($start < $end){ var_dump([$start, $middle, $end]); $middle = ($start + $end) / 2; if($arr[$middle] === $charIndex){ return $middle - 1; }elseif($charIndex > $arr[$middle] && ($middle+1>=count($arr) || $charIndex <= $arr[$middle+1])){ return $middle; }elseif($charIndex < $arr[$middle]){ $end = $middle; }else{ $start = $middle + 1; } } return -1; } public function getLine(int $charIndex) : CharSequence { if($charIndex < 0 || $charIndex >= $this->length) { throw new \InvalidArgumentException("Char index out of bounds [0, $this->length): $charIndex"); } $lineIndexIndex = $this->getLineBreakIndexIndex($charIndex); echo "\n$charIndex => $lineIndexIndex\n"; $start = $lineIndexIndex === -1 ? 0 : $this->linesBreakIndex[$lineIndexIndex]+1; $end = $lineIndexIndex + 1 >= count($this->linesBreakIndex) ? $this->length : $this->linesBreakIndex[$lineIndexIndex+1]; return $this->subCharSequence($start, $end - $start); } public function charAt(int $i) : string { if($i < 0 || $i >= $this->length) { throw new \InvalidArgumentException("Char index out of bounds [0, $this->length): $i"); } else { return $this->chars[$this->offset + $i]; } } public function substring(int $start, ?int $length = null) : string { if($start < 0 || $start >= $this->length) { throw new \InvalidArgumentException("Start out of bounds [0, $this->length): $start"); } elseif($length !== null && $length < 0 || $start + $length > $this->length) { throw new \InvalidArgumentException("Length out of bounds [0, " . ($this->length-$start) . "]: $length"); } else { $ret = ''; for($i = $start; $i<$start+$length; $i++) { $ret .= $this->chars[$this->offset + $i]; } return $ret; } } public function subCharSequence(int $start, ?int $length = null) : CharSequence { if($start < 0 || $start >= $this->length) { throw new \InvalidArgumentException("Start out of bounds [0, $this->length): $start"); } elseif($length !== null && $length < 0 || $start + $length > $this->length) { throw new \InvalidArgumentException("Length out of bounds [0, " . ($this->length-$start) . "]: $length"); } else { if($length === null) { $length = $this->length - $start; } $newLineBreaks = []; $i = $this->getLineBreakIndexIndex($start); if($i === -1 || $this->linesBreakIndex[$i] < $start){ $i++; } for(; $i<count($this->linesBreakIndex) && $this->linesBreakIndex[$i] < $start + $length; $i++){ $newLineBreaks[] = $this->linesBreakIndex[$i] - $start; } return new CharSequence($this->chars, $this->offset + $start, $length, $newLineBreaks); } } public function toString() : string { $ret = ''; for($i = 0; $i < $this->length; $i++){ $ret .= $this->chars[$this->offset + $i]; } return $ret; } public static function getInstance(string $string, bool $normalizeNewLines = false) { $chars = []; $pointer = 0; $length = 0; $linesBreak = []; $prevChar = null; while(($char = static::nextChar($string, $pointer)) !== null) { $realChar = null; if(!$normalizeNewLines) { $realChar = $char; } else { if($char === "\r") { $realChar = "\n"; } elseif($char !== "\n" || $prevChar !== "\r") { $realChar = $char; } } $prevChar = $char; if($realChar !== null) { if($realChar === "\n"){ $linesBreak[] = $length; } $chars[] = $realChar; $length++; } } return new CharSequence($chars, 0, $length, $linesBreak); } private static function nextChar(string $string, int &$pointer) : ?string { if(!isset($string[$pointer])) { return null; } $char = ord($string[$pointer]); if($char < 128){ return $string[$pointer++]; }else{ if($char < 224){ $bytes = 2; }elseif($char < 240){ $bytes = 3; }elseif($char < 248){ $bytes = 4; }elseif($char == 252){ $bytes = 5; }else{ $bytes = 6; } $str = substr($string, $pointer, $bytes); $pointer += $bytes; return $str; } } } echo "Creo stringa originaria\n"; $cs = CharSequence::getInstance("0123456789\n987654321"); echo "\n\nsubstr 10\n"; $cs->subCharSequence(10);

Abusive script

This script was stopped while abusing our resources


preferences:
37.65 ms | 405 KiB | 5 Q