Skip to content

Instantly share code, notes, and snippets.

@fprochazka
Forked from Majkl578/gist:1267600
Created October 6, 2011 14:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fprochazka/1267612 to your computer and use it in GitHub Desktop.
Save fprochazka/1267612 to your computer and use it in GitHub Desktop.
Strings::blend
<?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);
}
}
<?php
/**
* This file is part of the Kdyby (http://www.kdyby.org)
*
* Copyright (c) 2008, 2011 Filip Procházka (filip.prochazka@kdyby.org)
*
* @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