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
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Changelog

## [Unreleased]

### Changed

- When using passthrough, the chunk that produced the parsed frame is returned as a single chunk instead of individual remaining bytes
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
},
"require": {
"php": "~8.5",
"innmind/immutable": "~6.4"
"innmind/immutable": "~6.5"
},
"autoload": {
"psr-4": {
Expand Down
27 changes: 24 additions & 3 deletions proofs/frame.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@
->string(\implode('', $chunks))
->startsWith($a->append($b)->toString());
});

yield $prove
->proof('Frame->passthrough()')
->given(
Expand All @@ -132,9 +133,7 @@
);

[$first] = $parsed->toList();
$assert
->number($first->length())
->lessThanOrEqual($size);
$assert->same($size, $first->length());
$assert
->string(\implode('', $chunks))
->startsWith($first->toString());
Expand All @@ -143,4 +142,26 @@
$parsed->fold(Concat::monoid)->toString(),
);
});

yield $prove
->proof('Frame->passthrough() reaggregate the remaining unparsed chunk')
->given(
Set::strings()->between(11, 100_000),
Set::strings()->atLeast(1),
)
->test(static function(Assert $assert, $first, $last) {
$frame = Frame::chunk(10)->passthrough();
$parsed = $frame(
Sequence::of($first, $last)->map(Str::of(...)),
)->toList();

$assert->count(3, $parsed);
[$chunk, $remaining, $unaffected] = $parsed;
$assert->same(10, $chunk->length());
$assert->same(
Str::of($first, Str\Encoding::ascii)->drop(10)->toString(),
$remaining->toString(),
);
$assert->same($last, $unaffected->toString());
});
};
24 changes: 22 additions & 2 deletions src/Frame/Passthrough.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,31 @@ public function __invoke(Sequence $chunks): Sequence
/** @psalm-suppress InvalidArgument */
return $chunks
->map(static fn($chunk) => $chunk->toEncoding(Str\Encoding::ascii))
->flatMap(static fn($chunk) => $chunk->chunk())
->exclude(static fn($chunk) => $chunk->empty())
->map(Unparsed::of(...))
->prepend(Sequence::of($this->parse))
->aggregate(static fn(Parse|Parsed|Unparsed $a, Unparsed $b) => match (true) {
$a instanceof Parse => $a($b),
$a instanceof Parse => $b
->unwrap()
->chunk()
->map(Unparsed::of(...))
->prepend(Sequence::of($a))
->aggregate(static fn(Parse|Parsed|Unparsed $a, Unparsed $b) => match (true) {
$a instanceof Parse => $a($b),
default => Sequence::of($a, $b),
})
->match(
static fn($first, $rest) => match (true) {
$first instanceof Parsed => Sequence::of(
$first,
$b->map(
static fn($chunk) => $chunk->takeEnd($rest->size()),
),
),
default => $rest->prepend(Sequence::of($first)),
},
static fn() => Sequence::of(), // this case should not happen
),
default => Sequence::of($a, $b),
})
->keep(Instance::of(Parsed::class)->or(
Expand Down
16 changes: 12 additions & 4 deletions src/Unparsed.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,29 @@
final class Unparsed
{
private function __construct(
private Str $char,
private Str $value,
) {
}

/**
* @internal
* @psalm-pure
*/
public static function of(Str $char): self
public static function of(Str $value): self
{
return new self($char);
return new self($value);
}

/**
* @param pure-callable(Str): Str $map
*/
public function map(callable $map): self
{
return new self($map($this->value));
}

public function unwrap(): Str
{
return $this->char;
return $this->value;
}
}
Loading