This is the mail archive of the libstdc++@gcc.gnu.org mailing list for the libstdc++ project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: [Patch] To string::find


Paolo Carlini wrote:

Tomorrow I'd like to go ahead...

I'm also applying the below, which robustifies the code wrt arithmetic overflows (note, beyond the existing string::find). After the release of 4.2.0 we can consider further improving the performance (there are various options).


Paolo.

///////////////////
2006-09-05  Paolo Carlini  <pcarlini@suse.de>

	* include/bits/basic_string.tcc (find(const _CharT*, size_type,
	size_type)): Robustify.
	* include/ext/vstring.tcc (find(const _CharT*, size_type,
	size_type)): Likewise.
Index: include/ext/vstring.tcc
===================================================================
--- include/ext/vstring.tcc	(revision 116698)
+++ include/ext/vstring.tcc	(working copy)
@@ -277,10 +277,14 @@
       if (__n == 0)
 	return __pos <= __size ? __pos : npos;
 
-      for (; __pos + __n <= __size; ++__pos)
-	if (traits_type::eq(__data[__pos], __s[0])
-	    && traits_type::compare(__data + __pos + 1, __s + 1, __n - 1) == 0)
-	  return __pos;
+      if (__n <= __size)
+	{
+	  for (; __pos + __n <= __size; ++__pos)
+	    if (traits_type::eq(__data[__pos], __s[0])
+		&& traits_type::compare(__data + __pos + 1,
+					__s + 1, __n - 1) == 0)
+	      return __pos;
+	}
       return npos;
     }
 
Index: include/bits/basic_string.tcc
===================================================================
--- include/bits/basic_string.tcc	(revision 116698)
+++ include/bits/basic_string.tcc	(working copy)
@@ -716,10 +716,14 @@
       if (__n == 0)
 	return __pos <= __size ? __pos : npos;
 
-      for (; __pos + __n <= __size; ++__pos)
-	if (traits_type::eq(__data[__pos], __s[0])
-	    && traits_type::compare(__data + __pos + 1, __s + 1, __n - 1) == 0)
-	  return __pos;
+      if (__n <= __size)
+	{
+	  for (; __pos + __n <= __size; ++__pos)
+	    if (traits_type::eq(__data[__pos], __s[0])
+		&& traits_type::compare(__data + __pos + 1,
+					__s + 1, __n - 1) == 0)
+	      return __pos;
+	}
       return npos;
     }
 

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]