Skip to content

Commit

Permalink
Merge pull request #3278 from mhsdesign/task/fixTextIteratorFollowing
Browse files Browse the repository at this point in the history
!!! TASK: Fix `TextIterator::following` and `preceding`
  • Loading branch information
mhsdesign committed Jan 25, 2024
2 parents 028cd8c + 2b30740 commit e5b1248
Showing 1 changed file with 14 additions and 8 deletions.
22 changes: 14 additions & 8 deletions Neos.Utility.Unicode/Classes/TextIterator.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
<?php

declare(strict_types=1);

namespace Neos\Utility\Unicode;

/*
Expand Down Expand Up @@ -195,13 +198,13 @@ public function last(): string
}

/**
* Returns the next elment following the character of the original string
* Returns the offset of the next element following the character of the original string
* given by its offset
*
* @param integer $offset The offset of the character
* @return string The element following this character
* @return int The offset of the element following this character
*/
public function following(int $offset): string
public function following(int $offset): int
{
$this->rewind();
while ($this->valid()) {
Expand All @@ -215,14 +218,15 @@ public function following(int $offset): string
}

/**
* Returns the element preceding the character of the original string given by its offset
* Returns the offset of the element preceding the character of the original string given by its offset
*
* @param integer $offset The offset of the character
* @return string The element preceding this character
* @return int The offset of the element preceding this character
*/
public function preceding(int $offset): string
public function preceding(int $offset): int
{
$this->rewind();
$currentElement = null;
while ($this->valid()) {
$previousElement = $this->getCurrentElement();
$this->next();
Expand All @@ -231,8 +235,10 @@ public function preceding(int $offset): string
return $previousElement->getOffset() + $previousElement->getLength();
}
}
/** @phpstan-ignore-next-line */
return $currentElement->getOffset() + $currentElement->getLength();
if ($currentElement) {
return $currentElement->getOffset() + $currentElement->getLength();
}
return -1;
}

/**
Expand Down

0 comments on commit e5b1248

Please sign in to comment.