Skip to content

Commit

Permalink
[mms] Add Horde_Stream#substring().
Browse files Browse the repository at this point in the history
  • Loading branch information
slusarz committed Oct 1, 2013
1 parent 5057c8c commit 37e50eb
Show file tree
Hide file tree
Showing 3 changed files with 142 additions and 11 deletions.
74 changes: 63 additions & 11 deletions framework/Stream/lib/Horde/Stream.php
Expand Up @@ -89,7 +89,8 @@ public function __clone()
*/
public function __toString()
{
return $this->getString(0);
$this->rewind();
return $this->substring();
}

/**
Expand All @@ -115,7 +116,7 @@ public function add($data, $reset = false)
} elseif ($data instanceof Horde_Stream) {
$dpos = $data->pos();
while (!$data->eof()) {
$this->add($data->getString(null, $data->pos() + 65536));
$this->add($data->substring(0, 65536));
}
$data->seek($dpos, false);
} else {
Expand Down Expand Up @@ -149,7 +150,6 @@ public function length($utf8 = false)
++$len;
}
} else {

if (!$this->end()) {
throw new Horde_Stream_Exception('ERROR');
}
Expand Down Expand Up @@ -179,7 +179,7 @@ public function getToChar($end)
$res = $this->search($end);

if (is_null($res)) {
return $this->getString();
return $this->substring();
}

$len = strlen($end);
Expand Down Expand Up @@ -289,26 +289,78 @@ public function search($char, $reverse = false, $reset = true)
*/
public function getString($start = null, $end = null)
{
if (!is_null($start)) {
$this->seek($start, ($start < 0));
if (!is_null($start) && ($start >= 0)) {
$this->seek($start, false);
$start = 0;
}

if (is_null($end)) {
$len = 8192;
$len = null;
} else {
$end = ($end >= 0)
? $end - $this->pos() + 1
: $this->length() - $this->pos() + $end;
$len = max($end, 0);
}

return $this->substring($start, $len);
}

/**
* Return part of the stream as a string.
*
* @since 1.4.0
*
* @param integer $start Start, as an offset from the current postion.
* @param integer $length Length of string to return. If null, returns
* rest of the stream. If negative, this many
* characters will be omitted from the end of the
* stream.
* @param boolean $char If true, $start/$length is the length in
* characters. If false, $start/$length is the
* length in bytes.
*
* @return string The substring.
*/
public function substring($start = 0, $length = null, $char = false)
{
if ($start !== 0) {
$this->seek($start, true, $char);
}

$out = '';
$to_end = is_null($length);

/* If length is greater than remaining stream, use more efficient
* algorithm below. Also, if doing a negative length, deal with that
* below also. */
if ($char &&
$this->utf8_char &&
!$to_end &&
($length >= 0) &&
($length < ($this->length() - $this->pos()))) {
while ($length-- && (($char = $this->getChar()) !== false)) {
$out .= $char;
}
return $out;
}

if (!$to_end && ($length < 0)) {
$pos = $this->pos();
$this->end();
$this->seek($length, true, $char);
$length = $this->pos() - $pos;
$this->seek($pos, false);
if ($length < 0) {
return '';
}
}

while (!feof($this->stream) && (is_null($end) || $len)) {
$read = fread($this->stream, $len);
while (!feof($this->stream) && ($to_end || $length)) {
$read = fread($this->stream, $to_end ? 16384 : $length);
$out .= $read;
if (!is_null($end)) {
$len -= strlen($read);
if (!$to_end) {
$length -= strlen($read);
}
}

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] Add Horde_Stream#substring().
* [mms] Fix getString() in certain use cases.
* [mms] Horde_Stream#getToChar() now supports multiple characters in search string.
* [mms] Horde_Stream#search() now supports multiple characters in search string.
Expand Down Expand Up @@ -206,6 +207,7 @@
<date>2013-08-05</date>
<license uri="http://www.horde.org/licenses/lgpl21">LGPL-2.1</license>
<notes>
* [mms] Add Horde_Stream#substring().
* [mms] Fix getString() in certain use cases.
* [mms] Horde_Stream#getToChar() now supports multiple characters in search string.
* [mms] Horde_Stream#search() now supports multiple characters in search string.
Expand Down
77 changes: 77 additions & 0 deletions framework/Stream/test/Horde/Stream/TempTest.php
Expand Up @@ -545,4 +545,81 @@ public function testParsingAnExistingStreamObject()
);
}

public function testSubstring()
{
$stream = new Horde_Stream_Temp();
$stream->add('1234567890');
$stream->rewind();

$this->assertEquals(
'123',
$stream->substring(0, 3)
);
$this->assertEquals(
'456',
$stream->substring(0, 3)
);
$this->assertEquals(
'7890',
$stream->substring(0)
);
$this->assertEquals(
'',
$stream->substring(0, 3)
);

$stream->rewind();

$this->assertEquals(
'456',
$stream->substring(3, 3)
);
$this->assertEquals(
'',
$stream->substring(10, 3)
);

$stream->rewind();

$this->assertEquals(
'123',
$stream->substring(-3, 3)
);
$this->assertEquals(
'123',
$stream->substring(-3, 3)
);

$stream2 = new Horde_Stream_Temp();
$stream2->add('AönönAönön');
$stream2->utf8_char = true;
$stream2->rewind();

$this->assertEquals(
'Aö',
$stream2->substring(0, 3)
);

$stream2->rewind();

$this->assertEquals(
'Aön',
$stream2->substring(0, 3, true)
);

$stream2->rewind();

$this->assertEquals(
'AönönAönön',
$stream2->substring(0, null, true)
);

$stream2->rewind();

$this->assertEquals(
'Aönön',
$stream2->substring(5, null, true)
);
}

}

0 comments on commit 37e50eb

Please sign in to comment.