-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScanner.php
More file actions
180 lines (158 loc) · 4.16 KB
/
Scanner.php
File metadata and controls
180 lines (158 loc) · 4.16 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
<?php
namespace pelish8\scanner;
class Scanner {
/**
* Specifying search options.
*
* @var string
*/
const BACKWARD_SEARCH = 'backwardSearch';
/**
* The string to scan.
*
* @var string
* @access private
*/
private $string;
/**
* Current position in string.
*
* @var int
* @access private
*/
private $location = 0;
/**
* Total length of the scanned string.
*
* @var int
* @access private
*/
private $length = 0;
/**
* Active search options.
*
* @var string
* @access private
*/
private $mask = '';
/**
* Active search options.
*
* @var string
* @access private
*/
private $origin = '';
/**
* Constructor.
*
* @param string $string The string to scan.
* @param string $mask Specifying search options.
* @access public
*/
public function __construct (&$string, $mask = '') {
if ($mask === self::BACKWARD_SEARCH) {
// revert string
$this->string = strrev($string);
} else {
$this->string = $string;
}
// save reference
$this->origin = &$string;
$this->length = strlen($string);
// set mask for later
$this->mask = $mask;
}
/**
* Scans to the string for which to scan and include that string.
*
* @param string $string The string for which to scan.
* @access public
*/
public function scanToString ($string) {
$location = strpos($this->string, $string, $this->location);
if ($location === false) {
$this->location = $this->length;
} else {
$this->location = $location + strlen($string);
}
}
/**
* Scans to the string for which to scan and do not include that string.
*
* @param string $string The string for which to scan.
* @access public
*/
public function scanUpToString ($string) {
$location = strpos($this->string, $string, $this->location);
if ($location === false) {
$this->location = $this->length;
} else {
$this->location = $location;
}
}
/**
* Return scanner location in string.
*
* @access public
*/
public function location () {
if ($this->mask === self::BACKWARD_SEARCH) {
return $this->length - $this->location;
} else {
return $this->location;
}
}
/**
* Check if the scanner is at end.
*
* @access public
*/
public function isAtEnd () {
return ($this->location >= $this->length);
}
/**
* Set new location from where to start scanning.
*
* @param int $location New location form witch to start scanning.
* @access public
*/
public function setLocation ($location) {
$this->location = $location;
}
/**
* Returns the string with which the receiver was created.
*
* @access public
*/
public function string () {
return $this->origin;
}
/**
* Returns the length of scand string.
*
* @access public
*/
public function length () {
return $this->length;
}
/**
* Scans the string until a character from a given string is encountered and include character.
*
* @param string $charactersSet The characters up to which to scan.
* @access public
*/
public function scanToCharacterFromString ($charactersString) {
$location = strcspn($this->string, $charactersString, $this->location, $this->length - $this->location);
$this->location += $location - 1;
}
/**
* Scans the string until a character from a given string is encountered and do not include character.
*
* @param string $charactersSet The characters up to which to scan.
* @access public
*/
public function scanUpToCharacterFromString ($charactersString) {
$location = strcspn($this->string, $charactersString, $this->location, $this->length - $this->location);
$this->location += $location;
}
}