1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 |
- <?php
- /**
- * @package Kohana/Codebench
- * @category Tests
- * @author Geert De Deckere <geert@idoe.be>
- */
- class Bench_MDDoImageURL extends Codebench {
- public $description =
- 'Optimization for the <code>doImageURL()</code> method of <code>Kohana_Kodoc_Markdown</code>
- for the Kohana Userguide.';
- public $loops = 10000;
- public $subjects = [
- // Valid matches
- '',
- '',
- '',
- '',
- '![Alt text containing [square] brackets](img/install.png)',
- '![Empty src]()',
- // Invalid matches
- '
- {
- return preg_replace_callback('~!\[(.+?)\]\((\S*(?:\s*".+?")?)\)~', [$this, '_add_image_url_original'], $subject);
- }
- protected function _add_image_url_original($matches)
- {
- if ($matches[2] AND strpos($matches[2], '://') === FALSE)
- {
- // Add the base url to the link URL
- $matches[2] = 'http://BASE/'.$matches[2];
- }
- // Recreate the link
- return "![{$matches[1]}]({$matches[2]})";
- }
- public function bench_optimized_callback($subject)
- {
- // Moved the check for "://" to the regex, simplifying the callback function
- return preg_replace_callback('~!\[(.+?)\]\((?!\w++://)(\S*(?:\s*+".+?")?)\)~', [$this, '_add_image_url_optimized'], $subject);
- }
- protected function _add_image_url_optimized($matches)
- {
- // Add the base url to the link URL
- $matches[2] = 'http://BASE/'.$matches[2];
- // Recreate the link
- return "![{$matches[1]}]({$matches[2]})";
- }
- public function bench_callback_gone($subject)
- {
- // All the optimized callback was doing now, is prepend some text to the URL.
- // We don't need a callback for that, and that should be clearly faster.
- return preg_replace('~(!\[.+?\]\()(?!\w++://)(\S*(?:\s*+".+?")?\))~', '$1http://BASE/$2', $subject);
- }
- }
|