From c115559b66f115b424fd1322db776d3815319f8c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vicen=C8=9Biu=20Ciorbaru?= Date: Wed, 29 Sep 2021 14:04:31 +0300 Subject: [PATCH] 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. --- sql/sql_string.cc | 28 ++++++++++++++++------------ sql/sql_string.h | 1 + 2 files changed, 17 insertions(+), 12 deletions(-) diff --git a/sql/sql_string.cc b/sql/sql_string.cc index fbc97ab54fb..d4639fb6bc4 100644 --- a/sql/sql_string.cc +++ b/sql/sql_string.cc @@ -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 - const char *str = Ptr+offset; - const char *search=s.ptr(); - const char *end=Ptr+str_length-s.length()+1; - const char *search_end=s.ptr()+s.length(); + const char *str= Ptr + offset; + const char *end= Ptr + str_length - search_length + 1; + const char *search_end= search + search_length; skip: while (str != end) { if (*str++ == *search) { - char *i,*j; - i=(char*) str; j=(char*) search+1; - while (j != search_end) - if (*i++ != *j++) goto skip; - return (int) (str-Ptr) -1; + char *i= (char*) str; + char *j= (char*) search + 1 ; + while (j != search_end) + if (*i++ != *j++) goto skip; + return (int) (str-Ptr) -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 */ diff --git a/sql/sql_string.h b/sql/sql_string.h index b1f02bdb43b..7bf0e2d2518 100644 --- a/sql/sql_string.h +++ b/sql/sql_string.h @@ -330,6 +330,7 @@ public: // Returns offset to substring or -1 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 int strrstr(const Binary_string &search, uint32 offset=0);