-
-
Save fprochazka/1267612 to your computer and use it in GitHub Desktop.
Strings::blend
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* @param string $a | |
* @param string $b | |
* @return string | |
*/ | |
public static function blend($a, $b) | |
{ | |
$pos = strrpos($a, $b); | |
if ($pos !== FALSE) { // is croping | |
return substr($a, 0, $pos + strlen($b)); | |
} else { // is merging | |
$fromRight = 0; | |
do { | |
$fromRight--; | |
$pos = strrpos($a, $match = substr($b, 0, $fromRight)); | |
} while ($pos === FALSE && $match); | |
return substr($a, 0, $pos + strlen($match)) . substr($b, $fromRight); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* This file is part of the Kdyby (http://www.kdyby.org) | |
* | |
* Copyright (c) 2008, 2011 Filip Procházka ([email protected]) | |
* | |
* @license http://www.kdyby.org/license | |
*/ | |
namespace Kdyby\Testing\Tools; | |
use Kdyby; | |
use Nette; | |
/** | |
* @author Filip Procházka | |
*/ | |
class TestTest extends Kdyby\Testing\TestCase | |
{ | |
/** | |
* @return array | |
*/ | |
public function getBlendData() | |
{ | |
return array( | |
array( | |
'/var/www/libs/library/namespace/subns', 'namespace', '/var/www/libs/library/namespace', | |
), | |
array( | |
'abcdefghij', 'hijkl', 'abcdefghijkl', | |
), | |
array( | |
'/var/www/libs/library/namespace', 'namespace', '/var/www/libs/library/namespace', | |
), | |
array( | |
'/var/www/libs/library/namespace', 'namespace/subns', '/var/www/libs/library/namespace/subns', | |
) | |
); | |
} | |
/** | |
* @dataProvider getBlendData | |
*/ | |
public function testBlend($a, $b, $result) | |
{ | |
$this->assertSame($result, Kdyby\Tools\Strings::blend($a, $b)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment