Skip to content

Commit

Permalink
- fix related to buggy full-case-folding used in grapheme_stri*()
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolas-grekas committed Apr 25, 2012
1 parent f88c902 commit 152f700
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 3 deletions.
1 change: 1 addition & 0 deletions class/Patchwork/PHP/Override/Intl.php
Expand Up @@ -124,6 +124,7 @@ static function grapheme_strstr ($s, $needle, $before_needle = false) {return m
protected static function grapheme_position($s, $needle, $offset, $mode)
{
if ($offset > 0) $s = (string) self::grapheme_substr($s, $offset);
else if ($offset < 0) $offset = 0;
if ('' === (string) $needle) return false;
if ('' === (string) $s) return false;

Expand Down
24 changes: 21 additions & 3 deletions class/Patchwork/Utf8.php
Expand Up @@ -141,11 +141,29 @@ static function substr($s, $start, $len = 2147483647)

static function strlen($s) {return grapheme_strlen($s);}
static function strpos ($s, $needle, $offset = 0) {return grapheme_strpos ($s, $needle, $offset);}
static function stripos ($s, $needle, $offset = 0) {return grapheme_stripos ($s, $needle, $offset);}
static function strrpos ($s, $needle, $offset = 0) {return grapheme_strrpos ($s, $needle, $offset);}
static function strripos($s, $needle, $offset = 0) {return grapheme_strripos($s, $needle, $offset);}

static function stripos ($s, $needle, $offset = 0)
{
if ($offset < 0) $offset = 0;
if (!$needle = mb_stripos($s, $needle, $offset, 'UTF-8')) return $needle;
return grapheme_strlen(iconv_substr($s, 0, $needle, 'UTF-8'));
}

static function strripos($s, $needle, $offset = 0)
{
if ($offset < 0) $offset = 0;
if (!$needle = mb_strripos($s, $needle, $offset, 'UTF-8')) return $needle;
return grapheme_strlen(iconv_substr($s, 0, $needle, 'UTF-8'));
}

static function stristr ($s, $needle, $before_needle = false)
{
if ('' == (string) $needle) return false;
return mb_stristr($s, $needle, $before_needle, 'UTF-8');
}

static function strstr ($s, $needle, $before_needle = false) {return grapheme_strstr ($s, $needle, $before_needle);}
static function stristr ($s, $needle, $before_needle = false) {return grapheme_stristr($s, $needle, $before_needle);}
static function strrchr ($s, $needle, $before_needle = false) {return mb_strrchr ($s, $needle, $before_needle, 'UTF-8');}
static function strrichr($s, $needle, $before_needle = false) {return mb_strrichr($s, $needle, $before_needle, 'UTF-8');}

Expand Down
15 changes: 15 additions & 0 deletions tests/Patchwork/Tests/PHP/Override/IntlTest.php
Expand Up @@ -89,6 +89,13 @@ function testGrapheme_substr()
$this->assertSame( "", grapheme_substr($c, -1, 0) );
$this->assertSame( false, grapheme_substr($c, 1, -4) );
}
else
{
// Expected fail: this buggy behavior has been fixed in PHP5.4
$this->assertSame( false, grapheme_substr($c, -2, 3) );
$this->assertSame( false, grapheme_substr($c, -1, 0) );
$this->assertSame( 'éjà', grapheme_substr($c, 1, -4) );
}
$this->assertSame( "j", grapheme_substr($c, -2, -1) );
$this->assertSame( "", grapheme_substr($c, -2, -2) );
$this->assertSame( false, grapheme_substr($c, 5, 0) );
Expand Down Expand Up @@ -143,6 +150,7 @@ function testGrapheme_strpos()
$this->assertSame( false, p::grapheme_strrpos('한국어', '') );
$this->assertSame( 1, p::grapheme_strrpos('한국어', '국') );
$this->assertSame( 3, p::grapheme_strripos('DÉJÀ', 'à') );
$this->assertSame( 16, p::grapheme_stripos('der Straße nach Paris', 'Paris') );
}

/**
Expand All @@ -156,5 +164,12 @@ function testGrapheme_strstr()

$this->assertSame( '국어', p::grapheme_strstr('한국어', '국') );
$this->assertSame( 'ÉJÀ', p::grapheme_stristr('DÉJÀ', 'é') );
$this->assertSame( 'Paris', p::grapheme_stristr('der Straße nach Paris', 'Paris') );
}

function testGrapheme_bugs()
{
$this->assertSame( 17, grapheme_stripos('der Straße nach Paris', 'Paris') ); // Expected fail: the non-bugged result is 16
$this->assertSame( 'aris', grapheme_stristr('der Straße nach Paris', 'Paris') ); // Expected fail: the non-bugged result is Paris
}
}
6 changes: 6 additions & 0 deletions tests/Patchwork/Tests/Utf8Test.php
Expand Up @@ -159,9 +159,12 @@ function testStrpos()
$this->assertSame( 0, u::strpos('abc', 'a', -1) );
$this->assertSame( 1, u::strpos('한국어', '국') );
$this->assertSame( 3, u::stripos('DÉJÀ', 'à') );
$this->assertSame( 1, u::stripos('aςσb', 'ΣΣ') );
$this->assertSame( false, u::strrpos('한국어', '') );
$this->assertSame( 1, u::strrpos('한국어', '국') );
$this->assertSame( 3, u::strripos('DÉJÀ', 'à') );
$this->assertSame( 1, u::strripos('aςσb', 'ΣΣ') );
$this->assertSame( 16, u::stripos('der Straße nach Paris', 'Paris') );
}

/**
Expand All @@ -174,13 +177,16 @@ function testStrstr()
{
$this->assertSame( 'éjàdéjà', u::strstr('déjàdéjà', 'é') );
$this->assertSame( 'ÉJÀDÉJÀ', u::stristr('DÉJÀDÉJÀ', 'é') );
$this->assertSame( 'ςσb', u::stristr('aςσb', 'ΣΣ') );
$this->assertSame( 'éjà', u::strrchr('déjàdéjà', 'é') );
$this->assertSame( 'ÉJÀ', u::strrichr('DÉJÀDÉJÀ', 'é') );

$this->assertSame( 'd', u::strstr('déjàdéjà', 'é', true) );
$this->assertSame( 'D', u::stristr('DÉJÀDÉJÀ', 'é', true) );
$this->assertSame( 'a', u::stristr('aςσb', 'ΣΣ', true) );
$this->assertSame( 'déjàd', u::strrchr('déjàdéjà', 'é', true) );
$this->assertSame( 'DÉJÀD', u::strrichr('DÉJÀDÉJÀ', 'é', true) );
$this->assertSame( 'Paris', u::stristr('der Straße nach Paris', 'Paris') );
}

/**
Expand Down

0 comments on commit 152f700

Please sign in to comment.