AW: [PEAR-DEV] [Patch] Use str_replace instead of preg_replace in
| From: | Matthias Pigulla | Date: | Mon, 20 Sep 2004 11:52:18 +0000 |
| Subject: | AW: [PEAR-DEV] [Patch] Use str_replace instead of preg_replace in | ||
| Groups: | php.pear.dev | ||
| Request: | Send a blank email to pear-dev+get-33482@lists.php.net to get a copy of this message | ||
Maybe it's not as obvious as using PCRE, but it still is easy and, most of all, faster.
Neither the old code nor the patch replaced single \r at all, but it's no problem to change
that. Both of these should work correctly:
1. $data = preg_replace("/(?<!\r)\n|\r(?!\n)/", "\r\n", $data);
2. $data = str_replace(array("\n", "\r\r\n", "\r",
"\r\n\n"), "\r\n", $data);
I did some simple tests to time execution, and it seems as if (2.) would still be better (=faster)
than (1.).
(2.) corresponds to the above PCRE:
First half of the PCRE: Rewrite \n to \r\n, except if there's a \r preceding the \n.
=> str_replace \n with \r\n. If there was a \r preceding \n, you'll get \r\r\n, str_replace
it back to \r\n. You can always be sure that the \r\r\n contains the extra \r introduced in the
first step, so you'll never match any "real" \r\r\n.
Second half: Rewrite \r to \r\n, except if there's a \n following the \r.
=> str_replace \r with \r\n. If there was a \n following the \r, you'll have \r\n\n right
now and str_replace it back to \r\n. You can always be sure that the middle \n in \r\n\n has just
been introduced and thus you can remove it safely.
Here are some more tests for the test case I submitted, and both of the above (1. and 2.) pass it
successfully.
Best regards,
Matthias
--
$test = array(
"\n" => "\r\n",
"\r\n" => "\r\n",
"\nxx" => "\r\nxx",
"xx\n" => "xx\r\n",
"xx\nxx" => "xx\r\nxx",
"\n\nxx" => "\r\n\r\nxx",
"xx\n\nxx" => "xx\r\n\r\nxx",
"xx\n\n" => "xx\r\n\r\n",
"\r\nxx" => "\r\nxx",
"xx\r\n" => "xx\r\n",
"xx\r\nxx" => "xx\r\nxx",
"\r\n\r\nxx" => "\r\n\r\nxx",
"xx\r\n\r\nxx" => "xx\r\n\r\nxx",
"xx\r\n\r\n" => "xx\r\n\r\n",
"\r\n\nxx" => "\r\n\r\nxx",
"\n\r\nxx" => "\r\n\r\nxx",
"xx\r\n\nxx" => "xx\r\n\r\nxx",
"xx\n\r\nxx" => "xx\r\n\r\nxx",
"xx\r\n\n" => "xx\r\n\r\n",
"xx\n\r\n" => "xx\r\n\r\n",
"\r" => "\r\n",
"\rxx" => "\r\nxx",
"xx\rxx" => "xx\r\nxx",
"xx\r" => "xx\r\n",
"\r\r" => "\r\n\r\n",
"\r\rxx" => "\r\n\r\nxx",
"xx\r\rxx" => "xx\r\n\r\nxx",
"xx\r\r" => "xx\r\n\r\n",
"xx\rxx\nxx\r\nxx" => "xx\r\nxx\r\nxx\r\nxx",
"\r\r\n\n" => "\r\n\r\n\r\n"
);
--
> -----Ursprüngliche Nachricht-----
> Von: Michael Wallner [mailto:mike@php.net]
> Gesendet: Montag, 20. September 2004 11:41
> An: pear-dev@lists.php.net
> Betreff: Re: [PEAR-DEV] [Patch] Use str_replace instead of
> preg_replace in
>
> Hi Chuck Hagenbuch, you wrote:
>
> > I've committed the comma-doubling change - thanks. However, I need
> > someone to indulge my lack of testing time at the moment
> and convince
> > me that the first change really does what it's supposed to do and
> > won't miss things/double newlines/etc.
>
> Well, I doubt that the new line problem can be solved with
> str_replace(), as easy as with PCRE. I think one would have
> to transform any CR and CRLF to single LF first. The patch
> also seems to miss the replacement of single CRs. Though I'd
> recommend using the following regular expression:
> preg_replace(array('/(?<!\r)\n/','/\r(?!\n)/'), "\r\n", ...)
>
> Regards,
> --
> Michael - < mike(@)php.net >
>