-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

[re-adding bug-gnulib; this patch does not make sense for memmem.c, since
it no longer uses knuth_morris_pratt, but might still be useful for str-kmp.h]

- -------- Original Message --------
On Tue, 2008-01-08 at 11:15 +1100, Peter Miller wrote:
| Or am I missing something subtle?

yes, I was missing something subtle.
The attached patch adds comments to explain it to me.


Regards
Peter Miller <[EMAIL PROTECTED]>
/\/\*        http://miller.emu.id.au/pmiller/

PGP public key ID: 1024D/D0EDB64D
fingerprint = AD0A C5DF C426 4F03 5D53  2BDB 18D8 A4E2 D0ED B64D
See http://www.keyserver.net or any PGP keyserver for public key.

"A fanatic is a man who consciously over compensates
a secret doubt."  -- Aldous Huxley
- --
Don't work too hard, make some time for fun as well!

Eric Blake             [EMAIL PROTECTED]
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.5 (Cygwin)
Comment: Public key at home.comcast.net/~ericblake/eblake.gpg
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFHg3Vy84KuGfSFAYARAsxTAKChH+O+TYRuRQysiI0/+/DaIEPeFQCfUqYn
XtSXewa0G56QcuQ4YyS/nj4=
=b74R
-----END PGP SIGNATURE-----
--- memmem.c	2008-01-08 11:09:47.000000000 +1100
+++ new-memmem.c	2008-01-08 11:30:59.000000000 +1100
@@ -31,7 +31,9 @@
 
 /* Knuth-Morris-Pratt algorithm.
    See http://en.wikipedia.org/wiki/Knuth-Morris-Pratt_algorithm
-   Return a boolean indicating success.  */
+   Return a boolean indicating success: false means the malloc failed,
+   true means that the comparison has been completed, and *resultp will
+   the appropriate value to return from memmem.  */
 
 static bool
 knuth_morris_pratt (const unsigned char *haystack,
@@ -148,6 +150,8 @@
   }
 
   freea (table);
+  /* The comparison has been completed, and *resultp now contains the
+     correct value to be returned from memmem.  */
   return true;
 }
 
@@ -218,7 +222,10 @@
 	       needle.  */
 	    if (comparison_count >= needle_len)
 	      {
-		/* Try the Knuth-Morris-Pratt algorithm.  */
+                /* Try the Knuth-Morris-Pratt algorithm.  Note that
+                   returning false means the malloc failed, and we
+                   will not try KMP again.  Returning true means that
+                   "result" contains the value to be returned my memmem.  */
 		const unsigned char *result;
 		if (knuth_morris_pratt (haystack, last_haystack,
 					needle - 1, needle_len, &result))

Reply via email to