Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions ext/mbstring/Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ public function getFunctions(): array
new mb_stripos(),
new mb_strrpos(),
new mb_strrichr(),
new mb_strstr(),
...(CompilerVersion::supportsMbTrimFunctions() ? [
new mb_trim(),
new mb_ltrim(),
Expand Down
20 changes: 20 additions & 0 deletions ext/mbstring/VmMbstring.php
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,26 @@ public static function strpos(string $haystack, string $needle, int $offset = 0,
return self::utf8Strpos($haystack, $needle, $offset, false, $encoding, 'mb_strpos');
}

/**
* mb_strstr() — find first occurrence and return haystack from that point (php-src ext/mbstring/mbstring.c).
*
* @return string|false
*/
public static function strstr(string $haystack, string $needle, bool $part = false, string $encoding = 'UTF-8')
{
self::assertSearchEncoding($encoding);
$pos = self::utf8Strpos($haystack, $needle, 0, false, $encoding, 'mb_strstr');
if (false === $pos) {
return false;
}
if ($part) {
return VmString::utf8CharSubstr($haystack, 0, $pos);
}
$charLen = VmString::utf8CharLength($haystack);

return VmString::utf8CharSubstr($haystack, $pos, $charLen - $pos);
}

public static function substr(
string $string,
int $start,
Expand Down
76 changes: 76 additions & 0 deletions ext/mbstring/mb_strstr.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

declare(strict_types=1);

namespace PHPCompiler\ext\mbstring;

use PHPCompiler\ext\standard\VmString;
use PHPCompiler\Frame;
use PHPCompiler\Func\Internal;
use PHPCompiler\JIT\Context;
use PHPCompiler\JIT\Variable as JITVariable;
use PHPCompiler\VM\BuiltinExecute;
use PHPCompiler\VM\Variable;
use PHPLLVM\Value;

/**
* mb_strstr() — multibyte strstr (php-src ext/mbstring/mbstring.c).
*/
final class mb_strstr extends Internal
{
public function __construct()
{
parent::__construct('mb_strstr');
}

public function execute(Frame $frame): void
{
$argc = \count($frame->calledArgs);
if ($argc < 2 || $argc > 4) {
throw new \ArgumentCountError(sprintf(
'mb_strstr() expects at least 2 arguments, %d given',
$argc
));
}
$haystack = VmString::coerceStringBuiltinArg(
$frame->calledArgs[0],
'mb_strstr',
0,
'haystack'
);
if (null === $frame->returnVar) {
return;
}
$needle = VmString::coerceStringBuiltinArg(
$frame->calledArgs[1],
'mb_strstr',
1,
'needle'
);
$part = $argc >= 3
? VmMbstring::coercePartArg($frame->calledArgs[2], 'mb_strstr', 2)
: false;
$encoding = $argc >= 4
? VmMbstring::coerceEncodingArg($frame->calledArgs[3], 'mb_strstr', 3)
: 'UTF-8';
$result = VmMbstring::strstr($haystack, $needle, $part, $encoding);
BuiltinExecute::writeReturn($frame, static function (Variable $ret) use ($result): void {
if (false === $result) {
$ret->bool(false);

return;
}
$ret->string($result);
});
}

public function call(Context $context, JITVariable ...$args): Value
{
$argc = \count($args);
if ($argc < 2 || $argc > 4) {
throw new \LogicException('mb_strstr() requires two to four arguments');
}

throw new \LogicException('mb_strstr() is not lowered for JIT/AOT in this compiler build');
}
}
15 changes: 15 additions & 0 deletions test/compliance/cases/stdlib/mb_strstr.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
--TEST--
stdlib mb_strstr() — multibyte strstr (#19941, ext/mbstring/mbstring.c)
--FILE--
<?php
var_dump(mb_strstr('Hello World', 'World'));
var_dump(mb_strstr('Hello World', 'World', true));
var_dump(mb_strstr('Hello World', 'xyz'));
var_dump(mb_strstr("caf\xC3\xA9 r\xC3\xA9sum\xC3\xA9", "\xC3\xA9"));
var_dump(mb_strstr("caf\xC3\xA9 r\xC3\xA9sum\xC3\xA9", "\xC3\xA9", true));
--EXPECT--
string(5) "World"
string(6) "Hello "
bool(false)
string(11) "é résumé"
string(3) "caf"