Package home | Report new bug | New search | Development Roadmap Status: Open | Feedback | All | Closed Since Version 0.3.1

Bug #10572 stringReplace does infinite loop
Submitted: 2007-03-31 11:51 UTC
From: abadura Assigned: doconnor
Status: Closed Package: I18N_UnicodeString (version 0.2.1)
PHP Version: 5.2.0 OS: Windows XP, SP2
Roadmaps: (Not assigned)    
Subscription  


 [2007-03-31 11:51 UTC] abadura (Adam Badura)
Description: ------------ It seems to me that stringReplace does not handle correctly a case where the replacing string does contain the string to be replaced because it hangs (I suspect it enters infinte loop). Perhaps it was intended behavior (but why? performance?) but if so than you should perhaps add a funcion which does not have this drawback and of course make it clear in documentation. Test script: --------------- $ustr_amp_find = new I18N_UnicodeString('&', 'ASCII'); $ustr_amp_replace = new I18N_UnicodeString('&', 'ASCII'); $ustr = new I18N_UnicodeString('(p&&q)||r', 'HTML'); $ustr = $ustr->stringReplace($ustr_amp_find, $ustr_amp_replace); Expected result: ---------------- (p&&q)||r Actual result: -------------- The stringReplace function never ends.

Comments

 [2007-06-19 15:15 UTC] bpuettbach (Benedikt P├╝ttbach)
For reasons out of scope for me, the upload script times out when trying to commit a patch for the referred problem. Therefore my solution gets posted here. As far as I see, the problem is solved by the lines below, which 'fix' a break in encapsulation (original code accessing private $_unicode property) too. I guess this is a performance issue, but didn't test it. string-replace-loop.patch -------------------------- Index: UnicodeString.php =================================================================== --- UnicodeString.php (revision 1) +++ UnicodeString.php (working copy) @@ -429,17 +429,16 @@ * @see subStringReplace() */ function stringReplace(&$find, &$replace) { - $return = new I18N_UnicodeString($this->_unicode, 'Unicode'); + $haystack = new I18N_UnicodeString($this->_unicode, 'Unicode'); + $return = new I18N_UnicodeString(array(), 'Unicode'); - while($return->strStr($find) !== false) { - $after = $return->strStr($find); - $begin = $return->subString(0, $return->length() - $after->length()); - $after = $after->subString($find->length()); - - $return = new I18N_UnicodeString(array_merge($begin->_unicode, $replace->_unicode, $after->_unicode), 'Unicode'); + while($haystack->strStr($find) !== false) { + $after = $haystack->strStr($find); + $begin = $haystack->subString(0, $haystack->length() - $after->length()); + $haystack = $after->subString($find->length()); + $return = $return->append($begin)->append($replace)->append($after->subString(0, $find->length())); } - - return $return; + return $return->append($haystack); } /**
 [2011-12-09 15:29 UTC] doconnor (Daniel O'Connor)
clockwerx@clockwerx-desktop:~/pear-svn-git/I18N_UnicodeString$ patch -p1 < patch-download.php\?id\=10572\&patch\=string-replace- loop\&revision\=1182272533 (Stripping trailing CRs from patch.) can't find file to patch at input line 5 Perhaps you used the wrong -p or --strip option? The text leading up to this was: -------------------------- |Index: UnicodeString.php |================================================ =================== |--- UnicodeString.php (revision 1) |+++ UnicodeString.php (working copy) -------------------------- File to patch: I18N_UnicodeString.php patching file I18N_UnicodeString.php Hunk #1 FAILED at 429. 1 out of 1 hunk FAILED -- saving rejects to file I18N_UnicodeString.php.rej
 [2011-12-09 15:41 UTC] doconnor (Daniel O'Connor)
-Status: Open +Status: Closed -Assigned To: +Assigned To: doconnor
This bug has been fixed in SVN. If this was a documentation problem, the fix will appear on pear.php.net by the end of next Sunday (CET). If this was a problem with the pear.php.net website, the change should be live shortly. Otherwise, the fix will appear in the package's next release. Thank you for the report and for helping us make PEAR better.