菜谱项目

Reader.php 2.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\CssSelector\Parser;
  11. /**
  12. * CSS selector reader.
  13. *
  14. * This component is a port of the Python cssselect library,
  15. * which is copyright Ian Bicking, @see https://github.com/SimonSapin/cssselect.
  16. *
  17. * @author Jean-François Simon <jeanfrancois.simon@sensiolabs.com>
  18. *
  19. * @internal
  20. */
  21. class Reader
  22. {
  23. private $source;
  24. private $length;
  25. private $position = 0;
  26. /**
  27. * @param string $source
  28. */
  29. public function __construct($source)
  30. {
  31. $this->source = $source;
  32. $this->length = strlen($source);
  33. }
  34. /**
  35. * @return bool
  36. */
  37. public function isEOF()
  38. {
  39. return $this->position >= $this->length;
  40. }
  41. /**
  42. * @return int
  43. */
  44. public function getPosition()
  45. {
  46. return $this->position;
  47. }
  48. /**
  49. * @return int
  50. */
  51. public function getRemainingLength()
  52. {
  53. return $this->length - $this->position;
  54. }
  55. /**
  56. * @param int $length
  57. * @param int $offset
  58. *
  59. * @return string
  60. */
  61. public function getSubstring($length, $offset = 0)
  62. {
  63. return substr($this->source, $this->position + $offset, $length);
  64. }
  65. /**
  66. * @param string $string
  67. *
  68. * @return int
  69. */
  70. public function getOffset($string)
  71. {
  72. $position = strpos($this->source, $string, $this->position);
  73. return false === $position ? false : $position - $this->position;
  74. }
  75. /**
  76. * @param string $pattern
  77. *
  78. * @return array|false
  79. */
  80. public function findPattern($pattern)
  81. {
  82. $source = substr($this->source, $this->position);
  83. if (preg_match($pattern, $source, $matches)) {
  84. return $matches;
  85. }
  86. return false;
  87. }
  88. /**
  89. * @param int $length
  90. */
  91. public function moveForward($length)
  92. {
  93. $this->position += $length;
  94. }
  95. public function moveToEnd()
  96. {
  97. $this->position = $this->length;
  98. }
  99. }