ValidColor.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. /**
  3. * @package koseven/Codebench
  4. * @category Tests
  5. * @author Geert De Deckere <geert@idoe.be>
  6. */
  7. class Bench_ValidColor extends Codebench {
  8. public $description =
  9. 'Optimization for <code>Validate::color()</code>.
  10. See: http://koseven.discourse.group/comments.php?DiscussionID=2192.
  11. Note that the methods with an <em>_invalid</em> suffix contain flawed regexes and should be
  12. completely discarded. I left them in here for educational purposes, and to remind myself
  13. to think harder and test more thoroughly. It can\'t be that I only found out so late in
  14. the game. For the regex explanation have a look at the forum topic mentioned earlier.';
  15. public $loops = 10000;
  16. public $subjects = [
  17. // Valid colors
  18. 'aaA',
  19. '123',
  20. '000000',
  21. '#123456',
  22. '#abcdef',
  23. // Invalid colors
  24. 'ggg',
  25. '1234',
  26. '#1234567',
  27. "#000\n",
  28. '}§è!çà%$z',
  29. ];
  30. // Note that I added the D modifier to corey's regexes. We need to match exactly
  31. // the same if we want the benchmarks to be of any value.
  32. public function bench_corey_regex_1_invalid($subject)
  33. {
  34. return (bool) preg_match('/^#?([0-9a-f]{1,2}){3}$/iD', $subject);
  35. }
  36. public function bench_corey_regex_2($subject)
  37. {
  38. return (bool) preg_match('/^#?([0-9a-f]){3}(([0-9a-f]){3})?$/iD', $subject);
  39. }
  40. // Optimized corey_regex_1
  41. // Using non-capturing parentheses and a possessive interval
  42. public function bench_geert_regex_1a_invalid($subject)
  43. {
  44. return (bool) preg_match('/^#?(?:[0-9a-f]{1,2}+){3}$/iD', $subject);
  45. }
  46. // Optimized corey_regex_2
  47. // Removed useless parentheses, made the remaining ones non-capturing
  48. public function bench_geert_regex_2a($subject)
  49. {
  50. return (bool) preg_match('/^#?[0-9a-f]{3}(?:[0-9a-f]{3})?$/iD', $subject);
  51. }
  52. // Optimized geert_regex_1a
  53. // Possessive "#"
  54. public function bench_geert_regex_1b_invalid($subject)
  55. {
  56. return (bool) preg_match('/^#?+(?:[0-9a-f]{1,2}+){3}$/iD', $subject);
  57. }
  58. // Optimized geert_regex_2a
  59. // Possessive "#"
  60. public function bench_geert_regex_2b($subject)
  61. {
  62. return (bool) preg_match('/^#?+[0-9a-f]{3}(?:[0-9a-f]{3})?$/iD', $subject);
  63. }
  64. // Using \z instead of $
  65. public function bench_salathe_regex_1($subject)
  66. {
  67. return (bool) preg_match('/^#?+[0-9a-f]{3}(?:[0-9a-f]{3})?\z/i', $subject);
  68. }
  69. // Using \A instead of ^
  70. public function bench_salathe_regex_2($subject)
  71. {
  72. return (bool) preg_match('/\A#?+[0-9a-f]{3}(?:[0-9a-f]{3})?\z/i', $subject);
  73. }
  74. // A solution without regex
  75. public function bench_geert_str($subject)
  76. {
  77. if ($subject[0] === '#')
  78. {
  79. $subject = substr($subject, 1);
  80. }
  81. $strlen = strlen($subject);
  82. return (($strlen === 3 OR $strlen === 6) AND ctype_xdigit($subject));
  83. }
  84. // An ugly, but fast, solution without regex
  85. public function bench_salathe_str($subject)
  86. {
  87. if ($subject[0] === '#')
  88. {
  89. $subject = substr($subject, 1);
  90. }
  91. // TRUE if:
  92. // 1. $subject is 6 or 3 chars long
  93. // 2. $subject contains only hexadecimal digits
  94. return (((isset($subject[5]) AND ! isset($subject[6])) OR
  95. (isset($subject[2]) AND ! isset($subject[3])))
  96. AND ctype_xdigit($subject));
  97. }
  98. }