Extend Binary_string::strstr to also take in a const char pointer

One shouldn't have to instantiate a Binary_string every time a strstr
call is needed.
This commit is contained in:
Vicențiu Ciorbaru 2021-09-29 14:04:31 +03:00
parent cd873c8688
commit c115559b66
2 changed files with 17 additions and 12 deletions

View file

@ -677,33 +677,37 @@ bool String::append_with_prefill(const char *s,uint32 arg_length,
} }
int Binary_string::strstr(const Binary_string &s, uint32 offset) int Binary_string::strstr(const char *search, uint32 search_length, uint32 offset)
{ {
if (s.length()+offset <= str_length) if (search_length + offset <= str_length)
{ {
if (!s.length()) if (!search_length)
return ((int) offset); // Empty string is always found return ((int) offset); // Empty string is always found
const char *str = Ptr+offset; const char *str= Ptr + offset;
const char *search=s.ptr(); const char *end= Ptr + str_length - search_length + 1;
const char *end=Ptr+str_length-s.length()+1; const char *search_end= search + search_length;
const char *search_end=s.ptr()+s.length();
skip: skip:
while (str != end) while (str != end)
{ {
if (*str++ == *search) if (*str++ == *search)
{ {
char *i,*j; char *i= (char*) str;
i=(char*) str; j=(char*) search+1; char *j= (char*) search + 1 ;
while (j != search_end) while (j != search_end)
if (*i++ != *j++) goto skip; if (*i++ != *j++) goto skip;
return (int) (str-Ptr) -1; return (int) (str-Ptr) -1;
} }
} }
} }
return -1; return -1;
} }
int Binary_string::strstr(const Binary_string &s, uint32 offset)
{
return strstr(s.ptr(), s.length(), offset);
}
/* /*
** Search string from end. Offset is offset to the end of string ** Search string from end. Offset is offset to the end of string
*/ */

View file

@ -330,6 +330,7 @@ public:
// Returns offset to substring or -1 // Returns offset to substring or -1
int strstr(const Binary_string &search, uint32 offset=0); int strstr(const Binary_string &search, uint32 offset=0);
int strstr(const char *search, uint32 search_length, uint32 offset=0);
// Returns offset to substring or -1 // Returns offset to substring or -1
int strrstr(const Binary_string &search, uint32 offset=0); int strrstr(const Binary_string &search, uint32 offset=0);