prefixing require_once statements- previous version worked only for for a few minutes...
[lhc/web/wiklou.git] / PHPTAL-NP-0.7.0 / libs / Types / OString.php
1 <?php
2 /* vim: set expandtab tabstop=4 shiftwidth=4: */
3 //
4 // Copyright (c) 2003 Laurent Bedubourg
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 //
20 // Authors: Laurent Bedubourg <laurent.bedubourg@free.fr>
21 //
22
23
24 require_once PT_IP . "/Types.php";
25 require_once PT_IP . "/Types/Errors.php";
26
27 /**
28 * Represents end of line string sequence '\n' of '<br/>' depending on
29 * script context.
30 */
31 if (!defined('endl')) {
32 if (isset($GLOBALS['REQUEST_URI'])) {
33 define('endl', "<br/>\n");
34 } else {
35 define('endl', "\n");
36 }
37 }
38
39 /**
40 * Object String wrapper class.
41 *
42 * This class is an attempt to normalize php strings in an oo way.
43 *
44 * PHP string related functions have different arguments scheme that decrease
45 * code readability in OO programs. This class provides some php functions
46 * in a comprehensible String class.
47 *
48 * The second interest reside in the StringIterator class which normalize
49 * iteration over strings just like any other iterable object.
50 *
51 * @author Laurent Bedubourg <laurent.bedubourg@free.fr>
52 */
53 class OString
54 {
55 var $_str;
56
57 /**
58 * String constructor.
59 *
60 * @param mixed $str
61 * May be an object from a class implementing the
62 * 'toString()' method or a php variable that can be
63 * casted into a string.
64 */
65 function OString($str="")
66 {
67 return $this->__construct($str);
68 }
69
70 /**
71 * String php4.4 constructor.
72 *
73 * @param mixed $str
74 * May be an object from a class implementing the
75 * 'toString()' method or a php variable that can be
76 * casted into a string.
77 *
78 * @throws TypeError
79 */
80 function __construct($str="")
81 {
82 if (is_object($str)) {
83 if (!method_exists($str, "toString")) {
84 $err = new TypeError('String constructor requires string or '
85 . ' object implementing toString() method.',
86 PEAR_ERROR_DIE);
87 // return EX::raise($err);
88 return PEAR::raiseError($err);
89 }
90 $this->_str = $str->toString();
91 } else {
92 $this->_str = $str;
93 }
94 }
95
96 /**
97 * Append string values of arguments to this string.
98 *
99 * @param mixed ...
100 * php variables or objects implementing the toString()
101 * method.
102 */
103 function append()
104 {
105 $args = func_get_args();
106 foreach ($args as $arg) {
107 if (is_object($arg)) {
108 $this->_str .= $arg->toString();
109 } else {
110 $this->_str .= $arg;
111 }
112 }
113 }
114
115 /**
116 * Retrieve char at specified position.
117 *
118 * @param int $i Character position
119 * @return char
120 */
121 function charAt($i)
122 {
123 return $this->_str[$i];
124 }
125
126 /**
127 * Find first index of needle in current string.
128 *
129 * Return the first position of needle in string or *false* if
130 * not found.
131 *
132 * @param mixed $needle object implementing toString or php variable.
133 * @param int $pos Search start position
134 * @return int
135 */
136 function indexOf($needle, $pos=0)
137 {
138 if (is_object($needle)) { $needle = $needle->toString(); }
139 return strpos($this->_str, $needle, $pos);
140 }
141
142 /**
143 * Find last occurence of needle in current string.
144 *
145 * Returns the last position of needle in string or *false* if
146 * not found.
147 *
148 * @param mixed needle object implementing toString or php variable.
149 * @return int
150 */
151 function lastIndexOf($needle)
152 {
153 if (is_object($needle)) { $needle = $needle->toString(); }
154 return strrpos($this->_str, $needle);
155 }
156
157 /**
158 * Returns true if the string match the specified regex.
159 *
160 * @param mixed $regex object implementing toString or php string.
161 * @return boolean
162 */
163 function matches($regex)
164 {
165 if (is_object($regex)) { $regex = $regex->toString(); }
166 return preg_match($regex, $this->_str);
167 }
168
169 /**
170 * Returns true if the string contains specified substring.
171 *
172 * @param mixed $str object implementing toString or php string.
173 * @return boolean
174 */
175 function contains($str)
176 {
177 if (is_object($str)){ $str = $str->toString(); }
178 return (strpos($this->_str, $str) !== false);
179 }
180
181 /**
182 * Returns true if the string begins with specified token.
183 *
184 * @param mixed $str object implementing toString or php string.
185 * @return boolean
186 */
187 function startsWith($str)
188 {
189 if (is_object($str)){ $str = $str->toString(); }
190 return preg_match('|^'. preg_quote($str) . '|', $this->_str);
191 }
192
193 /**
194 * Returns true if the string ends with specified token.
195 *
196 * @param mixed $str object implementing toString or php string.
197 * @return boolean
198 */
199 function endsWith($str)
200 {
201 if (is_object($str)){ $str = $str->toString(); }
202 return preg_match('|'. preg_quote($str) . '$|', $this->_str);
203 }
204
205 /**
206 * Replace occurences of 'old' with 'new'.
207 *
208 * @param mixed $old token to replace
209 * @param mixed $new new token
210 * @return string
211 */
212 function replace($old, $new)
213 {
214 if (is_object($old)){ $old = $old->toString(); }
215 if (is_object($new)){ $new = $new->toString(); }
216 return str_replace($old, $new, $this->_str);
217 }
218
219 /**
220 * Split this string using specified separator.
221 *
222 * @param mixed $sep Separator token
223 *
224 * @return array of phpstrings
225 */
226 function split($sep)
227 {
228 if (is_object($sep)){ $sep = $sep->toString(); }
229 return split($sep, $this->_str);
230 }
231
232 /**
233 * Retrieve a sub string.
234 *
235 * @param int $start
236 * start offset
237 *
238 * @param int $end
239 * End offset (up to end if no specified)
240 *
241 * @return string
242 */
243 function substr($start, $end=false)
244 {
245 if ($end === false){ return substr($this->_str, $start); }
246 return substr($this->_str, $start, ($end-$start));
247 }
248
249 /**
250 * Retrieve a sub string giving its length.
251 *
252 * @param int $start
253 * start offset
254 *
255 * @param int $length
256 * length from the start offset
257 *
258 * @return string
259 */
260 function extract($start, $length)
261 {
262 return substr($this->_str, $start, $length);
263 }
264
265 /**
266 * Return this string lower cased.
267 * @return string
268 */
269 function toLowerCase()
270 {
271 return strtolower($this->_str);
272 }
273
274 /**
275 * Return this string upper cased.
276 * @return string
277 */
278 function toUpperCase()
279 {
280 return strtoupper($this->_str);
281 }
282
283 /**
284 * Remove white characters from the beginning and the end of the string.
285 * @return string
286 */
287 function trim()
288 {
289 return trim($this->_str);
290 }
291
292 /**
293 * Test if this string equals specified object.
294 *
295 * @param mixed $o
296 * php string or object implementing the toString() method.
297 * @return boolean
298 */
299 function equals($o)
300 {
301 if (is_object($o)) {
302 return $this->_str == $str->toString();
303 } else {
304 return $this->_str == $o;
305 }
306 }
307
308 /**
309 * Return the length of this string.
310 * @return int
311 */
312 function length()
313 {
314 return strlen($this->_str);
315 }
316
317 /**
318 * Return the php string handled by this object
319 * @return string
320 */
321 function toString()
322 {
323 return $this->_str;
324 }
325
326 /**
327 * Create a string iterator for this String object.
328 *
329 * StringIterators iterate at a character level.
330 *
331 * @return StringIterator
332 */
333 function &getNewIterator()
334 {
335 return new StringIterator($this);
336 }
337 }
338
339 /**
340 * String buffer class.
341 *
342 * This class represents a buffer that concatenate appended string or objects
343 * into a simple php string.
344 *
345 * @author Laurent Bedubourg <laurent.bedubourg@free.fr>
346 */
347 class StringBuffer extends OString
348 {
349 /**
350 * Append some elements to the buffer with an endl terminator.
351 *
352 * @param mixed ...
353 * List of php variables and/or objects implementing the
354 * toString() method.
355 */
356 function appendln()
357 {
358 $args = func_get_args();
359 foreach ($args as $arg) {
360 if (is_object($arg)) {
361 $this->_str .= $arg->toString();
362 } else {
363 $this->_str .= $arg;
364 }
365 }
366 $this->_str .= endl;
367 }
368 }
369
370 /**
371 * Iterator for php strings and objects implementing toString method.
372 *
373 * @author Laurent Bedubourg <laurent.bedubourg@free.fr>
374 */
375 class StringIterator
376 {
377 var $_str;
378 var $_index = -1;
379 var $_end = false;
380 var $_value;
381
382 /**
383 * Constructor a string iterator.
384 *
385 * @param mixed $str
386 * String object, string variable, or Object implementing
387 * the toString() method.
388 */
389 function StringIterator(&$str)
390 {
391 $this->__construct($str);
392 }
393
394 /**
395 * Constructor a string iterator.
396 *
397 * @param mixed $str
398 * object implementing toString or php string variable
399 */
400 function __construct(&$str)
401 {
402 if (is_object($str)) {
403 $this->_string = new OString($str->toString());
404 } else if (is_string($str)) {
405 $this->_string = new OString($str);
406 }
407 $this->reset();
408 }
409
410 /**
411 * Reset iterator to the begining of the string.
412 *
413 * If empty string, this iterator assume it is at the end of string.
414 */
415 function reset()
416 {
417 $this->_end = false;
418 $this->_index = 0;
419 if ($this->_string->length() == 0) {
420 $this->_end = true;
421 } else {
422 $this->_value = $this->_string->charAt(0);
423 }
424 }
425
426 /**
427 * Return next character.
428 *
429 * @return char
430 */
431 function next()
432 {
433 if ($this->_end || ++$this->_index >= $this->_string->length()) {
434 $this->_end = true;
435 return null;
436 }
437
438 $this->_value = $this->_string->charAt($this->_index);
439 return $this->_value;
440 }
441
442 /**
443 * Return true if the end of the string is not reached yet.
444 *
445 * @return boolean
446 */
447 function isValid()
448 {
449 return !$this->_end;
450 }
451
452 /**
453 * Retrieve current iterator index.
454 *
455 * @return int
456 */
457 function index()
458 {
459 return $this->_index;
460 }
461
462 /**
463 * Retrieve current iterator value.
464 *
465 * @return char
466 */
467 function value()
468 {
469 return $this->_value;
470 }
471 }
472
473 ?>