aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/scssphp/source-span/src/SourceFile.php
blob: 33bc5502cf946eacb11256a9cacebfa9114395cd (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
<?php

namespace SourceSpan;

use League\Uri\Contracts\UriInterface;

final class SourceFile
{
    private readonly string $string;

    private readonly ?UriInterface $sourceUrl;

    /**
     * @var list<int>
     */
    private readonly array $lineStarts;

    /**
     * The 0-based last line that was returned by {@see getLine}
     *
     * This optimizes computation for successive accesses to
     * the same line or to the next line.
     * It is stored as 0-based to correspond to the indices
     * in {@see $lineStarts}.
     *
     * @var int|null
     */
    private ?int $cachedLine = null;

    public static function fromString(string $content, ?UriInterface $sourceUrl = null): SourceFile
    {
        return new SourceFile($content, $sourceUrl);
    }

    private function __construct(string $content, ?UriInterface $sourceUrl = null)
    {
        $this->string = $content;
        $this->sourceUrl = $sourceUrl;

        // Extract line starts
        $lineStarts = [0];

        if ($content === '') {
            $this->lineStarts = $lineStarts;
            return;
        }

        $prev = 0;

        while (true) {
            $crPos = strpos($content, "\r", $prev);
            $lfPos = strpos($content, "\n", $prev);

            if ($crPos === false && $lfPos === false) {
                break;
            }

            if ($crPos !== false) {
                // Return not followed by newline is treated as a newline
                if ($lfPos === false || $lfPos > $crPos + 1) {
                    $lineStarts[] = $crPos + 1;
                    $prev = $crPos + 1;
                    continue;
                }
            }

            if ($lfPos !== false) {
                $lineStarts[] = $lfPos + 1;
                $prev = $lfPos + 1;
            }
        }

        $this->lineStarts = $lineStarts;
    }

    public function getLength(): int
    {
        return \strlen($this->string);
    }

    /**
     * The number of lines in the file.
     */
    public function getLines(): int
    {
        return \count($this->lineStarts);
    }

    public function span(int $start, ?int $end = null): FileSpan
    {
        if ($end === null) {
            $end = \strlen($this->string);
        }

        return new ConcreteFileSpan($this, $start, $end);
    }

    public function location(int $offset): FileLocation
    {
        if ($offset < 0) {
            throw new \OutOfRangeException("Offset may not be negative, was $offset.");
        }

        if ($offset > \strlen($this->string)) {
            $fileLength = \strlen($this->string);

            throw new \OutOfRangeException("Offset $offset must not be greater than the number of characters in the file, $fileLength.");
        }

        return new FileLocation($this, $offset);
    }

    public function getSourceUrl(): ?UriInterface
    {
        return $this->sourceUrl;
    }

    public function getString(): string
    {
        return $this->string;
    }

    /**
     * The 0-based line corresponding to that offset.
     */
    public function getLine(int $offset): int
    {
        if ($offset < 0) {
            throw new \OutOfRangeException('Position cannot be negative');
        }

        if ($offset > \strlen($this->string)) {
            throw new \OutOfRangeException('Position cannot be greater than the number of characters in the string.');
        }

        if ($offset < $this->lineStarts[0]) {
            return -1;
        }

        if ($offset >= Util::listLast($this->lineStarts)) {
            return \count($this->lineStarts) - 1;
        }

        if ($this->isNearCacheLine($offset)) {
            assert($this->cachedLine !== null);

            return $this->cachedLine;
        }

        $this->cachedLine = $this->binarySearch($offset) - 1;

        return $this->cachedLine;
    }

    /**
     * Returns `true` if $offset is near {@see $cachedLine}.
     *
     * Checks on {@see $cachedLine} and the next line. If it's on the next line, it
     * updates {@see $cachedLine} to point to that.
     */
    private function isNearCacheLine(int $offset): bool
    {
        if ($this->cachedLine === null) {
            return false;
        }

        if ($offset < $this->lineStarts[$this->cachedLine]) {
            return false;
        }

        if (
            $this->cachedLine >= \count($this->lineStarts) - 1 ||
            $offset < $this->lineStarts[$this->cachedLine + 1]
        ) {
            return true;
        }

        if (
            $this->cachedLine >= \count($this->lineStarts) - 2 ||
            $offset < $this->lineStarts[$this->cachedLine + 2]
        ) {
            ++$this->cachedLine;

            return true;
        }

        return false;
    }

    /**
     * Binary search through {@see $lineStarts} to find the line containing $offset.
     *
     * Returns the index of the line in {@see $lineStarts}.
     */
    private function binarySearch(int $offset): int
    {
        $min = 0;
        $max = \count($this->lineStarts) - 1;

        while ($min < $max) {
            $half = $min + intdiv($max - $min, 2);

            if ($this->lineStarts[$half] > $offset) {
                $max = $half;
            } else {
                $min = $half + 1;
            }
        }

        return $max;
    }

    /**
     * The 0-based column of that offset.
     */
    public function getColumn(int $offset): int
    {
        $line = $this->getLine($offset);

        return $offset - $this->lineStarts[$line];
    }

    /**
     * Gets the offset for a line and column.
     */
    public function getOffset(int $line, int $column = 0): int
    {
        if ($line < 0) {
            throw new \OutOfRangeException('Line may not be negative.');
        }

        if ($line >= \count($this->lineStarts)) {
            throw new \OutOfRangeException('Line must be less than the number of lines in the file.');
        }

        if ($column < 0) {
            throw new \OutOfRangeException('Column may not be negative.');
        }

        $result = $this->lineStarts[$line] + $column;

        if ($result > \strlen($this->string) || ($line + 1 < \count($this->lineStarts) && $result >= $this->lineStarts[$line + 1])) {
            throw new \OutOfRangeException("Line $line doesn't have $column columns.");
        }

        return $result;
    }

    /**
     * Returns the text of the file from $start to $end (exclusive).
     *
     * If $end isn't passed, it defaults to the end of the file.
     */
    public function getText(int $start, ?int $end = null): string
    {
        if ($end !== null) {
            if ($end < $start) {
                throw new \InvalidArgumentException("End $end must come after start $start.");
            }

            if ($end > $this->getLength()) {
                throw new \OutOfRangeException("End $end not be greater than the number of characters in the file, {$this->getLength()}.");
            }
        }

        if ($start < 0) {
            throw new \OutOfRangeException("Start may not be negative, was $start.");
        }

        return Util::substring($this->string, $start, $end);
    }
}