@ 2017-11-17T23:42:55Z <?php
$charmap = [
'NUL' => "\x00", // NULL (U+0000)
'SOH' => "\x01", // START OF HEADING (U+0001)
'STX' => "\x02", // START OF TEXT (U+0002)
'ETX' => "\x03", // END OF TEXT (U+0003)
'EOT' => "\x04", // END OF TRANSMISSION (U+0004)
'ENQ' => "\x05", // ENQUIRY (U+0005)
'HT' => "\x09", // HORIZONTAL TAB (U+0009)
'LF' => "\x0a", // LINE FEED (U+000A)
'VT' => "\x0b", // VERTICAL TAB (U+000B)
'CR' => "\x0d", // CARRIAGE RETURN (U+000D)
'ETB' => "\x17", // END OF TRANSMISSION BLOCK (U+0017)
'SP' => "\x20", // SPACE (U+0020)
'ZWS' => "\xe2\x80\x8b", // ZERO WIDTH SPACE (U+200B)
'MSBS' => "\xf0\x9d\x85\xb7", // MUSICAL SYMBOL BEGIN SLUR (U+1D177)
'MSBP' => "\xf0\x9d\x85\xb9", // MUSICAL SYMBOL BEGIN PHRASE (U+1D179)
];
foreach ($charmap as $k => $v) {
define($k, $v);
}
$strings = [
'user6003859' =>
LF .
LF .
LF .
'a' . SP . 'b'. SP . SP . 'c' . SP . SP . SP . LF .
SP . 'd' . SP . SP . SP . SP . 'e' . LF .
MSBS . 'f' . MSBS . 'g' . MSBS . MSBS . 'h' . MSBS . MSBS . ZWS . ZWS . MSBP . MSBP . 'i' . MSBP . SP . MSBP . SP . 'j' . LF .
LF .
LF .
LF .
LF .
'k' . SP . 'l' . SP . 'm' . SP . 'n' . SP . SP . SP . LF .
MSBP . 'o' . MSBP . MSBP . 'p' . LF .
LF .
LF .
LF,
'mickmackusa' =>
NUL . LF .
LF .
SOH . LF .
CR . LF .
VT .
ETB . 'a' . SP . 'ab' . CR . LF .
HT . HT . CR .
CR . LF .
'cà' . SOH . 'ê߀' . NUL . NUL . 'abcbc'. SP . SP . SP . 'd' . LF .
LF .
HT . CR . LF .
ENQ . SP . SP . SP . 'e' . STX . LF .
ETX . LF .
EOT . LF
];
function display($str, $charmap) {
$converter = array_map(function ($i) { return '{'.$i.'}'; }, array_flip($charmap));
$handle = fopen("data:text/plain,$str", 'r');
while ( false !== $line = fgets($handle) ) {
echo strtr($line, $converter), PHP_EOL;
}
fclose($handle);
}
class Replacements {
const FUNC = 0;
const REGEX = 1;
protected $patterns;
protected $replacements;
protected $func;
protected $typeRegex;
public function __construct($arg) {
if ( is_array($arg) ) {
$this->type = self::REGEX;
$this->patterns = [];
$this->replacements = [];
$this->addPatterns($arg);
} elseif ( is_callable($arg) ) {
$this->type = self::FUNC;
$this->addFunction($arg);
} else throw new Exception('invalid argument type');
}
protected function addPatterns($replacements) {
foreach($replacements as $pattern => $replacement) {
$this->patterns[] = $pattern;
$this->replacements[] = $replacement;
}
}
protected function addFunction($func) {
$this->func = $func;
}
public function execute($str) {
if ( $this->type === self::REGEX )
return preg_replace($this->patterns, $this->replacements, $str);
return call_user_func_array($this->func, [&$str]);
}
};
$original = new Replacements([
'~\R~Su' => "\n",
'/(?:^((\pZ)+|((?!\n)\pC)+)(?1)*)|((?1)$)|(?:((?2)+|(?3)+)(?=(?2)|(?3)))/um' => '',
'/(\pZ+)|((?!\n)\pC)/u' => ' ',
'/(^\n+)|(\n+$)|(\n(?=\n{2}))/u' => ''
]);
$new = new Replacements([
'~\r\n?|\x0b|\f|\xe2\x80[\xa8\xa9]~S' => "\n",
'~
[^\pZ\pC]+ \K
\pZ* (?:[^\PC\n]+\pZ*)*
(?: (\n) \pZ*+ (?:[^\PC\n]+\pZ*)*+ (?: (\n) [\pZ\pC]* )?+ (?!\z) | [\pZ\pC]+ )?
|
[\pZ\pC]+
~Aux' => '$1$2 ',
'~ (?:$|(?<=^ ))~m' => ''
]);
$simple = new Replacements([
'~\A[\pZ\pC]+|[\pZ\pC]+\z~u' => '', # trim the string
'~\R~u' => "\n", # normalize newlines
'~\pZ+|[^\n\PC]+~u' => ' ', # replace Z and C with space
'~^ +| +$| \K +~m' => '', # trim lines, delete consecutive spaces
'~\n\n\K\n+~' => '' # removes more than 2 consecutives newlines
]);
// tests
$tests = ['original' => $original, 'new' => $new, 'simple' => $simple];
$str = $strings['user6003859'] . $strings['mickmackusa'];
$str = str_repeat($str, 10);
$res = [];
foreach ($tests as $k=>&$test) {
$res[$k] = $test->execute($str);
}
echo 'same result: ', var_dump(count(array_unique($res)) === 1);
$names = array_keys($tests);
$times = array_fill_keys($names, 0);
define('REPETITIONS', 100);
for ($i=0; $i < REPETITIONS; $i++) {
shuffle($names);
foreach ($names as $name) {
$start = microtime(true);
$tests[$name]->execute($str);
$stop = microtime(true);
$times[$name] += $stop - $start;
}
}
foreach($times as $k=>$v) {
printf("%-12s: %.2es\n", $k, $v/REPETITIONS);
}
// display($res['new'], $charmap);
Enable javascript to submit You have javascript disabled. You will not be able to edit any code.
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).
Version System time (s) User time (s) Memory (MiB) 7.3.1 0.010 0.027 16.84 7.3.0 0.010 0.029 16.72 7.2.13 0.003 0.031 17.10 7.2.12 0.007 0.033 17.23 7.2.11 0.003 0.033 17.18 7.2.10 0.004 0.029 17.19 7.2.9 0.010 0.020 17.13 7.2.8 0.000 0.037 17.09 7.2.7 0.013 0.023 17.18 7.2.6 0.007 0.033 17.11 7.2.5 0.003 0.033 17.38 7.2.4 0.007 0.031 17.30 7.2.3 0.003 0.030 17.27 7.2.2 0.003 0.042 17.19 7.2.1 0.009 0.028 17.29 7.2.0 0.009 0.025 17.31 7.1.25 0.000 0.049 16.25 7.1.11 0.007 0.041 18.55 7.1.10 0.021 0.046 18.23 7.1.9 0.007 0.043 18.22 7.1.8 0.000 0.047 18.48 7.1.7 0.006 0.041 17.43 7.1.6 0.023 0.039 34.97 7.1.5 0.017 0.042 34.65 7.1.4 0.022 0.038 34.20 7.1.3 0.021 0.041 34.30 7.1.2 0.014 0.048 34.35 7.1.1 0.006 0.039 16.96 7.1.0 0.007 0.039 16.98 7.0.25 0.009 0.038 17.82 7.0.24 0.003 0.043 17.98 7.0.23 0.010 0.038 17.75 7.0.22 0.003 0.043 18.16 7.0.21 0.000 0.044 16.52 7.0.20 0.007 0.042 17.03 7.0.19 0.003 0.041 16.87 7.0.18 0.007 0.040 16.53 7.0.17 0.007 0.039 16.60 7.0.16 0.007 0.041 16.61 7.0.15 0.003 0.043 16.36 7.0.14 0.006 0.039 16.80 7.0.13 0.006 0.039 16.70 7.0.12 0.013 0.032 16.83 7.0.11 0.003 0.041 16.71 7.0.10 0.006 0.039 16.64 7.0.9 0.006 0.042 16.76 7.0.8 0.013 0.059 16.77 7.0.7 0.007 0.045 16.64 7.0.6 0.010 0.038 16.47 7.0.5 0.007 0.041 16.64 7.0.4 0.003 0.054 16.87 7.0.3 0.000 0.050 16.80 7.0.2 0.012 0.037 16.71 7.0.1 0.020 0.059 16.86 7.0.0 0.006 0.048 16.71
preferences:dark mode live preview ace vim emacs key bindings
28.04 ms | 403 KiB | 5 Q