ArrCallback.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. <?php
  2. /**
  3. * @package Kohana/Codebench
  4. * @category Tests
  5. * @author Geert De Deckere <geert@idoe.be>
  6. */
  7. class Bench_ArrCallback extends Codebench {
  8. public $description =
  9. 'Parsing <em>command[param,param]</em> strings in <code>Arr::callback()</code>:
  10. http://github.com/shadowhand/kohana/commit/c3aaae849164bf92a486e29e736a265b350cb4da#L0R127';
  11. public $loops = 10000;
  12. public $subjects = [
  13. // Valid callback strings
  14. 'foo',
  15. 'foo::bar',
  16. 'foo[apple,orange]',
  17. 'foo::bar[apple,orange]',
  18. '[apple,orange]', // no command, only params
  19. 'foo[[apple],[orange]]', // params with brackets inside
  20. // Invalid callback strings
  21. 'foo[apple,orange', // no closing bracket
  22. ];
  23. public function bench_shadowhand($subject)
  24. {
  25. // The original regex we're trying to optimize
  26. if (preg_match('/([^\[]*+)\[(.*)\]/', $subject, $match))
  27. return $match;
  28. }
  29. public function bench_geert_regex_1($subject)
  30. {
  31. // Added ^ and $ around the whole pattern
  32. if (preg_match('/^([^\[]*+)\[(.*)\]$/', $subject, $matches))
  33. return $matches;
  34. }
  35. public function bench_geert_regex_2($subject)
  36. {
  37. // A rather experimental approach using \K which requires PCRE 7.2 ~ PHP 5.2.4
  38. // Note: $matches[0] = params, $matches[1] = command
  39. if (preg_match('/^([^\[]*+)\[\K.*(?=\]$)/', $subject, $matches))
  40. return $matches;
  41. }
  42. public function bench_geert_str($subject)
  43. {
  44. // A native string function approach which beats all the regexes
  45. if (strpos($subject, '[') !== FALSE AND substr($subject, -1) === ']')
  46. return explode('[', substr($subject, 0, -1), 2);
  47. }
  48. }