1 | <?php |
||||
2 | |||||
3 | declare(strict_types=1); |
||||
4 | |||||
5 | namespace PhpSpellcheck\Utils; |
||||
6 | |||||
7 | use PhpSpellcheck\Exception\InvalidArgumentException; |
||||
8 | use Webmozart\Assert\Assert; |
||||
9 | |||||
10 | class LineAndOffset |
||||
11 | { |
||||
12 | /** |
||||
13 | * When spellcheckers gives the offset position of a misspelled word from the whole text's first character, |
||||
14 | * this helps finding the offset position from line's first character instead. |
||||
15 | * |
||||
16 | * @param string $text Chunk of text from which the line and offset are computed |
||||
17 | * @param int $offsetFromFirstCharacter Offset position from the text's first character |
||||
18 | * |
||||
19 | * @return array<int,int> Line number as the first element and offset from beginning of line as second element |
||||
20 | */ |
||||
21 | public static function findFromFirstCharacterOffset(string $text, int $offsetFromFirstCharacter): array |
||||
22 | { |
||||
23 | // positive offset |
||||
24 | Assert::greaterThanEq($offsetFromFirstCharacter, 0, \Safe\sprintf('Offset must be a positive integer, "%s" given', $offsetFromFirstCharacter)); |
||||
0 ignored issues
–
show
|
|||||
25 | |||||
26 | $textLength = mb_strlen($text); |
||||
27 | if ($textLength < $offsetFromFirstCharacter) { |
||||
28 | throw new InvalidArgumentException( |
||||
29 | \Safe\sprintf('Offset given "%d" is higher than the string length "%d"', $offsetFromFirstCharacter, $textLength) |
||||
0 ignored issues
–
show
The function
Safe\sprintf() has been deprecated: The Safe version of this function is no longer needed in PHP 8.0+
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This function has been deprecated. The supplier of the function has supplied an explanatory message. The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead. ![]() |
|||||
30 | ); |
||||
31 | } |
||||
32 | |||||
33 | $textBeforeOffset = mb_substr($text, 0, $offsetFromFirstCharacter); |
||||
34 | $line = ((int) \Safe\preg_match_all('/\R/u', $textBeforeOffset, $matches)) + 1; |
||||
35 | $offsetOfPreviousLinebreak = mb_strrpos($textBeforeOffset, PHP_EOL, 0); |
||||
36 | |||||
37 | $offset = $offsetFromFirstCharacter - ($offsetOfPreviousLinebreak !== false ? $offsetOfPreviousLinebreak + 1 : 0); |
||||
38 | |||||
39 | return [$line, $offset]; |
||||
40 | } |
||||
41 | } |
||||
42 |
This function has been deprecated. The supplier of the function has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.