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

Here you find the average performance (time & memory) of each version. A grayed out version indicates it didn't complete successfully (based on exit-code).

VersionSystem time (s)User time (s)Memory (MiB)
8.3.110.0160.01620.94
8.3.100.0190.00824.06
8.3.90.0120.01226.77
8.3.80.0070.01117.97
8.3.70.0170.02116.50
8.3.60.0340.01716.75
8.3.50.0290.01321.27
8.3.40.0100.01418.93
8.3.30.0160.01618.79
8.3.20.0430.00020.19
8.3.10.0120.00921.95
8.3.00.0080.00417.93
8.2.230.0030.00622.58
8.2.220.0000.00937.54
8.2.210.0170.01126.77
8.2.200.0110.00016.88
8.2.190.0130.00616.88
8.2.180.0280.00916.75
8.2.170.0080.01522.96
8.2.160.0190.01921.09
8.2.150.0060.02224.18
8.2.140.0040.00824.66
8.2.130.0110.00722.09
8.2.120.0180.00526.35
8.2.110.0090.01322.18
8.2.100.0070.00717.91
8.2.90.0040.00717.75
8.2.80.0070.00418.77
8.2.70.0080.00417.75
8.2.60.0030.00617.63
8.2.50.0060.00617.93
8.2.40.0030.00618.05
8.2.30.0000.01019.44
8.2.20.0030.00618.13
8.2.10.0040.00418.54
8.2.00.0040.00818.49
8.1.290.0090.01418.88
8.1.280.0150.01525.92
8.1.270.0120.00423.99
8.1.260.0220.00026.35
8.1.250.0180.00728.09
8.1.240.0060.01723.97
8.1.230.0170.00419.04
8.1.220.0090.00317.74
8.1.210.0030.00618.77
8.1.200.0110.00417.35
8.1.190.0030.00717.35
8.1.180.0060.00618.10
8.1.170.0060.00317.62
8.1.160.0030.00619.01
8.1.150.0110.00021.97
8.1.140.0040.00919.58
8.1.130.0030.00719.04
8.1.120.0030.00717.48
8.1.110.0080.00817.58
8.1.100.0000.01117.60
8.1.90.0040.00417.58
8.1.80.0000.00917.46
8.1.70.0100.00717.53
8.1.60.0000.00817.59
8.1.50.0150.00417.69
8.1.40.0030.00717.59
8.1.30.0070.00317.71
8.1.20.0110.00017.71
8.1.10.0000.00917.72
8.1.00.0000.01117.67
8.0.300.0030.01019.80
8.0.290.0100.00016.75
8.0.280.0100.01018.40
8.0.270.0030.01717.27
8.0.260.0030.00619.02
8.0.250.0130.00017.02
8.0.240.0030.01217.08
8.0.230.0040.01316.95
8.0.220.0030.00616.87
8.0.210.0100.00016.87
8.0.200.0090.00416.97
8.0.190.0000.01517.03
8.0.180.0030.01016.96
8.0.170.0090.00916.90
8.0.160.0070.00717.09
8.0.150.0030.00916.93
8.0.140.0160.00416.95
8.0.130.0080.00013.49
8.0.120.0090.00616.93
8.0.110.0070.00316.84
8.0.100.0040.01116.84
8.0.90.0050.00516.92
8.0.80.0280.02516.89
8.0.70.0100.00716.79
8.0.60.0030.01016.76
8.0.50.0090.00816.84
8.0.30.0440.02017.01
8.0.20.0460.03017.40
8.0.10.0040.00716.96
8.0.00.0280.03917.07
7.4.330.0100.00715.55
7.4.320.0080.00416.50
7.4.300.0030.00716.53
7.4.290.0130.00316.55
7.4.280.0120.00416.43
7.4.270.0070.00416.57
7.4.260.0120.00416.35
7.4.250.0090.00316.52
7.4.240.0030.00716.45
7.4.230.0040.00816.56
7.4.220.0230.03116.50
7.4.210.0200.03716.49
7.4.200.0030.00716.53
7.4.160.0380.01816.50
7.4.150.0290.01417.40
7.4.140.0590.02917.86
7.4.130.0340.02416.60
7.4.120.0360.03116.54
7.4.110.0600.03816.46
7.4.100.0780.04516.54
7.4.90.0280.02716.64
7.4.80.0450.02619.39
7.4.70.0980.02016.48
7.4.60.0300.02016.45
7.4.50.0140.01816.39
7.4.40.0610.04016.54
7.4.30.0170.01416.44
7.4.00.0110.01815.09
7.3.330.0140.00013.50
7.3.320.0130.00313.52
7.3.310.0060.00616.35
7.3.300.0110.00416.30
7.3.290.0200.02616.41
7.3.280.0260.02416.36
7.3.270.0420.01517.40
7.3.260.0520.02916.59
7.3.250.0470.02716.44
7.3.240.0420.01616.65
7.3.230.0510.02316.41
7.3.210.0400.01816.64
7.3.200.0230.03416.42
7.3.190.0260.03416.41
7.3.180.0160.01916.44
7.3.170.0830.03216.34
7.3.160.0680.05116.56
7.2.330.0430.02816.89
7.2.320.0350.03216.93
7.2.310.0280.04416.75
7.2.300.0280.03416.65
7.2.290.0280.03216.88
7.2.60.0140.01116.89
7.2.00.0030.01719.07
7.1.200.0070.01015.55
7.1.70.0090.01316.84
7.1.60.0170.00317.35
7.1.50.0170.01417.15
7.1.40.0230.02734.64
7.1.30.0270.02734.58
7.1.20.0200.02034.54
7.1.10.0100.01716.37
7.1.00.0100.01316.30
7.0.200.0060.00916.59
7.0.180.0030.01016.15
7.0.170.0070.01015.96
7.0.160.0070.01015.98
7.0.150.0070.00715.99
7.0.140.0130.00016.05
7.0.130.0130.00716.38
7.0.120.0070.01316.19
7.0.110.0070.00716.05
7.0.100.0100.00316.07
7.0.90.0000.01716.07
7.0.80.0070.00716.25
7.0.70.0000.01316.23
7.0.60.0030.01015.68
7.0.50.0030.01016.13
7.0.40.0030.01016.28
7.0.30.0030.00716.20
7.0.20.0070.00715.98
7.0.10.0030.01016.11
7.0.00.0000.01316.10

preferences:
34.78 ms | 403 KiB | 5 Q