MDDoBaseURL.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <?php
  2. /**
  3. * @package koseven/Codebench
  4. * @category Tests
  5. * @author Geert De Deckere <geert@idoe.be>
  6. */
  7. class Bench_MDDoBaseURL extends Codebench {
  8. public $description =
  9. 'Optimization for the <code>doBaseURL()</code> method of <code>KO7_Kodoc_Markdown</code>
  10. for the KO7 Userguide.';
  11. public $loops = 10000;
  12. public $subjects = [
  13. // Valid matches
  14. '[filesystem](about.filesystem)',
  15. '[filesystem](about.filesystem "Optional title")',
  16. '[same page link](#id)',
  17. '[object oriented](http://wikipedia.org/wiki/Object-Oriented_Programming)',
  18. // Invalid matches
  19. '![this is image syntax](about.filesystem)',
  20. '[filesystem](about.filesystem',
  21. ];
  22. public function bench_original($subject)
  23. {
  24. // The original regex contained a bug, which is fixed here for benchmarking purposes.
  25. // At the very start of the regex, (?!!) has been replace by (?<!!)
  26. return preg_replace_callback('~(?<!!)\[(.+?)\]\(([^#]\S*(?:\s*".+?")?)\)~', [$this, '_add_base_url_original'], $subject);
  27. }
  28. public function _add_base_url_original($matches)
  29. {
  30. if ($matches[2] AND strpos($matches[2], '://') === FALSE)
  31. {
  32. // Add the base url to the link URL
  33. $matches[2] = 'http://BASE/'.$matches[2];
  34. }
  35. // Recreate the link
  36. return "[{$matches[1]}]({$matches[2]})";
  37. }
  38. public function bench_optimized_callback($subject)
  39. {
  40. return preg_replace_callback('~(?<!!)\[(.+?)\]\((?!\w++://)([^#]\S*(?:\s*+".+?")?)\)~', [$this, '_add_base_url_optimized'], $subject);
  41. }
  42. public function _add_base_url_optimized($matches)
  43. {
  44. // Add the base url to the link URL
  45. $matches[2] = 'http://BASE/'.$matches[2];
  46. // Recreate the link
  47. return "[{$matches[1]}]({$matches[2]})";
  48. }
  49. public function bench_callback_gone($subject)
  50. {
  51. // All the optimized callback was doing now, is prepend some text to the URL.
  52. // We don't need a callback for that, and that should be clearly faster.
  53. return preg_replace('~(?<!!)(\[.+?\]\()(?!\w++://)([^#]\S*(?:\s*+".+?")?\))~', '$1http://BASE/$2', $subject);
  54. }
  55. }