Main Page | Namespace List | Class Hierarchy | Class List | File List | Class Members | File Members | Related Pages

OString.php

Go to the documentation of this file.
00001 <?php 00002 /* vim: set expandtab tabstop=4 shiftwidth=4: */ 00003 // 00004 // Copyright (c) 2003 Laurent Bedubourg 00005 // 00006 // This library is free software; you can redistribute it and/or 00007 // modify it under the terms of the GNU Lesser General Public 00008 // License as published by the Free Software Foundation; either 00009 // version 2.1 of the License, or (at your option) any later version. 00010 // 00011 // This library is distributed in the hope that it will be useful, 00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00014 // Lesser General Public License for more details. 00015 // 00016 // You should have received a copy of the GNU Lesser General Public 00017 // License along with this library; if not, write to the Free Software 00018 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00019 // 00020 // Authors: Laurent Bedubourg <laurent.bedubourg@free.fr> 00021 // 00022 00023 00024 require_once PT_IP . "/Types.php"; 00025 require_once PT_IP . "/Types/Errors.php"; 00026 00031 if (!defined('endl')) { 00032 if (isset($GLOBALS['REQUEST_URI'])) { 00033 define('endl', "<br/>\n"); 00034 } else { 00035 define('endl', "\n"); 00036 } 00037 } 00038 00053 class OString 00054 { 00055 var $_str; 00056 00065 function OString($str="") 00066 { 00067 return $this->__construct($str); 00068 } 00069 00080 function __construct($str="") 00081 { 00082 if (is_object($str)) { 00083 if (!method_exists($str, "toString")) { 00084 $err = new TypeError('String constructor requires string or ' 00085 . ' object implementing toString() method.', 00086 PEAR_ERROR_DIE); 00087 // return EX::raise($err); 00088 return PEAR::raiseError($err); 00089 } 00090 $this->_str = $str->toString(); 00091 } else { 00092 $this->_str = $str; 00093 } 00094 } 00095 00103 function append() 00104 { 00105 $args = func_get_args(); 00106 foreach ($args as $arg) { 00107 if (is_object($arg)) { 00108 $this->_str .= $arg->toString(); 00109 } else { 00110 $this->_str .= $arg; 00111 } 00112 } 00113 } 00114 00121 function charAt($i) 00122 { 00123 return $this->_str[$i]; 00124 } 00125 00136 function indexOf($needle, $pos=0) 00137 { 00138 if (is_object($needle)) { $needle = $needle->toString(); } 00139 return strpos($this->_str, $needle, $pos); 00140 } 00141 00151 function lastIndexOf($needle) 00152 { 00153 if (is_object($needle)) { $needle = $needle->toString(); } 00154 return strrpos($this->_str, $needle); 00155 } 00156 00163 function matches($regex) 00164 { 00165 if (is_object($regex)) { $regex = $regex->toString(); } 00166 return preg_match($regex, $this->_str); 00167 } 00168 00175 function contains($str) 00176 { 00177 if (is_object($str)){ $str = $str->toString(); } 00178 return (strpos($this->_str, $str) !== false); 00179 } 00180 00187 function startsWith($str) 00188 { 00189 if (is_object($str)){ $str = $str->toString(); } 00190 return preg_match('|^'. preg_quote($str) . '|', $this->_str); 00191 } 00192 00199 function endsWith($str) 00200 { 00201 if (is_object($str)){ $str = $str->toString(); } 00202 return preg_match('|'. preg_quote($str) . '$|', $this->_str); 00203 } 00204 00212 function replace($old, $new) 00213 { 00214 if (is_object($old)){ $old = $old->toString(); } 00215 if (is_object($new)){ $new = $new->toString(); } 00216 return str_replace($old, $new, $this->_str); 00217 } 00218 00226 function split($sep) 00227 { 00228 if (is_object($sep)){ $sep = $sep->toString(); } 00229 return split($sep, $this->_str); 00230 } 00231 00243 function substr($start, $end=false) 00244 { 00245 if ($end === false){ return substr($this->_str, $start); } 00246 return substr($this->_str, $start, ($end-$start)); 00247 } 00248 00260 function extract($start, $length) 00261 { 00262 return substr($this->_str, $start, $length); 00263 } 00264 00269 function toLowerCase() 00270 { 00271 return strtolower($this->_str); 00272 } 00273 00278 function toUpperCase() 00279 { 00280 return strtoupper($this->_str); 00281 } 00282 00287 function trim() 00288 { 00289 return trim($this->_str); 00290 } 00291 00299 function equals($o) 00300 { 00301 if (is_object($o)) { 00302 return $this->_str == $str->toString(); 00303 } else { 00304 return $this->_str == $o; 00305 } 00306 } 00307 00312 function length() 00313 { 00314 return strlen($this->_str); 00315 } 00316 00321 function toString() 00322 { 00323 return $this->_str; 00324 } 00325 00333 function &getNewIterator() 00334 { 00335 return new StringIterator($this); 00336 } 00337 } 00338 00347 class StringBuffer extends OString 00348 { 00356 function appendln() 00357 { 00358 $args = func_get_args(); 00359 foreach ($args as $arg) { 00360 if (is_object($arg)) { 00361 $this->_str .= $arg->toString(); 00362 } else { 00363 $this->_str .= $arg; 00364 } 00365 } 00366 $this->_str .= endl; 00367 } 00368 } 00369 00375 class StringIterator 00376 { 00377 var $_str; 00378 var $_index = -1; 00379 var $_end = false; 00380 var $_value; 00381 00389 function StringIterator(&$str) 00390 { 00391 $this->__construct($str); 00392 } 00393 00400 function __construct(&$str) 00401 { 00402 if (is_object($str)) { 00403 $this->_string = new OString($str->toString()); 00404 } else if (is_string($str)) { 00405 $this->_string = new OString($str); 00406 } 00407 $this->reset(); 00408 } 00409 00415 function reset() 00416 { 00417 $this->_end = false; 00418 $this->_index = 0; 00419 if ($this->_string->length() == 0) { 00420 $this->_end = true; 00421 } else { 00422 $this->_value = $this->_string->charAt(0); 00423 } 00424 } 00425 00431 function next() 00432 { 00433 if ($this->_end || ++$this->_index >= $this->_string->length()) { 00434 $this->_end = true; 00435 return null; 00436 } 00437 00438 $this->_value = $this->_string->charAt($this->_index); 00439 return $this->_value; 00440 } 00441 00447 function isValid() 00448 { 00449 return !$this->_end; 00450 } 00451 00457 function index() 00458 { 00459 return $this->_index; 00460 } 00461 00467 function value() 00468 { 00469 return $this->_value; 00470 } 00471 } 00472 00473 ?>

Generated on Tue Jun 29 23:40:05 2004 for Mediawiki by doxygen 1.3.7