Skip to content

Commit

Permalink
[mms] Horde_Stream#search() now supports multiple characters in searc…
Browse files Browse the repository at this point in the history
…h string.
  • Loading branch information
slusarz committed Sep 25, 2013
1 parent 20b3b43 commit 5748a6d
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 20 deletions.
55 changes: 35 additions & 20 deletions framework/Stream/lib/Horde/Stream.php
Expand Up @@ -212,40 +212,55 @@ public function peek($length = 1)
}

/**
* Search for a character and return its position.
* Search for character(s) and return its position.
*
* @param string $char The character to search for.
* @param string $char The character to search for. As of 1.4.0,
* $char can be a multi-character string.
* @param boolean $reverse Do a reverse search?
* @param boolean $reset Reset the pointer to the original position?
*
* @return mixed The position (integer), or null if character not found.
* @return mixed The start position of the search string (integer), or
* null if character not found.
*/
public function search($char, $reverse = false, $reset = true)
{
$found_pos = null;

if (strlen($char)) {
$pos = $this->pos();

if ($reverse) {
for ($i = $pos - 1; $i >= 0; --$i) {
$this->seek($i, false);
if ($this->getChar() == $char) {
$found_pos = $i;
break;
if ($len = strlen($char)) {
do {
$pos = $this->pos();

if ($reverse) {
for ($i = $pos - 1; $i >= 0; --$i) {
$this->seek($i, false);
$c = $this->peek();
if ($c == substr($char, 0, strlen($c))) {
$found_pos = $i;
break;
}
}
}
} else {
while (($c = $this->getChar()) !== false) {
if ($c == $char) {
$found_pos = $this->pos() - 1;
break;
} else {
while (($c = $this->getChar()) !== false) {
if ($c == substr($char, 0, strlen($c))) {
$found_pos = $this->pos() - strlen($c);
break;
}
}
}
}

if (is_null($found_pos) ||
($len == 1) ||
($this->getString($found_pos, $found_pos + $len - 1) == $char)) {
break;
}

$this->seek($found_pos + ($reverse ? 0 : 1), false);
$found_pos = null;
} while (true);

$this->seek(
($reset || is_null($found_pos)) ? $pos : $found_pos, false
($reset || is_null($found_pos)) ? $pos : $found_pos,
false
);
}

Expand Down
2 changes: 2 additions & 0 deletions framework/Stream/package.xml
Expand Up @@ -22,6 +22,7 @@
</stability>
<license uri="http://www.horde.org/licenses/lgpl21">LGPL-2.1</license>
<notes>
* [mms] Horde_Stream#search() now supports multiple characters in search string.
* [mms] Add ability to get UTF-8 character length of stream from Horde_Stream#length().
* [mms] Add length argument to peek().
* [mms] Add Horde_Stream#close().
Expand Down Expand Up @@ -203,6 +204,7 @@
<date>2013-08-05</date>
<license uri="http://www.horde.org/licenses/lgpl21">LGPL-2.1</license>
<notes>
* [mms] Horde_Stream#search() now supports multiple characters in search string.
* [mms] Add ability to get UTF-8 character length of stream from Horde_Stream#length().
* [mms] Add length argument to peek().
* [mms] Add Horde_Stream#close().
Expand Down
23 changes: 23 additions & 0 deletions framework/Stream/test/Horde/Stream/TempTest.php
Expand Up @@ -273,6 +273,15 @@ public function testSearch()
3,
$stream->pos()
);

$stream->rewind();

$this->assertEquals(
3,
$stream->search('34')
);

$this->assertNull($stream->search('35'));
}

public function testAddMethod()
Expand Down Expand Up @@ -438,6 +447,20 @@ public function testUtf8Parsing()
'A',
$stream->getChar()
);

$stream->rewind();

$this->assertEquals(
1,
$stream->search('ön')
);

$stream->end();

$this->assertEquals(
4,
$stream->search('ön', true)
);
}

}

0 comments on commit 5748a6d

Please sign in to comment.