GruberURL.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <?php
  2. /**
  3. * @package koseven/Codebench
  4. * @category Tests
  5. * @author Geert De Deckere <geert@idoe.be>
  6. */
  7. class Bench_GruberURL extends Codebench {
  8. public $description =
  9. 'Optimization for http://daringfireball.net/2009/11/liberal_regex_for_matching_urls';
  10. public $loops = 10000;
  11. public $subjects = [
  12. 'http://foo.com/blah_blah',
  13. 'http://foo.com/blah_blah/',
  14. '(Something like http://foo.com/blah_blah)',
  15. 'http://foo.com/blah_blah_(wikipedia)',
  16. '(Something like http://foo.com/blah_blah_(wikipedia))',
  17. 'http://foo.com/blah_blah.',
  18. 'http://foo.com/blah_blah/.',
  19. '<http://foo.com/blah_blah>',
  20. '<http://foo.com/blah_blah/>',
  21. 'http://foo.com/blah_blah,',
  22. 'http://www.example.com/wpstyle/?p=364.',
  23. 'http://✪df.ws/e7l',
  24. 'rdar://1234',
  25. 'rdar:/1234',
  26. 'x-yojimbo-item://6303E4C1-xxxx-45A6-AB9D-3A908F59AE0E',
  27. 'message://%3c330e7f8409726r6a4ba78dkf1fd71420c1bf6ff@mail.gmail.com%3e',
  28. 'http://➡.ws/䨹',
  29. 'www.➡.ws/䨹',
  30. '<tag>http://example.com</tag>',
  31. 'Just a www.example.com link.',
  32. // To test the use of possessive quatifiers:
  33. 'httpppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp',
  34. ];
  35. public function bench_daringfireball($subject)
  36. {
  37. // Original regex by John Gruber
  38. preg_match('~\b(([\w-]+://?|www[.])[^\s()<>]+(?:\([\w\d]+\)|([^[:punct:]\s]|/)))~', $subject, $matches);
  39. return (empty($matches)) ? FALSE : $matches[0];
  40. }
  41. public function bench_daringfireball_v2($subject)
  42. {
  43. // Removed outer capturing parentheses, made another pair non-capturing
  44. preg_match('~\b(?:[\w-]+://?|www[.])[^\s()<>]+(?:\([\w\d]+\)|(?:[^[:punct:]\s]|/))~', $subject, $matches);
  45. return (empty($matches)) ? FALSE : $matches[0];
  46. }
  47. public function bench_daringfireball_v3($subject)
  48. {
  49. // Made quantifiers possessive where possible
  50. preg_match('~\b(?:[\w-]++://?+|www[.])[^\s()<>]+(?:\([\w\d]++\)|(?:[^[:punct:]\s]|/))~', $subject, $matches);
  51. return (empty($matches)) ? FALSE : $matches[0];
  52. }
  53. }