AW: [PEAR-DEV] [Patch] Use str_replace instead of preg_replace inNet_SMTP/SMTP.php
| From: | Matthias Pigulla | Date: | Sun, 19 Sep 2004 16:18:56 +0000 |
| Subject: | AW: [PEAR-DEV] [Patch] Use str_replace instead of preg_replace inNet_SMTP/SMTP.php | ||
| Groups: | php.pear.dev | ||
| Request: | Send a blank email to pear-dev+get-33450@lists.php.net to get a copy of this message | ||
The old code needed two preg calls to handle the case of two consecutive newlines
("...\n\n..."), as the first preg matched the first one, replaced it and continued
matching at the second newline without "looking back". Upon inspecting the second newline,
there was no char at all in front of it, so it was not matched. Thus \n\n might remain after the
first preg call and was treated by the second preg.
(I just noticed that an input string starting with \n would never have been rewritten correctly at
the beginning, so the old code was even broken for that special case.)
Basically, what you would want to use (instead of the two pregs) is just one preg with an assertion,
something like
/(?<!\r)\n/ => \r\n.
Using str_replace instead of such a preg should be correct:
1. Every \n will be replaced by \r\n, now matter where it occurs (at the beginning of the input or
preceding/following another \r or \n).
2. The only exception (as stated by the assertion) are \n immediately preceded by \r. These \r\n
will always be \r\r\n after the first step, so \r\r\n => \r\n will remove the extra \r
introduced. The other way round, you can always be sure that after step one, every
"\r\r\n" contains an extra "\r", so removing one of them will always correct the
extra \r introduced in step one and will never remove any \r that is part of the input string.
Was that a clarification or obfuscation? ;)
Matthias
> -----Ursprüngliche Nachricht-----
> Von: Chuck Hagenbuch [mailto:chuck@horde.org]
> Gesendet: Sonntag, 19. September 2004 17:20
> An: pear-dev@lists.php.net
> Betreff: Re: [PEAR-DEV] [Patch] Use str_replace instead of
> preg_replace inNet_SMTP/SMTP.php
>
>
> 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.