Completed
Push — refactor-parsing ( adbb6b...fbe6de )
by Colin
08:21 queued 07:01
created

Cursor::advanceToNextNonSpaceOrTab()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 5
cts 5
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the league/commonmark package.
7
 *
8
 * (c) Colin O'Dell <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace League\CommonMark\Parser;
15
16
class Cursor
17
{
18
    public const INDENT_LEVEL = 4;
19
20
    /**
21
     * @var string
22
     */
23
    private $line;
24
25
    /**
26
     * @var int
27
     */
28
    private $length;
29
30
    /**
31
     * @var int
32
     *
33
     * It's possible for this to be 1 char past the end, meaning we've parsed all chars and have
34
     * reached the end.  In this state, any character-returning method MUST return null.
35
     */
36
    private $currentPosition = 0;
37
38
    /**
39
     * @var int
40
     */
41
    private $column = 0;
42
43
    /**
44
     * @var int
45
     */
46
    private $indent = 0;
47
48
    /**
49
     * @var int
50
     */
51
    private $previousPosition = 0;
52
53
    /**
54
     * @var int|null
55
     */
56
    private $nextNonSpaceCache;
57
58
    /**
59
     * @var bool
60
     */
61
    private $partiallyConsumedTab = false;
62
63
    /**
64
     * @var bool
65
     */
66
    private $lineContainsTabs;
67
68
    /**
69
     * @var bool
70
     */
71
    private $isMultibyte;
72
73
    /**
74
     * @var array<int, string>
75
     */
76
    private $charCache = [];
77
78
    /**
79
     * @param string $line The line being parsed (ASCII or UTF-8)
80
     */
81 2997
    public function __construct(string $line)
82
    {
83 2997
        $this->line = $line;
84 2997
        $this->length = \mb_strlen($line, 'UTF-8') ?: 0;
85 2997
        $this->isMultibyte = $this->length !== \strlen($line);
86 2997
        $this->lineContainsTabs = false !== \strpos($line, "\t");
87 2997
    }
88
89
    /**
90
     * Returns the position of the next character which is not a space (or tab)
91
     *
92
     * @return int
93
     */
94 2697
    public function getNextNonSpacePosition(): int
95
    {
96 2697
        if ($this->nextNonSpaceCache !== null) {
97 2517
            return $this->nextNonSpaceCache;
98
        }
99
100 2697
        $i = $this->currentPosition;
101 2697
        $cols = $this->column;
102
103 2697
        while (($c = $this->getCharacter($i)) !== null) {
104 2673
            if ($c === ' ') {
105 810
                $i++;
106 810
                $cols++;
107 2631
            } elseif ($c === "\t") {
108 36
                $i++;
109 36
                $cols += (4 - ($cols % 4));
110
            } else {
111 2631
                break;
112
            }
113
        }
114
115 2697
        $nextNonSpace = ($c === null) ? $this->length : $i;
116 2697
        $this->indent = $cols - $this->column;
117
118 2697
        return $this->nextNonSpaceCache = $nextNonSpace;
119
    }
120
121
    /**
122
     * Returns the next character which isn't a space (or tab)
123
     *
124
     * @return string
125
     */
126 2463
    public function getNextNonSpaceCharacter(): ?string
127
    {
128 2463
        return $this->getCharacter($this->getNextNonSpacePosition());
129
    }
130
131
    /**
132
     * Calculates the current indent (number of spaces after current position)
133
     *
134
     * @return int
135
     */
136 2577
    public function getIndent(): int
137
    {
138 2577
        if ($this->nextNonSpaceCache === null) {
139 360
            $this->getNextNonSpacePosition();
140
        }
141
142 2577
        return $this->indent;
143
    }
144
145
    /**
146
     * Whether the cursor is indented to INDENT_LEVEL
147
     *
148
     * @return bool
149
     */
150 2517
    public function isIndented(): bool
151
    {
152 2517
        return $this->getIndent() >= self::INDENT_LEVEL;
153
    }
154
155
    /**
156
     * @param int|null $index
157
     *
158
     * @return string|null
159
     */
160 2757
    public function getCharacter(?int $index = null): ?string
161
    {
162 2757
        if ($index === null) {
163 2469
            $index = $this->currentPosition;
164
        }
165
166
        // Index out-of-bounds, or we're at the end
167 2757
        if ($index < 0 || $index >= $this->length) {
168 2367
            return null;
169
        }
170
171 2715
        if ($this->isMultibyte) {
172 102
            if (isset($this->charCache[$index])) {
173 48
                return $this->charCache[$index];
174
            }
175
176 102
            return $this->charCache[$index] = \mb_substr($this->line, $index, 1, 'UTF-8');
177
        }
178
179 2619
        return $this->line[$index];
180
    }
181
182
    /**
183
     * Returns the next character (or null, if none) without advancing forwards
184
     *
185
     * @param int $offset
186
     *
187
     * @return string|null
188
     */
189 1329
    public function peek(int $offset = 1): ?string
190
    {
191 1329
        return $this->getCharacter($this->currentPosition + $offset);
192
    }
193
194
    /**
195
     * Whether the remainder is blank
196
     *
197
     * @return bool
198
     */
199 2511
    public function isBlank(): bool
200
    {
201 2511
        return $this->nextNonSpaceCache === $this->length || $this->getNextNonSpacePosition() === $this->length;
202
    }
203
204
    /**
205
     * Move the cursor forwards
206
     */
207 510
    public function advance(): void
208
    {
209 510
        $this->advanceBy(1);
210 510
    }
211
212
    /**
213
     * Move the cursor forwards
214
     *
215
     * @param int  $characters       Number of characters to advance by
216
     * @param bool $advanceByColumns Whether to advance by columns instead of spaces
217
     *
218
     * @return void
219
     */
220 2865
    public function advanceBy(int $characters, bool $advanceByColumns = false): void
221
    {
222 2865
        if ($characters === 0) {
223 2574
            $this->previousPosition = $this->currentPosition;
224
225 2574
            return;
226
        }
227
228 2694
        $this->previousPosition = $this->currentPosition;
229 2694
        $this->nextNonSpaceCache = null;
230
231
        // Optimization to avoid tab handling logic if we have no tabs
232 2694
        if (!$this->lineContainsTabs || false === \strpos(
233 51
            $nextFewChars = $this->isMultibyte ?
234 6
                \mb_substr($this->line, $this->currentPosition, $characters, 'UTF-8') :
235 51
                \substr($this->line, $this->currentPosition, $characters),
236 2694
            "\t"
237
        )) {
238 2682
            $length = \min($characters, $this->length - $this->currentPosition);
239 2682
            $this->partiallyConsumedTab = false;
240 2682
            $this->currentPosition += $length;
241 2682
            $this->column += $length;
242
243 2682
            return;
244
        }
245
246 45
        if ($characters === 1 && !empty($nextFewChars)) {
247 18
            $asArray = [$nextFewChars];
248 39
        } elseif ($this->isMultibyte) {
249
            /** @var string[] $asArray */
250
            $asArray = \preg_split('//u', $nextFewChars, -1, \PREG_SPLIT_NO_EMPTY);
251
        } else {
252 39
            $asArray = \str_split($nextFewChars);
253
        }
254
255 45
        foreach ($asArray as $relPos => $c) {
256 45
            if ($c === "\t") {
257 45
                $charsToTab = 4 - ($this->column % 4);
258 45
                if ($advanceByColumns) {
259 33
                    $this->partiallyConsumedTab = $charsToTab > $characters;
260 33
                    $charsToAdvance = $charsToTab > $characters ? $characters : $charsToTab;
261 33
                    $this->column += $charsToAdvance;
262 33
                    $this->currentPosition += $this->partiallyConsumedTab ? 0 : 1;
263 33
                    $characters -= $charsToAdvance;
264
                } else {
265 18
                    $this->partiallyConsumedTab = false;
266 18
                    $this->column += $charsToTab;
267 18
                    $this->currentPosition++;
268 45
                    $characters--;
269
                }
270
            } else {
271 12
                $this->partiallyConsumedTab = false;
272 12
                $this->currentPosition++;
273 12
                $this->column++;
274 12
                $characters--;
275
            }
276
277 45
            if ($characters <= 0) {
278 45
                break;
279
            }
280
        }
281 45
    }
282
283
    /**
284
     * Advances the cursor by a single space or tab, if present
285
     *
286
     * @return bool
287
     */
288 390
    public function advanceBySpaceOrTab(): bool
289
    {
290 390
        $character = $this->getCharacter();
291
292 390
        if ($character === ' ' || $character === "\t") {
293 378
            $this->advanceBy(1, true);
294
295 378
            return true;
296
        }
297
298 294
        return false;
299
    }
300
301
    /**
302
     * Parse zero or more space/tab characters
303
     *
304
     * @return int Number of positions moved
305
     */
306 2529
    public function advanceToNextNonSpaceOrTab(): int
307
    {
308 2529
        $newPosition = $this->getNextNonSpacePosition();
309 2529
        $this->advanceBy($newPosition - $this->currentPosition);
310 2529
        $this->partiallyConsumedTab = false;
311
312 2529
        return $this->currentPosition - $this->previousPosition;
313
    }
314
315
    /**
316
     * Parse zero or more space characters, including at most one newline.
317
     *
318
     * Tab characters are not parsed with this function.
319
     *
320
     * @return int Number of positions moved
321
     */
322 249
    public function advanceToNextNonSpaceOrNewline(): int
323
    {
324 249
        $remainder = $this->getRemainder();
325
326
        // Optimization: Avoid the regex if we know there are no spaces or newlines
327 249
        if (empty($remainder) || ($remainder[0] !== ' ' && $remainder[0] !== "\n")) {
328 216
            $this->previousPosition = $this->currentPosition;
329
330 216
            return 0;
331
        }
332
333 84
        $matches = [];
334 84
        \preg_match('/^ *(?:\n *)?/', $remainder, $matches, \PREG_OFFSET_CAPTURE);
335
336
        // [0][0] contains the matched text
337
        // [0][1] contains the index of that match
338 84
        $increment = $matches[0][1] + \strlen($matches[0][0]);
339
340 84
        $this->advanceBy($increment);
341
342 84
        return $this->currentPosition - $this->previousPosition;
343
    }
344
345
    /**
346
     * Move the position to the very end of the line
347
     *
348
     * @return int The number of characters moved
349
     */
350 795
    public function advanceToEnd(): int
351
    {
352 795
        $this->previousPosition = $this->currentPosition;
353 795
        $this->nextNonSpaceCache = null;
354
355 795
        $this->currentPosition = $this->length;
356
357 795
        return $this->currentPosition - $this->previousPosition;
358
    }
359
360 2631
    public function getRemainder(): string
361
    {
362 2631
        if ($this->currentPosition >= $this->length) {
363 552
            return '';
364
        }
365
366 2604
        $prefix = '';
367 2604
        $position = $this->currentPosition;
368 2604
        if ($this->partiallyConsumedTab) {
369 12
            $position++;
370 12
            $charsToTab = 4 - ($this->column % 4);
371 12
            $prefix = \str_repeat(' ', $charsToTab);
372
        }
373
374 2604
        $subString = $this->isMultibyte ?
375 72
            \mb_substr($this->line, $position, null, 'UTF-8') :
376 2604
            \substr($this->line, $position);
377
378 2604
        return $prefix . $subString;
379
    }
380
381 1962
    public function getLine(): string
382
    {
383 1962
        return $this->line;
384
    }
385
386 2151
    public function isAtEnd(): bool
387
    {
388 2151
        return $this->currentPosition >= $this->length;
389
    }
390
391
    /**
392
     * Try to match a regular expression
393
     *
394
     * Returns the matching text and advances to the end of that match
395
     *
396
     * @param string $regex
397
     *
398
     * @return string|null
399
     */
400 2370
    public function match(string $regex): ?string
401
    {
402 2370
        $subject = $this->getRemainder();
403
404 2370
        if (!\preg_match($regex, $subject, $matches, \PREG_OFFSET_CAPTURE)) {
405 942
            return null;
406
        }
407
408
        // $matches[0][0] contains the matched text
409
        // $matches[0][1] contains the index of that match
410
411 2337
        if ($this->isMultibyte) {
412
            // PREG_OFFSET_CAPTURE always returns the byte offset, not the char offset, which is annoying
413 57
            $offset = \mb_strlen(\substr($subject, 0, $matches[0][1]), 'UTF-8');
414 57
            $matchLength = \mb_strlen($matches[0][0], 'UTF-8');
415
        } else {
416 2280
            $offset = $matches[0][1];
417 2280
            $matchLength = \strlen($matches[0][0]);
418
        }
419
420
        // [0][0] contains the matched text
421
        // [0][1] contains the index of that match
422 2337
        $this->advanceBy($offset + $matchLength);
423
424 2337
        return $matches[0][0];
425
    }
426
427
    /**
428
     * Encapsulates the current state of this cursor in case you need to rollback later.
429
     *
430
     * WARNING: Do not parse or use the return value for ANYTHING except for
431
     * passing it back into restoreState(), as the number of values and their
432
     * contents may change in any future release without warning.
433
     */
434 1686
    public function saveState(): CursorState
435
    {
436 1686
        return new CursorState([
437 1686
            $this->currentPosition,
438 1686
            $this->previousPosition,
439 1686
            $this->nextNonSpaceCache,
440 1686
            $this->indent,
441 1686
            $this->column,
442 1686
            $this->partiallyConsumedTab,
443
        ]);
444
    }
445
446
    /**
447
     * Restore the cursor to a previous state.
448
     *
449
     * Pass in the value previously obtained by calling saveState().
450
     *
451
     * @param CursorState $state
452
     */
453 1593
    public function restoreState(CursorState $state): void
454
    {
455
        [
456 1593
            $this->currentPosition,
457 1593
            $this->previousPosition,
458 1593
            $this->nextNonSpaceCache,
459 1593
            $this->indent,
460 1593
            $this->column,
461 1593
            $this->partiallyConsumedTab,
462 1593
          ] = $state->toArray();
463 1593
    }
464
465 732
    public function getPosition(): int
466
    {
467 732
        return $this->currentPosition;
468
    }
469
470 396
    public function getPreviousText(): string
471
    {
472 396
        return \mb_substr($this->line, $this->previousPosition, $this->currentPosition - $this->previousPosition, 'UTF-8');
473
    }
474
475 423
    public function getSubstring(int $start, ?int $length = null): string
476
    {
477 423
        if ($this->isMultibyte) {
478 21
            return \mb_substr($this->line, $start, $length, 'UTF-8');
479 402
        } elseif ($length !== null) {
480 399
            return \substr($this->line, $start, $length);
481
        }
482
483 3
        return \substr($this->line, $start);
484
    }
485
486 285
    public function getColumn(): int
487
    {
488 285
        return $this->column;
489
    }
490
}
491