ExplodeIterator.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. /**
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation; either version 2 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License along
  14. * with this program; if not, write to the Free Software Foundation, Inc.,
  15. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  16. * http://www.gnu.org/copyleft/gpl.html
  17. *
  18. * @file
  19. */
  20. /**
  21. * An iterator which works exactly like:
  22. *
  23. * foreach ( explode( $delim, $s ) as $element ) {
  24. * ...
  25. * }
  26. *
  27. * Except it doesn't use 193 byte per element
  28. */
  29. class ExplodeIterator implements Iterator {
  30. // The subject string
  31. private $subject, $subjectLength;
  32. // The delimiter
  33. private $delim, $delimLength;
  34. // The position of the start of the line
  35. private $curPos;
  36. // The position after the end of the next delimiter
  37. private $endPos;
  38. /** @var string|false The current token */
  39. private $current;
  40. /**
  41. * Construct a DelimIterator
  42. * @param string $delim
  43. * @param string $subject
  44. */
  45. public function __construct( $delim, $subject ) {
  46. $this->subject = $subject;
  47. $this->delim = $delim;
  48. // Micro-optimisation (theoretical)
  49. $this->subjectLength = strlen( $subject );
  50. $this->delimLength = strlen( $delim );
  51. $this->rewind();
  52. }
  53. public function rewind() {
  54. $this->curPos = 0;
  55. $this->endPos = strpos( $this->subject, $this->delim );
  56. $this->refreshCurrent();
  57. }
  58. public function refreshCurrent() {
  59. if ( $this->curPos === false ) {
  60. $this->current = false;
  61. } elseif ( $this->curPos >= $this->subjectLength ) {
  62. $this->current = '';
  63. } elseif ( $this->endPos === false ) {
  64. $this->current = substr( $this->subject, $this->curPos );
  65. } else {
  66. $this->current = substr( $this->subject, $this->curPos, $this->endPos - $this->curPos );
  67. }
  68. }
  69. public function current() {
  70. return $this->current;
  71. }
  72. /**
  73. * @return int|bool Current position or boolean false if invalid
  74. */
  75. public function key() {
  76. return $this->curPos;
  77. }
  78. /**
  79. * @return void
  80. */
  81. public function next() {
  82. if ( $this->endPos === false ) {
  83. $this->curPos = false;
  84. } else {
  85. $this->curPos = $this->endPos + $this->delimLength;
  86. if ( $this->curPos >= $this->subjectLength ) {
  87. $this->endPos = false;
  88. } else {
  89. $this->endPos = strpos( $this->subject, $this->delim, $this->curPos );
  90. }
  91. }
  92. $this->refreshCurrent();
  93. }
  94. /**
  95. * @return bool
  96. */
  97. public function valid() {
  98. return $this->curPos !== false;
  99. }
  100. }