Skip to content
This repository has been archived by the owner on Jan 8, 2021. It is now read-only.

Commit

Permalink
fix #18 u::wordwrap() now relies on native behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolas-grekas committed Mar 26, 2014
1 parent aea0de8 commit f45ba8b
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 45 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## v1.1.21 (2014-03-26)

- fix #18 u::wordwrap() now relies on native behavior

## v1.1.20 (2014-03-01)
## v1.1.19 (2014-03-01)

- fix mb_regex_encoding() being disabled on some hosting providers
Expand Down
79 changes: 34 additions & 45 deletions class/Patchwork/Utf8.php
Original file line number Diff line number Diff line change
Expand Up @@ -257,65 +257,54 @@ static function strtoupper($s) {return mb_strtoupper($s, 'UTF-8');}

static function wordwrap($s, $width = 75, $break = "\n", $cut = false)
{
// This implementation could be extended to handle unicode word boundaries,
// but that's enough work for today (see http://www.unicode.org/reports/tr29/)
if (false === wordwrap('-', $width, $break, $cut)) return false;

$width = (int) $width;
$s = explode($break, $s);
is_string($break) or $break = (string) $break;

$w = '';
$s = explode($break, $s);
$iLen = count($s);
$result = array();
$line = '';
$lineLen = 0;
$chars = array();

if (1 === $iLen && '' === $s[0])
return '';

for ($i = 0; $i < $iLen; ++$i)
{
$words = explode(' ', $s[$i]);
$line && $result[] = $line;
$lineLen = grapheme_strlen($line);
$jLen = count($words);

for ($j = 0; $j < $jLen; ++$j)
if ($i)
{
$w = $words[$j];
$wLen = grapheme_strlen($w);

if ($lineLen + $wLen < $width)
{
if ($j) $line .= ' ';
$line .= $w;
$lineLen += $wLen + 1;
}
else
{
if ($j || $i) $result[] = $line;
$line = '';
$lineLen = 0;
$chars[] = $break;
$w .= '#';
}

if ($cut && $wLen > $width)
{
$w = self::str_split($w);
$c = $s[$i];
unset($s[$i]);

do
{
$result[] = implode('', array_slice($w, 0, $width));
$line = implode('', $w = array_slice($w, $width));
$lineLen = $wLen -= $width;
}
while ($wLen > $width);
foreach (self::str_split($c) as $c)
{
$chars[] = $c;
$w .= ' ' === $c ? ' ' : '?';
}
}

$w = implode('', $w);
}
$s = '';
$j = 0;
$b = $i = -1;
$w = wordwrap($w, $width, '#', $cut);

$line = $w;
$lineLen = $wLen;
}
while (false !== $b = strpos($w, '#', $b+1))
{
for (++$i; $i < $b; ++$i)
{
$s .= $chars[$j];
unset($chars[$j++]);
}
}

$line && $result[] = $line;
if ($break === $chars[$j] || ' ' === $chars[$j]) unset($chars[$j++]);
$s .= $break;
}

return implode($break, $result);
return $s . implode('', $chars);
}

static function chr($c)
Expand Down
6 changes: 6 additions & 0 deletions tests/Patchwork/Tests/Utf8Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,12 @@ function testStrstr()
*/
function testWordwrap()
{
$text = "string\nwith\nnew\nlines";
$this->assertSame( $text, u::wordwrap($text) );

$text = "a #b";
$this->assertSame( wordwrap($text, 2, '#', false), u::wordwrap($text, 2, '#', false) );

$text = 'A very long woooooooooooord.';

$this->assertSame( wordwrap($text, 8, "\n", false), u::wordwrap($text, 8, "\n", false) );
Expand Down

0 comments on commit f45ba8b

Please sign in to comment.