Skip to content

Commit

Permalink
[mms] Add ability to get UTF-8 character length of stream from Horde_…
Browse files Browse the repository at this point in the history
…Stream#length().
  • Loading branch information
slusarz committed Sep 25, 2013
1 parent fe19a38 commit 70490f9
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 21 deletions.
23 changes: 18 additions & 5 deletions framework/Stream/lib/Horde/Stream.php
Expand Up @@ -126,19 +126,32 @@ public function add($data, $reset = false)
/**
* Returns the length of the data. Does not change the stream position.
*
* @param boolean $utf8 If true, determines the UTF-8 length of the
* stream (as of 1.4.0). If false, determines the
* byte length of the stream.
*
* @return integer Stream size.
*
* @throws Horde_Stream_Exception
*/
public function length()
public function length($utf8 = false)
{
$pos = $this->pos();

if (!$this->end()) {
throw new Horde_Stream_Exception('ERROR');
}
if ($utf8 && $this->utf8_char) {
$this->rewind();
$len = 0;
while ($this->getChar() !== false) {
++$len;
}
} else {

$len = $this->pos();
if (!$this->end()) {
throw new Horde_Stream_Exception('ERROR');
}

$len = $this->pos();
}

if (!$this->seek($pos, false)) {
throw new Horde_Stream_Exception('ERROR');
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 ability to get UTF-8 character length of stream from Horde_Stream#length().
* [mms] Add length argument to peek().
* [mms] Add Horde_Stream#close().
* [mms] Add support for parsing UTF-8 characters in stream via Horde_Stream#getChar().
Expand Down Expand Up @@ -202,6 +203,7 @@
<date>2013-08-05</date>
<license uri="http://www.horde.org/licenses/lgpl21">LGPL-2.1</license>
<notes>
* [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().
* [mms] Add support for parsing UTF-8 characters in stream via Horde_Stream#getChar().
Expand Down
18 changes: 2 additions & 16 deletions framework/Stream/test/Horde/Stream/TempTest.php
Expand Up @@ -404,18 +404,11 @@ public function testUtf8Parsing()
$stream = new Horde_Stream_Temp();
$stream->add($test, true);

$i = 0;
while ($stream->getChar() !== false) {
++$i;
}

$this->assertEquals(
7,
$i
$stream->length()
);

$stream->rewind();

$this->assertEquals(
$test,
$stream->getToChar('ö')
Expand All @@ -425,18 +418,11 @@ public function testUtf8Parsing()
$stream->add($test, true);
$stream->utf8_char = true;

$i = 0;
while ($stream->getChar() !== false) {
++$i;
}

$this->assertEquals(
5,
$i
$stream->length(true)
);

$stream->rewind();

$this->assertEquals(
'Aö',
$stream->getToChar('n')
Expand Down

0 comments on commit 70490f9

Please sign in to comment.