-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathLineCol.php
More file actions
137 lines (112 loc) · 3.57 KB
/
LineCol.php
File metadata and controls
137 lines (112 loc) · 3.57 KB
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
<?php
namespace Phpactor\TextDocument;
use OutOfBoundsException;
use RuntimeException;
/**
* Value object for line / column position.
*
* Lines and columns start with 1.
*
* The "a" in "abcd" would have line "1" and column "1".
*/
final class LineCol
{
public const NEWLINE_PATTERN = '\\r\\n|\\n|\\r';
private int $line;
private int $col;
public function __construct(int $line, int $col)
{
if ($line < 1) {
throw new RuntimeException(sprintf(
'Line number cannot be less than 1 (got "%s")',
$line
));
}
if ($col < 1) {
throw new RuntimeException(sprintf(
'Col number cannot be less than 1 (got "%s")',
$col
));
}
$this->line = $line;
$this->col = $col;
}
public function toByteOffset(string $text): ByteOffset
{
$linesAndDelims = (array)preg_split('{(' . self::NEWLINE_PATTERN . ')}', $text, -1, PREG_SPLIT_DELIM_CAPTURE);
if (count($linesAndDelims) === 0) {
return ByteOffset::fromInt(
strlen((string)reset($linesAndDelims))
);
}
$lineNb = 1;
$offset = 0;
foreach ($linesAndDelims as $lineOrDelim) {
$lineOrDelim = (string)$lineOrDelim;
if ((bool)preg_match('{(' . self::NEWLINE_PATTERN . ')}', (string)$lineOrDelim)) {
$lineNb++;
$offset += strlen($lineOrDelim);
continue;
}
if ($lineNb === $this->line()) {
$lineSection = mb_substr(
$lineOrDelim,
0,
$this->col() - 1
);
return ByteOffset::fromInt(
$offset + (int)strlen($lineSection)
);
}
$offset += strlen((string)$lineOrDelim);
}
return ByteOffset::fromInt(strlen($text));
}
public static function fromByteOffset(string $text, ByteOffset $byteOffset): self
{
if ($byteOffset->toInt() > strlen($text)) {
$byteOffset = ByteOffset::fromInt(strlen($text));
}
$lines = preg_split('{(' . self::NEWLINE_PATTERN . ')}', $text, -1, PREG_SPLIT_DELIM_CAPTURE);
if (false === $lines) {
throw new RuntimeException(
'Failed to preg-split text into lines'
);
}
$offset = 0;
$lineNb = 0;
foreach ($lines as $lineOrDelim) {
$lineOrDelim = (string)$lineOrDelim;
if ((bool)preg_match('{(' . self::NEWLINE_PATTERN . ')}', (string)$lineOrDelim)) {
$offset += strlen($lineOrDelim);
continue;
}
$lineNb++;
$start = $offset;
$end = $offset + strlen($lineOrDelim);
// if the offset is in line...
if ($byteOffset->toInt() >= $start && $byteOffset->toInt() <= $end) {
$section = substr(
$lineOrDelim,
0,
$byteOffset->toInt() - $start
);
return new self($lineNb, mb_strlen($section) + 1);
}
$offset = $end;
}
throw new OutOfBoundsException(sprintf(
'Byte offset %s is larger than text length %s',
$byteOffset->toInt(),
strlen($text)
));
}
public function col(): int
{
return $this->col;
}
public function line(): int
{
return $this->line;
}
}