shared_func.h: add function starts_with and ends_with

pull/37/head
yuqing 2018-07-10 12:47:12 +08:00
parent 9589d315be
commit c20b7306b0
3 changed files with 48 additions and 1 deletions

View File

@ -1,7 +1,8 @@
Version 1.39 2018-07-07 Version 1.39 2018-07-10
* add #@function REPLACE_VARS * add #@function REPLACE_VARS
* #@set value can embed %{VARIABLE} * #@set value can embed %{VARIABLE}
* shared_func.h: add function starts_with and ends_with
Version 1.38 2018-06-26 Version 1.38 2018-06-26
* connection_pool.c: set err_no to 0 when success * connection_pool.c: set err_no to 0 when success

View File

@ -2633,3 +2633,33 @@ const char *long2str(const int64_t n, char *buff, const bool thousands_separator
} }
return buff; return buff;
} }
bool starts_with(const char *str, const char *needle)
{
int str_len;
int needle_len;
str_len = strlen(str);
needle_len = strlen(needle);
if (needle_len > str_len) {
return false;
}
return memcmp(str, needle, needle_len) == 0;
}
bool ends_with(const char *str, const char *needle)
{
int str_len;
int needle_len;
int start_offset;
str_len = strlen(str);
needle_len = strlen(needle);
start_offset = str_len - needle_len;
if (start_offset < 0) {
return false;
}
return memcmp(str + start_offset, needle, needle_len) == 0;
}

View File

@ -756,6 +756,22 @@ static inline const char *long_to_comma_str(const int64_t n, char *buff)
return long2str(n, buff, true); return long2str(n, buff, true);
} }
/** if the string starts with the needle string
* parameters:
* str: the string to detect
* needle: the needle string
* return: true for starts with the needle string, otherwise false
*/
bool starts_with(const char *str, const char *needle);
/** if the string ends with the needle string
* parameters:
* str: the string to detect
* needle: the needle string
* return: true for ends with the needle string, otherwise false
*/
bool ends_with(const char *str, const char *needle);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif