1 | <?php |
---|
2 | |
---|
3 | class Text |
---|
4 | { |
---|
5 | public static $variable = '\b[a-zA-Z_.$][\w.$]*'; |
---|
6 | public static $namespace = '\b[a-zA-Z_.$][\w.$]*(?:\.[a-zA-Z_.$][\w.$]*|\["[^"]+"\])*'; |
---|
7 | |
---|
8 | /** |
---|
9 | * Blanks out a portion of a string with whitespace |
---|
10 | * |
---|
11 | * @param $to_blank Portion of the string to be removed |
---|
12 | * @param $string Overall string to remove it from |
---|
13 | */ |
---|
14 | public static function blankOut($to_blank, $string) { |
---|
15 | $length = strlen($to_blank); |
---|
16 | if (!$length) { |
---|
17 | return $string; |
---|
18 | } |
---|
19 | |
---|
20 | $blanks = array_fill(0, $length, ' '); |
---|
21 | return preg_replace('%' . preg_quote($to_blank, '%') . '%', implode($blanks), $string, 1); |
---|
22 | } |
---|
23 | |
---|
24 | /** |
---|
25 | * Makes sure a variable is formatted namespace.namespace.etc |
---|
26 | * |
---|
27 | * @param shell |
---|
28 | * Called shell because it might be missing some data |
---|
29 | */ |
---|
30 | public static function normalizeVariableName($shell, $source, $start){ |
---|
31 | if (strpos($shell, '[') !== false) { |
---|
32 | $source_line = array_shift(Text::chop($source, $start[0], $start[1], $start[0] + 1)); |
---|
33 | preg_match('%^\s*([a-zA-Z_.$][\w.$]*(?:\.[a-zA-Z_.$][\w.$]|\["[^"]+"\])*)\s*=\s*function%', $source_line, $match); |
---|
34 | $shell = preg_replace('%\["([^"]+)"\]%', '.$1', $match[1]); |
---|
35 | } |
---|
36 | return $shell; |
---|
37 | } |
---|
38 | |
---|
39 | public static function getNextPosition($array, $line_position_pair) { |
---|
40 | list($line_number, $position) = $line_position_pair; |
---|
41 | ++$position; |
---|
42 | if ($position >= strlen($array[$line_number])) { |
---|
43 | ++$line_number; |
---|
44 | $position = 0; |
---|
45 | while (!strlen($array[$line_number])) { |
---|
46 | ++$line_number; |
---|
47 | } |
---|
48 | } |
---|
49 | return array($line_number, $position); |
---|
50 | } |
---|
51 | |
---|
52 | public static function blankOutAtPositions($to_blank, $start_line, $start_pos, $end_line = -1, $end_pos = -1) { |
---|
53 | foreach ($to_blank as $line_number => $line) { |
---|
54 | if ($line_number == $start_line) { |
---|
55 | $to_blank[$line_number] = Text::blankOutAt($line, $start_pos); |
---|
56 | } |
---|
57 | if ($line_number > $start_line) { |
---|
58 | if ($end_line == -1 || $line_number < $end_line) { |
---|
59 | $to_blank[$line_number] = Text::blankOutAt($line, 0); |
---|
60 | } |
---|
61 | elseif ($line_number == $end_line) { |
---|
62 | $to_blank[$line_number] = Text::blankOutAt($line, 0, $end_pos); |
---|
63 | } |
---|
64 | } |
---|
65 | } |
---|
66 | return $to_blank; |
---|
67 | } |
---|
68 | |
---|
69 | public static function blankOutAt($to_blank, $start, $end = -1) { |
---|
70 | if ($end == -1) { |
---|
71 | $end = strlen($to_blank) - 1; |
---|
72 | } |
---|
73 | $length = $end - $start + 1; |
---|
74 | if (!$length) { |
---|
75 | return $to_blank; |
---|
76 | } |
---|
77 | if ($length < 0) { |
---|
78 | throw new Exception("Length is less than 0"); |
---|
79 | } |
---|
80 | $blanks = array_fill(0, $length, ' '); |
---|
81 | return substr($to_blank, 0, $start) . implode($blanks) . substr($to_blank, $end + 1); |
---|
82 | } |
---|
83 | |
---|
84 | public static function trim($string) { |
---|
85 | return trim(preg_replace('%(^\s*/\*.*\*/\s*?|\s*?/\*.*\*/\s*$|^\s*//.*\n\s*?|\s*?//.*$)%U', '', $string)); |
---|
86 | } |
---|
87 | |
---|
88 | public static function chop($array, $start_line, $start_position, $end_line = false, $end_position = false, $exclusive = false) { |
---|
89 | if (!is_numeric($end_line)) { |
---|
90 | $end_line = end(array_keys($array)); |
---|
91 | } |
---|
92 | if (!is_numeric($end_position)) { |
---|
93 | $end_position = strlen($array[$end_line]) - 1; |
---|
94 | if ($end_position < 0) { |
---|
95 | $end_position = 0; |
---|
96 | } |
---|
97 | } |
---|
98 | |
---|
99 | $lines = array_slice($array, $start_line, $end_line - $start_line + 1, true); |
---|
100 | if ($start_position > 0) { |
---|
101 | $lines[$start_line] = Text::blankOutAt($lines[$start_line], 0, $start_position - 1); |
---|
102 | } |
---|
103 | $lines[$end_line] = Text::blankOutAt($lines[$end_line], $end_position + 1, strlen($lines[$end_line])); |
---|
104 | if ($exclusive) { |
---|
105 | if ($lines[$start_line]{$start_position}) { |
---|
106 | $lines[$start_line]{$start_position} = ' '; |
---|
107 | } |
---|
108 | if ($lines[$end_line]{$end_position}) { |
---|
109 | $lines[$end_line]{$end_position} = ' '; |
---|
110 | } |
---|
111 | } |
---|
112 | |
---|
113 | return $lines; |
---|
114 | } |
---|
115 | |
---|
116 | /** |
---|
117 | * Always starts at the beginning. If you want a character to be ignored, it shouldn't be passed (see chop and blankOutAt) |
---|
118 | */ |
---|
119 | public static function findTermination($source_array, $termination_characters, $enclosing_characters = '') { |
---|
120 | $characters = array(); |
---|
121 | $terminators = array(); |
---|
122 | foreach (self::toArray($termination_characters) as $character) { |
---|
123 | $terminators[$character] = true; |
---|
124 | } |
---|
125 | foreach (self::toArray($enclosing_characters) as $index => $character) { |
---|
126 | $characters[$character] = ($index % 2) ? -1 : 1; |
---|
127 | } |
---|
128 | $all_characters = array_merge(array_keys($terminators), array_keys($characters)); |
---|
129 | |
---|
130 | $balance = 0; |
---|
131 | |
---|
132 | foreach ($source_array as $line_number => $line) { |
---|
133 | $line = self::toArray($line); |
---|
134 | foreach (array_intersect($line, $all_characters) as $position => $character) { |
---|
135 | if (!$balance && $terminators[$character]) { |
---|
136 | return array($line_number, $position); |
---|
137 | } |
---|
138 | $balance += $characters[$character]; |
---|
139 | } |
---|
140 | } |
---|
141 | |
---|
142 | return array($line_number, $position); |
---|
143 | } |
---|
144 | |
---|
145 | public static function toArray($string) { |
---|
146 | return array_slice(preg_split('%%', $string), 1, -1); |
---|
147 | } |
---|
148 | |
---|
149 | /** |
---|
150 | * Splits a comment line up |
---|
151 | * |
---|
152 | * @returns Array(whitespace, comment data, non-comment data, comment data, has non-comment-data, comment continues on next line) |
---|
153 | */ |
---|
154 | public static function findComments($line, $started = false) { |
---|
155 | if (empty($line) && !$started) { |
---|
156 | return array(false, false, false, false, false); |
---|
157 | } |
---|
158 | |
---|
159 | $first = array(); |
---|
160 | $middle = array(); |
---|
161 | $last = array(); |
---|
162 | $data = false; |
---|
163 | $multiline = false; |
---|
164 | |
---|
165 | if ($started) { |
---|
166 | // If we're already in a (multi-line) comment, look to close it up |
---|
167 | if (($pos = strpos($line, '*/')) !== false) { |
---|
168 | $first[] = trim(substr($line, 0, $pos)); |
---|
169 | $line = substr($line, $pos + 2); |
---|
170 | } |
---|
171 | else { |
---|
172 | // We didn't find a terminator, we're still in a multi-line comment |
---|
173 | $multiline = true; |
---|
174 | } |
---|
175 | } |
---|
176 | |
---|
177 | $single_line = false; // Denotes that we're in a single-line comment. |
---|
178 | if (!$multiline) { |
---|
179 | // Split by //, /*, and */ while ignoring any surrounding whitespace |
---|
180 | $parts = preg_split('%(\s*(?://|/\*|\*/)\s*)%', $line, -1, PREG_SPLIT_DELIM_CAPTURE); |
---|
181 | foreach ($parts as $part) { |
---|
182 | if (!($trimmed = trim($part))) continue; |
---|
183 | if ($multiline && $trimmed == '*/') { |
---|
184 | $multiline = false; // Multi-line ends! |
---|
185 | } |
---|
186 | elseif ($single_line || $multiline) { // If we're within a comment |
---|
187 | if (!$data) { |
---|
188 | $first[] = $part; |
---|
189 | } |
---|
190 | else { // If we've found any non-comment data, we start logging this as "after" the data |
---|
191 | $last[] = $part; |
---|
192 | } |
---|
193 | } |
---|
194 | elseif ($trimmed == '//') { |
---|
195 | $single_line = true; |
---|
196 | } |
---|
197 | elseif ($trimmed == '/*') { |
---|
198 | $multiline = true; |
---|
199 | } |
---|
200 | else { |
---|
201 | $data = true; |
---|
202 | $middle = array_merge($middle, $last); |
---|
203 | $last = array(); |
---|
204 | } |
---|
205 | } |
---|
206 | } |
---|
207 | |
---|
208 | return array(trim(implode('', $first)), trim(implode('', $middle)), trim(implode('', $last)), $data, $multiline); |
---|
209 | } |
---|
210 | |
---|
211 | } |
---|
212 | |
---|
213 | ?> |
---|