2 /* vim: set expandtab tabstop=4 shiftwidth=4: */
4 // Copyright (c) 2003 Laurent Bedubourg
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.
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.
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
20 // Authors: Laurent Bedubourg <laurent.bedubourg@free.fr>
24 require_once PT_IP
. "/Types.php";
25 require_once PT_IP
. "/Types/Errors.php";
28 * Represents end of line string sequence '\n' of '<br/>' depending on
31 if (!defined('endl')) {
32 if (isset($GLOBALS['REQUEST_URI'])) {
33 define('endl', "<br/>\n");
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>
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);
71 * String php4.4 constructor.
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.
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.',
87 // return EX::raise($err);
88 return PEAR
::raiseError($err);
90 $this->_str
= $str->toString();
97 * Append string values of arguments to this string.
100 * php variables or objects implementing the toString()
105 $args = func_get_args();
106 foreach ($args as $arg) {
107 if (is_object($arg)) {
108 $this->_str
.= $arg->toString();
116 * Retrieve char at specified position.
118 * @param int $i Character position
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
132 * @param mixed $needle object implementing toString or php variable.
133 * @param int $pos Search start position
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
148 * @param mixed needle object implementing toString or php variable.
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.
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.
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.
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.
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
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
228 if (is_object($sep)){ $sep = $sep->toString(); }
229 return split($sep, $this->_str
);
233 * Retrieve a sub string.
239 * End offset (up to end if no specified)
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.
256 * length from the start offset
260 function extract($start, $length)
262 return substr($this->_str
, $start, $length);
266 * Return this string lower cased.
269 function toLowerCase()
271 return strtolower($this->_str
);
275 * Return this string upper cased.
278 function toUpperCase()
280 return strtoupper($this->_str
);
284 * Remove white characters from the beginning and the end of the string.
289 return trim($this->_str
);
293 * Test if this string equals specified object.
296 * php string or object implementing the toString() method.
302 return $this->_str
== $str->toString();
304 return $this->_str
== $o;
309 * Return the length of this string.
314 return strlen($this->_str
);
318 * Return the php string handled by this object
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.
353 * List of php variables and/or objects implementing the
358 $args = func_get_args();
359 foreach ($args as $arg) {
360 if (is_object($arg)) {
361 $this->_str
.= $arg->toString();
371 * Iterator for php strings and objects implementing toString method.
373 * @author Laurent Bedubourg <laurent.bedubourg@free.fr>
383 * Constructor a string iterator.
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.
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);
411 * Reset iterator to the begining of the string.
413 * If empty string, this iterator assume it is at the end of string.
419 if ($this->_string
->length() == 0) {
422 $this->_value
= $this->_string
->charAt(0);
427 * Return next character.
433 if ($this->_end || ++
$this->_index
>= $this->_string
->length()) {
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.
453 * Retrieve current iterator index.
459 return $this->_index
;
463 * Retrieve current iterator value.
469 return $this->_value
;