forked from tobeorla/Nginx-Config-Processor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathText.php
More file actions
201 lines (176 loc) · 4.71 KB
/
Text.php
File metadata and controls
201 lines (176 loc) · 4.71 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
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
<?php
/**
* This file is part of the Nginx Config Processor package.
*
* (c) Roman Piták <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
*/
namespace RomanPitak\Nginx\Config;
class Text
{
const CURRENT_POSITION = -1;
/** @var string $data */
private $data;
/** @var int $position */
private $position;
/**
* @param string $data
*/
public function __construct($data)
{
$this->position = 0;
$this->data = $data;
}
/*
* ========== Getters ==========
*/
/**
* Returns one character of the string.
*
* Does not move the string pointer. Use inc() to move the pointer after getChar().
*
* @param int $position If not specified, current character is returned.
* @return string The current character (under the pointer).
* @throws Exception When out of range
*/
public function getChar($position = self::CURRENT_POSITION)
{
if (self::CURRENT_POSITION === $position) {
$position = $this->position;
}
if (!is_int($position)) {
throw new Exception('Position is not int. ' . gettype($position));
}
if ($this->eof()) {
throw new Exception('Index out of range. Position: ' . $position . '.');
}
return $this->data[$position];
}
/**
* Get the text from $position to the next end of line.
*
* Does not move the string pointer.
*
* @param int $position
* @return string
*/
public function getRestOfTheLine($position = self::CURRENT_POSITION)
{
if (self::CURRENT_POSITION === $position) {
$position = $this->position;
}
$text = '';
while ((false === $this->eof($position)) && (false === $this->eol($position))) {
$text .= $this->getChar($position);
$position++;
}
return $text;
}
/**
* Is this the end of line?
*
* @param int $position
* @return bool
* @throws Exception
*/
public function eol($position = self::CURRENT_POSITION)
{
return (("\r" === $this->getChar($position)) || ("\n" === $this->getChar($position)));
}
/**
* Is this line empty?
*
* @param int $position
* @return bool
*/
public function isEmptyLine($position = self::CURRENT_POSITION)
{
$line = $this->getCurrentLine($position);
return (0 === strlen(trim($line)));
}
/**
* Get the current line.
*
* @param int $position
* @return string
*/
public function getCurrentLine($position = self::CURRENT_POSITION)
{
if (self::CURRENT_POSITION === $position) {
$position = $this->position;
}
$offset = $this->getLastEol($position);
$length = $this->getNextEol($position) - $offset;
return substr($this->data, $offset, $length);
}
/**
* Get the position of the last (previous) EOL.
*
* @param int $position
* @return int
*/
public function getLastEol($position = self::CURRENT_POSITION)
{
if (self::CURRENT_POSITION === $position) {
$position = $this->position;
}
return strrpos(substr($this->data, 0, $position), "\n", 0);
}
/**
* Get the position of the next EOL.
*
* @param int $position
* @return int
*/
public function getNextEol($position = self::CURRENT_POSITION)
{
if (self::CURRENT_POSITION === $position) {
$position = $this->position;
}
$eolPosition = strpos($this->data, "\n", $position);
if (false === $eolPosition) {
$eolPosition = strlen($this->data) - 1;
}
return $eolPosition;
}
/**
* Is this the end of file (string) or beyond?
*
* @param int $position
* @return bool
*/
public function eof($position = self::CURRENT_POSITION)
{
if (self::CURRENT_POSITION === $position) {
$position = $this->position;
}
return (!isset($this->data[$position]));
}
/*
* ========== Manipulators ==========
*/
/**
* Move string pointer.
*
* @param int $inc
*/
public function inc($inc = 1)
{
$this->position += $inc;
}
/**
* Move pointer (position) to the next EOL.
*
* @param int $position
*/
public function gotoNextEol($position = self::CURRENT_POSITION)
{
if (self::CURRENT_POSITION === $position) {
$position = $this->position;
}
$this->position = $this->getNextEol($position);
}
}