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

Commit

Permalink
Merge branch 'master' of git://git.zendframework.com/zf
Browse files Browse the repository at this point in the history
  • Loading branch information
mikaelkael committed Jul 15, 2010
3 parents 430ee32 + eff12bd + c9dfc6a commit aa8cf19
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 33 deletions.
70 changes: 41 additions & 29 deletions src/MultiByte.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,46 +46,58 @@ class MultiByte
*/
public static function wordWrap($string, $width = 75, $break = "\n", $cut = false, $charset = 'UTF-8')
{
$result = array();
$result = array();
$breakWidth = iconv_strlen($break, $charset);

while (($stringLength = iconv_strlen($string, $charset)) > 0) {
$subString = iconv_substr($string, 0, $width, $charset);
$breakPos = iconv_strpos($string, $break, 0, $charset);

if ($subString === $string) {
$cutLength = null;
if ($breakPos !== false && $breakPos < $width) {
if ($breakPos === $stringLength - $breakWidth) {
$subString = $string;
$cutLength = null;
} else {
$subString = iconv_substr($string, 0, $breakPos, $charset);
$cutLength = $breakPos + $breakWidth;
}
} else {
$nextChar = iconv_substr($string, $width, 1, $charset);
$subString = iconv_substr($string, 0, $width, $charset);

if ($nextChar === ' ' || $nextChar === $break) {
$afterNextChar = iconv_substr($string, $width + 1, 1, $charset);

if ($afterNextChar === false) {
$subString .= $nextChar;
}

$cutLength = iconv_strlen($subString, $charset) + 1;
if ($subString === $string) {
$cutLength = null;
} else {
$spacePos = iconv_strrpos($subString, ' ', $charset);
$nextChar = iconv_substr($string, $width, 1, $charset);

if ($breakWidth === 1) {
$nextBreak = $nextChar;
} else {
$nextBreak = iconv_substr($string, $breakWidth, 1, $charset);
}

if ($spacePos !== false) {
$subString = iconv_substr($subString, 0, $spacePos, $charset);
$cutLength = $spacePos + 1;
} else if ($cut === false) {
$spacePos = iconv_strpos($string, ' ', 0, $charset);
if ($nextChar === ' ' || $nextBreak === $break) {
$afterNextChar = iconv_substr($string, $width + 1, 1, $charset);

if ($spacePos !== false) {
$subString = iconv_substr($string, 0, $spacePos, $charset);
$cutLength = $spacePos + 1;
} else {
$subString = $string;
$cutLength = null;
if ($afterNextChar === false) {
$subString .= $nextChar;
}

$cutLength = iconv_strlen($subString, $charset) + 1;
} else {
$breakPos = iconv_strpos($subString, $break, 0, $charset);
$spacePos = iconv_strrpos($subString, ' ', $charset);

if ($breakPos !== false) {
$subString = iconv_substr($subString, 0, $breakPos, $charset);
$cutLength = $breakPos + 1;
if ($spacePos !== false) {
$subString = iconv_substr($subString, 0, $spacePos, $charset);
$cutLength = $spacePos + 1;
} else if ($cut === false) {
$spacePos = iconv_strpos($string, ' ', 0, $charset);

if ($spacePos !== false) {
$subString = iconv_substr($string, 0, $spacePos, $charset);
$cutLength = $spacePos + 1;
} else {
$subString = $string;
$cutLength = null;
}
} else {
$subString = iconv_substr($subString, 0, $width, $charset);
$cutLength = $width;
Expand Down
8 changes: 4 additions & 4 deletions test/FigletTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ public function testIncorrectEncoding()
public function testNonExistentFont()
{
try {
$figlet = new Figlet\Figlet(array('font' => dirname(__FILE__) . '/Figlet/NonExistentFont.flf'));
$figlet = new Figlet\Figlet(array('font' => __DIR__ . '/Figlet/NonExistentFont.flf'));
$this->fail('An expected Zend_Text_Figlet_Exception has not been raised');
} catch (Figlet\Exception $expected) {
$this->assertContains('Font file not found', $expected->getMessage());
Expand All @@ -143,7 +143,7 @@ public function testNonExistentFont()
public function testInvalidFont()
{
try {
$figlet = new Figlet\Figlet(array('font' => dirname(__FILE__) . '/Figlet/InvalidFont.flf'));
$figlet = new Figlet\Figlet(array('font' => __DIR__ . '/Figlet/InvalidFont.flf'));
$this->fail('An expected Zend_Text_Figlet_Exception has not been raised');
} catch (Figlet\Exception $expected) {
$this->assertContains('Not a FIGlet 2 font file', $expected->getMessage());
Expand All @@ -152,7 +152,7 @@ public function testInvalidFont()

public function testGzippedFont()
{
$figlet = new Figlet\Figlet(array('font' => dirname(__FILE__) . '/Figlet/GzippedFont.gz'));
$figlet = new Figlet\Figlet(array('font' => __DIR__ . '/Figlet/GzippedFont.gz'));
$this->_equalAgainstFile($figlet->render('Dummy'), 'StandardAlignLeft.figlet');
}

Expand Down Expand Up @@ -266,7 +266,7 @@ public function testEmptyString()

protected function _equalAgainstFile($output, $file)
{
$compareString = file_get_contents(dirname(__FILE__) . '/Figlet/' . $file);
$compareString = file_get_contents(__DIR__ . '/Figlet/' . $file);

$this->assertEquals($compareString, $output);
}
Expand Down
15 changes: 15 additions & 0 deletions test/MultiByteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,21 @@ public function testWordWrapCutMultiLineShortWords()
$this->assertEquals("Ä very\nlong\nwööööööö\nööööörd.", $line);
}

public function testWordWrapCutMultiLineWithPreviousNewlines()
{
$line = Zend_Text_MultiByte::wordWrap("Ä very\nlong wöööööööööööörd.", 8, "\n", false);
$this->assertEquals("Ä very\nlong\nwöööööööööööörd.", $line);
}

/**
* Long-Break tests
*/
public function testWordWrapLongBreak()
{
$line = Zend_Text_MultiByte::wordWrap("Ä very<br>long wöö<br>öööööööö<br>öörd.", 8, '<br>', false);
$this->assertEquals("Ä very<br>long<br>wöö<br>öööööööö<br>öörd.", $line);
}

/**
* Alternative cut tests
*/
Expand Down

0 comments on commit aa8cf19

Please sign in to comment.