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
113 changes: 113 additions & 0 deletions ext/standard/VmSscanf.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,21 @@ public static function parseWithConsumed(string $input, string $format, array $o
if ($fpos >= $fmtLen) {
return [$assigned, $inPos];
}
if ('[' === $format[$fpos]) {
[$matcher, $fpos] = self::parseScansetMatcher($format, $fpos, $fmtLen);
if ($outIdx >= \count($outVars)) {
return [$assigned, $inPos];
}
[$str, $consumed] = self::scanScansetMatch($input, $inPos, $inLen, $matcher, $width);
if (null === $str) {
return [$assigned, $inPos];
}
self::assignString($outVars[$outIdx], $str);
$inPos += $consumed;
++$outIdx;
++$assigned;
continue;
}
$spec = $format[$fpos];
if ('%' === $spec) {
if ($inPos >= $inLen || $input[$inPos] !== '%') {
Expand Down Expand Up @@ -240,6 +255,11 @@ public static function countConversionSpecs(string $format): int
if ($fpos >= $len) {
break;
}
if ('[' === $format[$fpos]) {
[, $fpos] = self::parseScansetMatcher($format, $fpos, $len);
++$count;
continue;
}
$spec = $format[$fpos];
if ('%' !== $spec) {
++$count;
Expand All @@ -265,6 +285,10 @@ public static function validateConversionCharacters(string $format): void
if ($fpos >= $len) {
break;
}
if ('[' === $format[$fpos]) {
self::parseScansetMatcher($format, $fpos, $len);
continue;
}
$spec = $format[$fpos];
if ('%' === $spec) {
continue;
Expand All @@ -275,6 +299,95 @@ public static function validateConversionCharacters(string $format): void
}
}

/**
* %[scanset] — php-src ext/standard/formatted_io.c scan_set_conversion().
*
* @return array{0: callable(string): bool, 1: int}
*/
private static function parseScansetMatcher(string $format, int $fpos, int $fmtLen): array
{
if ($fpos >= $fmtLen || '[' !== $format[$fpos]) {
throw new \ValueError('Bad scan conversion character "["');
}
++$fpos;
$negated = false;
if ($fpos < $fmtLen && '^' === $format[$fpos]) {
$negated = true;
++$fpos;
}
/** @var array<string, true> $chars */
$chars = [];
if ($fpos >= $fmtLen) {
throw new \ValueError('Unmatched [ in format string');
}
if (']' === $format[$fpos]) {
throw new \ValueError('Unmatched [ in format string');
}
while ($fpos < $fmtLen && ']' !== $format[$fpos]) {
$ch = $format[$fpos];
if (
$fpos + 2 < $fmtLen
&& '-' === $format[$fpos + 1]
&& ']' !== $format[$fpos + 2]
) {
$lo = $ch;
$hi = $format[$fpos + 2];
if (ord($lo) <= ord($hi)) {
for ($c = ord($lo); $c <= ord($hi); ++$c) {
$chars[chr($c)] = true;
}
$fpos += 3;
continue;
}
}
$chars[$ch] = true;
++$fpos;
}
if ($fpos >= $fmtLen || ']' !== $format[$fpos]) {
throw new \ValueError('Unmatched [ in format string');
}
++$fpos;
$matcher = static function (string $ch) use ($chars, $negated): bool {
$in = isset($chars[$ch]);

return $negated ? !$in : $in;
};

return [$matcher, $fpos];
}

/**
* @param callable(string): bool $matcher
*
* @return array{0: ?string, 1: int}
*/
private static function scanScansetMatch(
string $input,
int $pos,
int $len,
callable $matcher,
?int $maxWidth = null
): array {
$orig = $pos;
$start = $pos;
$read = 0;
while ($pos < $len) {
if (null !== $maxWidth && $read >= $maxWidth) {
break;
}
if (!$matcher($input[$pos])) {
break;
}
++$pos;
++$read;
}
if ($start === $pos) {
return [null, 0];
}

return [substr($input, $start, $pos - $start), $pos - $orig];
}

/**
* Optional decimal field width after '%' (php-src ext/standard/formatted_io.c).
*
Expand Down
22 changes: 22 additions & 0 deletions test/compliance/cases/stdlib/sscanf_scanset.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
--TEST--
stdlib sscanf() — %[scanset] conversion (#11281, ext/standard/formatted_io.c)
--FILE--
<?php
declare(strict_types=1);

var_export(sscanf('abc123', '%[a-z]'));
echo "\n";
var_export(sscanf('abc123', '%[a-c]'));
echo "\n";
var_export(sscanf('xyz', '%[^0-9]'));
echo "\n";
--EXPECT--
array (
0 => 'abc',
)
array (
0 => 'abc',
)
array (
0 => 'xyz',
)
10 changes: 10 additions & 0 deletions test/repro/maintainer_gap_sscanf_scanset.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

var_export(sscanf('abc123', '%[a-z]'));
echo "\n";
var_export(sscanf('abc123', '%[a-c]'));
echo "\n";
var_export(sscanf('xyz', '%[^0-9]'));
echo "\n";