3v4l.org

run code in 300+ PHP versions simultaneously
<?php class ContentTypeBuilder { private $contentTypeFactory; private $CTLs = [ "\x00" => 1, "\x01" => 1, "\x02" => 1, "\x03" => 1, "\x04" => 1, "\x05" => 1, "\x06" => 1, "\x07" => 1, "\x08" => 1, /* "\x09" */ /* "\x0A" */ "\x0B" => 1, "\x0C" => 1, /* "\x0D" */ "\x0E" => 1, "\x0F" => 1, "\x10" => 1, "\x11" => 1, "\x12" => 1, "\x13" => 1, "\x14" => 1, "\x15" => 1, "\x16" => 1, "\x17" => 1, "\x18" => 1, "\x19" => 1, "\x1A" => 1, "\x1B" => 1, "\x1C" => 1, "\x1D" => 1, "\x1E" => 1, "\x1F" => 1, "\x7F" => 1, ]; private $LWS = [ "\x09" => 1, "\x0A" => 1, "\x0D" => 1, "\x20" => 1, ]; private $separators = [ "\x28" => '(', "\x29" => ')', "\x3C" => '<', "\x3E" => '>', "\x40" => '@', "\x2C" => ',', "\x3A" => ':', "\x5C" => '\\', "\x22" => '"', "\x5B" => '[', "\x5D" => ']', "\x3F" => '?', "\x7B" => '{', "\x7D" => '}', ]; public function __construct(ContentTypeFactory $contentTypeFactory) { $this->contentTypeFactory = $contentTypeFactory; } public function build($typeDef, &$qValue = null) { /* Note: * * This routine favours speed and efficiency over readability and DRY. Deal with it. * * All chars are written as hex literals, to avoid any breakage if some idiot changes the * encoding of this source file to something that doesn't use ASCII code points for the * lower 128. AFAIK the PHP interpreter doesn't yet support this, but this code is a * literal implementation of a concrete spec and doesn't need to be maintainable in the * traditional sense, so I don't want to have to come back here to maintain it. */ $typeDefBytes = (string) $typeDef; $totalLength = strlen($typeDefBytes); $bytePos = 0; // Skip leading LWS while ($bytePos !== $totalLength && isset($this->LWS[$typeDefBytes[$bytePos]])) $bytePos++; $type = "\x00"; for (; $bytePos < $totalLength; $bytePos++) { if ( isset($this->CTLs[$typeDefBytes[$bytePos]]) // CTL || isset($this->LWS[$typeDefBytes[$bytePos]]) // LWS || isset($this->separators[$typeDefBytes[$bytePos]]) // separator || ($typeDefBytes[$bytePos] & "\x80") === "\x80" // >127 || $typeDefBytes[$bytePos] === "\x3B" // ; || $typeDefBytes[$bytePos] === "\x3D" // = ) { trigger_error( 'Syntax error in Accept header value:' . ' invalid token character ' . $typeDefBytes[$bytePos] . ' (0x' . sprintf('%02X', ord($typeDefBytes[$bytePos])) . ')' . ' at offset ' . $bytePos , E_USER_NOTICE); return null; } else if ($typeDefBytes[$bytePos] === "\x2F") { // / $bytePos++; break; } else { $type[$bytePos] = $typeDefBytes[$bytePos]; } } $subType = "\x00"; for ($valuePos = 0; $bytePos < $totalLength; $bytePos++) { if ( isset($this->CTLs[$typeDefBytes[$bytePos]]) // CTL || isset($this->separators[$typeDefBytes[$bytePos]]) // separator || ($typeDefBytes[$bytePos] & "\x80") === "\x80" // >127 || $typeDefBytes[$bytePos] === "\x2F" // / || $typeDefBytes[$bytePos] === "\x3D" // = ) { // invalid char for token trigger_error( 'Syntax error in Accept header value:' . ' invalid token character ' . $typeDefBytes[$bytePos] . ' (0x' . sprintf('%02X', ord($typeDefBytes[$bytePos])) . ')' . ' at offset ' . $bytePos , E_USER_NOTICE); return null; } else if (isset($this->LWS[$typeDefBytes[$bytePos]])) { // LWS $bytePos++; while ($bytePos !== $totalLength && isset($this->LWS[$typeDefBytes[$bytePos]])) $bytePos++; if ($typeDefBytes[$bytePos] === "\x3B") $bytePos++; // ; while ($bytePos !== $totalLength && isset($this->LWS[$typeDefBytes[$bytePos]])) $bytePos++; break; } else if ($typeDefBytes[$bytePos] === "\x3B") { // ; $bytePos++; while ($bytePos < $totalLength && isset($this->LWS[$typeDefBytes[$bytePos]])) $bytePos++; break; } else { $subType[$valuePos++] = $typeDefBytes[$bytePos]; } } // */something is not valid if ($type === "\x2A" && $subType !== "\x2A") { // * trigger_error( 'Semantic error in Accept header value:' . ' */' . $subType . ' is not a valid media-type' , E_USER_NOTICE); return null; } $params = []; $qValue = 1; while ($bytePos < $totalLength) { $name = "\x00"; for ($valuePos = 0; $bytePos < $totalLength; $bytePos++) { if ( isset($this->CTLs[$typeDefBytes[$bytePos]]) // CTL || isset($this->LWS[$typeDefBytes[$bytePos]]) // LWS || isset($this->separators[$typeDefBytes[$bytePos]]) // separator || ($typeDefBytes[$bytePos] & "\x80") === "\x80" // >127 || $typeDefBytes[$bytePos] === "\x3B" // ; || $typeDefBytes[$bytePos] === "\x2F" // / ) { // invalid char for token trigger_error( 'Syntax error in Accept header value:' . ' invalid token character ' . $typeDefBytes[$bytePos] . ' (0x' . sprintf('%02X', ord($typeDefBytes[$bytePos])) . ')' . ' at offset ' . $bytePos , E_USER_NOTICE); return null; } else if ($typeDefBytes[$bytePos] === "\x3D") { // = $bytePos++; break; } else { $name[$valuePos++] = $typeDefBytes[$bytePos]; } } if ($bytePos === $totalLength) { trigger_error( 'Syntax error in Accept header value:' . ' media-type parameter ' . $name . ' has no value' . ' at offset ' . $bytePos , E_USER_NOTICE); return null; } if (isset($this->LWS[$typeDefBytes[$bytePos]])) { trigger_error( 'Syntax error in Accept header value:' . ' LWS is not legal between a media-type parameter name and its value' . ' at offset ' . $bytePos , E_USER_NOTICE); return null; } if ($typeDefBytes[$bytePos] === "\x22") { // " quoted-string if ($typeDefBytes[++$bytePos] === "\x22") { // " empty string $value = ''; $bytePos++; } else { $value = "\x00"; for ($valuePos = 0; $bytePos < $totalLength; $bytePos++) { switch ($typeDefBytes[$bytePos]) { case "\x0D": case "\x0A": case "\x20": case "\x09": // LWS while (isset($this->LWS[$typeDefBytes[$bytePos + 1]])) $bytePos++; $value[$valuePos++] = ' '; // collapse LWS to a single SP break; case "\x22": // " $bytePos++; while ($bytePos !== $totalLength && isset($this->LWS[$typeDefBytes[$bytePos]])) $bytePos++; if ($typeDefBytes[$bytePos] === "\x3B") $bytePos++; // ; while ($bytePos !== $totalLength && isset($this->LWS[$typeDefBytes[$bytePos]])) $bytePos++; break 2; // end of value case "\x5C": // \ if (($typeDefBytes[++$bytePos] & "\x80") !== "\x00") { trigger_error( 'Syntax error in Accept header value:' . ' invalid quoted single character ' . $typeDefBytes[$bytePos] . ' (0x' . sprintf('%02X', ord($typeDefBytes[$bytePos])) . ')' . ' at offset ' . $bytePos , E_USER_NOTICE); return null; // >127 not a valid escape sequence } // break; intentionally omitted default: $value[$valuePos++] = $typeDefBytes[$bytePos]; } } } } else { // token $value = "\x00"; for ($valuePos = 0; $bytePos < $totalLength; $bytePos++) { if ( isset($this->CTLs[$typeDefBytes[$bytePos]]) // CTL || isset($this->separators[$typeDefBytes[$bytePos]]) // separator || ($typeDefBytes[$bytePos] & "\x80") === "\x80" // >127 || $typeDefBytes[$bytePos] === "\x2F" // / || $typeDefBytes[$bytePos] === "\x3D" // = ) { trigger_error( 'Syntax error in Accept header value:' . ' invalid token character ' . $typeDefBytes[$bytePos] . ' (0x' . sprintf('%02X', ord($typeDefBytes[$bytePos])) . ')' . ' at offset ' . $bytePos , E_USER_NOTICE); return null; } else if (isset($this->LWS[$typeDefBytes[$bytePos]])) { // LWS $bytePos++; while ($bytePos !== $totalLength && isset($this->LWS[$typeDefBytes[$bytePos]])) $bytePos++; if ($typeDefBytes[$bytePos] === "\x3B") $bytePos++; // ; while ($bytePos !== $totalLength && isset($this->LWS[$typeDefBytes[$bytePos]])) $bytePos++; break; } else if ($typeDefBytes[$bytePos] === "\x3B") { // ; $bytePos++; while ($bytePos !== $totalLength && isset($this->LWS[$typeDefBytes[$bytePos]])) $bytePos++; break; } else { $value[$valuePos++] = $typeDefBytes[$bytePos]; } } } if (($name | "\x20") === "\x71") { // q $qValue = (float) $value; } else { $params[strtolower($name)] = $value; } } return $this->contentTypeFactory->create($type, $subType, $params); } } class ContentTypeFactory { public function create($type, $subType, array $params = []) { return new ContentType($type, $subType, $params); } } class ContentType { private $type; private $subType; private $params; public function __construct($type, $subType, array $params = []) { $this->type = strtolower($type); $this->subType = strtolower($subType); $this->params = $params; } public function __toString() { $params = []; foreach ($this->params as $key => $val) { $params[] = $key . '=' . $val; } return $this->getType() . ($params ? ';' . implode(';', $params) : ''); } public function getFullType() { return $this->type . '/' . $this->subType; } public function setType($type) { $this->type = strtolower($type); } public function getType() { return $this->type; } public function setSubType($subType) { $this->subType = strtolower($subType); } public function getSubType() { return $this->subType; } public function setParam($name, $value) { if ($value === null) { unset($this->params[strtolower($name)]); } else { $this->params[strtolower($name)] = $value; } } public function getParam($name) { return isset($this->params[$name = strtolower($name)]) ? $this->params[$name] : null; } public function hasParam($name) { return isset($this->params[$name = strtolower($name)]); } public function getParams() { return $this->params; } } $f = new ContentTypeBuilder(new ContentTypeFactory); var_dump($f->build('text/html; test= "a test"; things=stuff ; q=0.8'));

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.60.0130.01016.85
8.3.50.0140.00721.95
8.3.40.0110.00418.95
8.3.30.0040.01119.34
8.3.20.0000.00920.45
8.3.10.0000.00823.52
8.3.00.0040.00419.51
8.2.180.0090.01318.16
8.2.170.0160.00022.96
8.2.160.0040.01120.51
8.2.150.0060.00324.18
8.2.140.0080.00024.66
8.2.130.0050.00326.16
8.2.120.0040.00422.25
8.2.110.0070.00322.23
8.2.100.0120.00018.03
8.2.90.0030.00519.32
8.2.80.0040.00417.97
8.2.70.0060.00317.88
8.2.60.0090.00018.05
8.2.50.0030.00618.07
8.2.40.0080.00019.93
8.2.30.0000.00818.41
8.2.20.0030.00617.97
8.2.10.0050.00318.27
8.2.00.0050.00318.00
8.1.280.0120.00625.92
8.1.270.0050.00323.96
8.1.260.0080.00026.35
8.1.250.0050.00328.09
8.1.240.0070.00323.92
8.1.230.0040.00819.09
8.1.220.0040.00417.93
8.1.210.0050.00318.77
8.1.200.0060.00317.48
8.1.190.0040.00417.48
8.1.180.0050.00518.10
8.1.170.0030.00618.67
8.1.160.0000.00722.18
8.1.150.0090.00018.81
8.1.140.0060.00317.62
8.1.130.0030.00517.99
8.1.120.0030.00517.61
8.1.110.0030.00517.54
8.1.100.0040.00417.67
8.1.90.0040.00417.50
8.1.80.0040.00417.65
8.1.70.0020.00517.54
8.1.60.0060.00317.75
8.1.50.0030.00617.71
8.1.40.0000.00817.67
8.1.30.0030.00517.66
8.1.20.0000.00817.82
8.1.10.0050.00317.73
8.1.00.0040.00417.68
8.0.300.0000.00820.65
8.0.290.0050.00317.13
8.0.280.0050.00618.62
8.0.270.0000.00717.39
8.0.260.0070.00017.51
8.0.250.0000.00717.16
8.0.240.0000.00717.13
8.0.230.0030.00517.23
8.0.220.0000.00717.15
8.0.210.0020.00517.00
8.0.200.0000.00717.08
8.0.190.0000.00917.02
8.0.180.0040.00417.04
8.0.170.0000.00817.05
8.0.160.0000.00817.24
8.0.150.0030.00617.03
8.0.140.0030.00517.10
8.0.130.0030.00313.58
8.0.120.0040.00417.10
8.0.110.0040.00417.14
8.0.100.0040.00417.14
8.0.90.0040.00416.91
8.0.80.0030.01217.14
8.0.70.0000.00817.17
8.0.60.0040.00417.08
8.0.50.0000.00817.24
8.0.30.0130.00717.37
8.0.20.0100.00917.40
8.0.10.0040.00417.02
8.0.00.0110.00816.83
7.4.330.0060.00015.15
7.4.320.0000.00716.86
7.4.300.0040.00416.82
7.4.290.0050.00316.70
7.4.280.0000.00816.66
7.4.270.0000.00716.66
7.4.260.0040.00416.80
7.4.250.0000.00816.63
7.4.240.0030.00516.73
7.4.230.0000.00716.86
7.4.220.0090.00916.63
7.4.210.0090.01116.69
7.4.200.0050.00316.78
7.4.190.0040.00416.86
7.4.160.0070.01016.82
7.4.150.0070.01417.40
7.4.140.0090.00917.86
7.4.130.0050.01216.74
7.4.120.0090.00916.78
7.4.110.0060.01116.58
7.4.100.0090.01216.62
7.4.90.0060.01416.65
7.4.80.0090.01219.39
7.4.70.0110.01116.69
7.4.60.0100.00916.60
7.4.50.0040.00416.80
7.4.40.0030.01316.63
7.4.30.0090.01016.69
7.4.00.0120.00315.40
7.3.330.0030.00313.45
7.3.320.0000.00613.60
7.3.310.0040.00416.48
7.3.300.0000.00716.60
7.3.290.0030.01016.59
7.3.280.0060.01316.61
7.3.270.0190.00017.40
7.3.260.0100.01016.61
7.3.250.0050.01816.70
7.3.240.0170.00716.66
7.3.230.0060.01216.80
7.3.210.0030.02216.69
7.3.200.0090.00919.39
7.3.190.0130.01016.75
7.3.180.0090.00916.79
7.3.170.0100.00716.65
7.3.160.0060.01616.53
7.3.120.0090.00915.17
7.2.330.0130.01016.96
7.2.320.0100.00716.82
7.2.310.0100.01416.75
7.2.300.0100.00816.76
7.2.290.0060.02016.97
7.2.60.0030.01216.98
7.1.200.0070.00715.90
7.1.100.0070.00717.78
7.1.70.0030.00917.34
7.1.60.0060.01919.46
7.1.50.0240.01434.93
7.1.00.0000.08022.41
7.0.200.0040.00416.78
7.0.140.0100.06722.11
7.0.100.0070.07020.03
7.0.90.0100.08320.01
7.0.80.0100.07320.07
7.0.70.0100.07320.07
7.0.60.0030.08720.02
7.0.50.0000.09320.41
7.0.40.0030.08720.10
7.0.30.0100.08320.04
7.0.20.0070.07720.08
7.0.10.0130.08019.95
7.0.00.0130.06320.09
5.6.280.0000.08021.21
5.6.250.0030.06320.75
5.6.240.0130.07720.75
5.6.230.0030.07020.76
5.6.220.0000.09020.52
5.6.210.0070.07320.65
5.6.200.0130.03721.17
5.6.190.0070.07321.06
5.6.180.0130.07721.07
5.6.170.0100.07321.16
5.6.160.0100.08321.14
5.6.150.0070.07721.14
5.6.140.0130.07021.07
5.6.130.0170.07021.07
5.6.120.0070.08021.07
5.6.110.0030.05321.02
5.6.100.0100.08021.18
5.6.90.0100.05021.03
5.6.80.0070.07320.52
5.6.70.0070.06320.34
5.6.60.0030.06020.48
5.6.50.0100.04020.58
5.6.40.0000.08320.46
5.6.30.0200.06720.48
5.6.20.0100.07320.40
5.6.10.0070.04720.40
5.6.00.0070.08720.32
5.5.380.0030.08020.46
5.5.370.0070.07320.48
5.5.360.0000.05320.44
5.5.350.0070.06320.46
5.5.340.0070.08320.94
5.5.330.0100.05020.91
5.5.320.0100.07720.91
5.5.310.0130.07320.67
5.5.300.0200.08020.91
5.5.290.0130.07320.86
5.5.280.0100.08720.96
5.5.270.0130.07320.73
5.5.260.0070.04720.78
5.5.250.0270.05720.45
5.5.240.0130.06720.22
5.5.230.0070.08320.01
5.5.220.0100.07320.27
5.5.210.0030.06020.15
5.5.200.0070.06719.98
5.5.190.0070.07720.30
5.5.180.0100.06720.21
5.5.160.0170.07020.02
5.5.150.0100.05720.18
5.5.140.0070.07320.14
5.5.130.0100.08020.30
5.5.120.0030.04320.17
5.5.110.0100.07320.25
5.5.100.0030.06720.08
5.5.90.0030.07320.10
5.5.80.0100.07320.14
5.5.70.0200.06720.09
5.5.60.0100.06720.07
5.5.50.0000.05320.09
5.5.40.0070.06020.10
5.5.30.0100.07720.16
5.5.20.0130.06720.03
5.5.10.0100.04720.17
5.5.00.0130.07720.14
5.4.450.0070.08019.37
5.4.440.0100.06019.41
5.4.430.0030.08319.28
5.4.420.0000.05019.38
5.4.410.0100.04319.33
5.4.400.0030.05018.87
5.4.390.0030.06318.90
5.4.380.0030.06019.07
5.4.370.0070.07319.10
5.4.360.0070.04019.23
5.4.350.0070.04318.90
5.4.340.0070.06719.06
5.4.320.0030.07019.17
5.4.310.0100.07019.23
5.4.300.0070.07719.13
5.4.290.0070.04719.13
5.4.280.0170.07019.23
5.4.270.0100.07319.13
5.4.260.0200.06319.09
5.4.250.0070.06718.98
5.4.240.0100.04319.09
5.4.230.0000.07718.87
5.4.220.0030.06718.90
5.4.210.0130.04719.05
5.4.200.0170.07019.13
5.4.190.0070.06319.04
5.4.180.0070.07719.17
5.4.170.0130.07719.05
5.4.160.0070.07319.07
5.4.150.0070.04319.20
5.4.140.0030.07316.46
5.4.130.0070.04016.50
5.4.120.0030.07716.46
5.4.110.0100.05016.34
5.4.100.0030.06316.54
5.4.90.0170.06316.45
5.4.80.0070.04016.51
5.4.70.0100.06316.50
5.4.60.0000.05316.55
5.4.50.0030.04316.52
5.4.40.0070.07716.43
5.4.30.0170.02316.50
5.4.20.0070.06716.36
5.4.10.0030.07316.46
5.4.00.0100.06715.91
5.3.290.0070.07714.69
5.3.280.0030.07314.62
5.3.270.0130.03014.66
5.3.260.0070.07714.66
5.3.250.0030.08014.71
5.3.240.0100.07314.58
5.3.230.0000.06014.58
5.3.220.0230.03314.64
5.3.210.0070.07714.58
5.3.200.0070.06714.66
5.3.190.0100.04314.63
5.3.180.0000.04714.50
5.3.170.0070.04314.59
5.3.160.0130.06014.54
5.3.150.0100.04014.63
5.3.140.0130.07014.49
5.3.130.0100.07314.50
5.3.120.0170.06014.50
5.3.110.0130.04314.62
5.3.100.0070.05714.17
5.3.90.0070.07314.04
5.3.80.0100.03714.08
5.3.70.0100.07314.11
5.3.60.0200.06013.92
5.3.50.0070.08013.86
5.3.40.0130.07013.96
5.3.30.0130.03313.93
5.3.20.0100.04713.68
5.3.10.0030.07313.79
5.3.00.0030.05713.77

preferences:
40.78 ms | 401 KiB | 5 Q