LinksUpdate uses SELECT FOR UPDATE, thus starting a new transaction. So we have to...
[mediawiki.git] / PHPTAL-NP-0.7.0 / libs / Types / OString.php
blob1a9ecbc5d27d800c21b73686a891989c42b0173c
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 //
24 require_once PT_IP . "/Types.php";
25 require_once PT_IP . "/Types/Errors.php";
27 /**
28 * Represents end of line string sequence '\n' of '<br/>' depending on
29 * script context.
31 if (!defined('endl')) {
32 if (isset($GLOBALS['REQUEST_URI'])) {
33 define('endl', "<br/>\n");
34 } else {
35 define('endl', "\n");
39 /**
40 * Object String wrapper class.
42 * This class is an attempt to normalize php strings in an oo way.
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.
48 * The second interest reside in the StringIterator class which normalize
49 * iteration over strings just like any other iterable object.
51 * @author Laurent Bedubourg <laurent.bedubourg@free.fr>
53 class OString
55 var $_str;
57 /**
58 * String constructor.
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.
65 function OString($str="")
67 return $this->__construct($str);
70 /**
71 * String php4.4 constructor.
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.
78 * @throws TypeError
80 function __construct($str="")
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);
90 $this->_str = $str->toString();
91 } else {
92 $this->_str = $str;
96 /**
97 * Append string values of arguments to this string.
99 * @param mixed ...
100 * php variables or objects implementing the toString()
101 * method.
103 function append()
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;
116 * Retrieve char at specified position.
118 * @param int $i Character position
119 * @return char
121 function charAt($i)
123 return $this->_str[$i];
127 * Find first index of needle in current string.
129 * Return the first position of needle in string or *false* if
130 * not found.
132 * @param mixed $needle object implementing toString or php variable.
133 * @param int $pos Search start position
134 * @return int
136 function indexOf($needle, $pos=0)
138 if (is_object($needle)) { $needle = $needle->toString(); }
139 return strpos($this->_str, $needle, $pos);
143 * Find last occurence of needle in current string.
145 * Returns the last position of needle in string or *false* if
146 * not found.
148 * @param mixed needle object implementing toString or php variable.
149 * @return int
151 function lastIndexOf($needle)
153 if (is_object($needle)) { $needle = $needle->toString(); }
154 return strrpos($this->_str, $needle);
158 * Returns true if the string match the specified regex.
160 * @param mixed $regex object implementing toString or php string.
161 * @return boolean
163 function matches($regex)
165 if (is_object($regex)) { $regex = $regex->toString(); }
166 return preg_match($regex, $this->_str);
170 * Returns true if the string contains specified substring.
172 * @param mixed $str object implementing toString or php string.
173 * @return boolean
175 function contains($str)
177 if (is_object($str)){ $str = $str->toString(); }
178 return (strpos($this->_str, $str) !== false);
182 * Returns true if the string begins with specified token.
184 * @param mixed $str object implementing toString or php string.
185 * @return boolean
187 function startsWith($str)
189 if (is_object($str)){ $str = $str->toString(); }
190 return preg_match('|^'. preg_quote($str) . '|', $this->_str);
194 * Returns true if the string ends with specified token.
196 * @param mixed $str object implementing toString or php string.
197 * @return boolean
199 function endsWith($str)
201 if (is_object($str)){ $str = $str->toString(); }
202 return preg_match('|'. preg_quote($str) . '$|', $this->_str);
206 * Replace occurences of 'old' with 'new'.
208 * @param mixed $old token to replace
209 * @param mixed $new new token
210 * @return string
212 function replace($old, $new)
214 if (is_object($old)){ $old = $old->toString(); }
215 if (is_object($new)){ $new = $new->toString(); }
216 return str_replace($old, $new, $this->_str);
220 * Split this string using specified separator.
222 * @param mixed $sep Separator token
224 * @return array of phpstrings
226 function split($sep)
228 if (is_object($sep)){ $sep = $sep->toString(); }
229 return split($sep, $this->_str);
233 * Retrieve a sub string.
235 * @param int $start
236 * start offset
238 * @param int $end
239 * End offset (up to end if no specified)
241 * @return string
243 function substr($start, $end=false)
245 if ($end === false){ return substr($this->_str, $start); }
246 return substr($this->_str, $start, ($end-$start));
250 * Retrieve a sub string giving its length.
252 * @param int $start
253 * start offset
255 * @param int $length
256 * length from the start offset
258 * @return string
260 function extract($start, $length)
262 return substr($this->_str, $start, $length);
266 * Return this string lower cased.
267 * @return string
269 function toLowerCase()
271 return strtolower($this->_str);
275 * Return this string upper cased.
276 * @return string
278 function toUpperCase()
280 return strtoupper($this->_str);
284 * Remove white characters from the beginning and the end of the string.
285 * @return string
287 function trim()
289 return trim($this->_str);
293 * Test if this string equals specified object.
295 * @param mixed $o
296 * php string or object implementing the toString() method.
297 * @return boolean
299 function equals($o)
301 if (is_object($o)) {
302 return $this->_str == $str->toString();
303 } else {
304 return $this->_str == $o;
309 * Return the length of this string.
310 * @return int
312 function length()
314 return strlen($this->_str);
318 * Return the php string handled by this object
319 * @return string
321 function toString()
323 return $this->_str;
327 * Create a string iterator for this String object.
329 * StringIterators iterate at a character level.
331 * @return StringIterator
333 function &getNewIterator()
335 return new StringIterator($this);
340 * String buffer class.
342 * This class represents a buffer that concatenate appended string or objects
343 * into a simple php string.
345 * @author Laurent Bedubourg <laurent.bedubourg@free.fr>
347 class StringBuffer extends OString
350 * Append some elements to the buffer with an endl terminator.
352 * @param mixed ...
353 * List of php variables and/or objects implementing the
354 * toString() method.
356 function appendln()
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;
366 $this->_str .= endl;
371 * Iterator for php strings and objects implementing toString method.
373 * @author Laurent Bedubourg <laurent.bedubourg@free.fr>
375 class StringIterator
377 var $_str;
378 var $_index = -1;
379 var $_end = false;
380 var $_value;
383 * Constructor a string iterator.
385 * @param mixed $str
386 * String object, string variable, or Object implementing
387 * the toString() method.
389 function StringIterator(&$str)
391 $this->__construct($str);
395 * Constructor a string iterator.
397 * @param mixed $str
398 * object implementing toString or php string variable
400 function __construct(&$str)
402 if (is_object($str)) {
403 $this->_string = new OString($str->toString());
404 } else if (is_string($str)) {
405 $this->_string = new OString($str);
407 $this->reset();
411 * Reset iterator to the begining of the string.
413 * If empty string, this iterator assume it is at the end of string.
415 function reset()
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);
427 * Return next character.
429 * @return char
431 function next()
433 if ($this->_end || ++$this->_index >= $this->_string->length()) {
434 $this->_end = true;
435 return null;
438 $this->_value = $this->_string->charAt($this->_index);
439 return $this->_value;
443 * Return true if the end of the string is not reached yet.
445 * @return boolean
447 function isValid()
449 return !$this->_end;
453 * Retrieve current iterator index.
455 * @return int
457 function index()
459 return $this->_index;
463 * Retrieve current iterator value.
465 * @return char
467 function value()
469 return $this->_value;