[v2,1/7] string.h: add str_has_suffix() helper for test string ends with specify string

Message ID 20240122074015.4042575-2-yebin10@huawei.com
State New
Headers
Series support '%pd' and '%pD' for print file name |

Commit Message

Ye Bin Jan. 22, 2024, 7:40 a.m. UTC
  str_has_suffix() is test string if ends with specify string.

Signed-off-by: Ye Bin <yebin10@huawei.com>
---
 include/linux/string.h | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)
  

Comments

Steven Rostedt Jan. 22, 2024, 4:07 p.m. UTC | #1
On Mon, 22 Jan 2024 15:40:09 +0800
Ye Bin <yebin10@huawei.com> wrote:

> str_has_suffix() is test string if ends with specify string.
> 
> Signed-off-by: Ye Bin <yebin10@huawei.com>
> ---
>  include/linux/string.h | 20 ++++++++++++++++++++
>  1 file changed, 20 insertions(+)
> 
> diff --git a/include/linux/string.h b/include/linux/string.h
> index 433c207a01da..e47e9597af27 100644
> --- a/include/linux/string.h
> +++ b/include/linux/string.h
> @@ -405,4 +405,24 @@ static __always_inline size_t str_has_prefix(const char *str, const char *prefix
>  	return strncmp(str, prefix, len) == 0 ? len : 0;
>  }
>  
> +/**
> + * str_has_suffix - Test if a string has a given suffix
> + * @str: The string to test
> + * @suffix: The string to see if @str ends with
> + *
> + * Returns:
> + * * strlen(@suffix) if @str ends with @suffix
> + * * 0 if @str does not end with @suffix
> + */
> +static __always_inline size_t str_has_suffix(const char *str, const char *suffix)
> +{
> +	size_t len = strlen(suffix);
> +	size_t str_len = strlen(str);
> +
> +	if (len > str_len)
> +		return 0;
> +
> +	return strncmp(str + str_len - len, suffix, len) == 0 ? len : 0;
> +}

I understand why you are returning the length of the suffix, as it matches
str_has_prefix(). But as the user of this will likely need the length of
the "str" too, we should either return the length of the string, or the
index of where the suffix was found.

/**
 [..]
 @index: The index into @str of where @suffix is if found (NULL to ignore)
 [..]
*/
static __always_inline size_t str_has_suffix(const char *str, const char *suffix, size_t *index)
{
	size_t len = strlen(suffix);
	size_t str_len = strlen(str);

	if (len > str_len)
		return 0;

	if (index)
		*index = str_len - len;

	return strncmp(str + str_len - len, suffix, len) == 0 ? len : 0;
}

-- Steve

> +
>  #endif /* _LINUX_STRING_H_ */
  
Ye Bin Jan. 23, 2024, 2:36 a.m. UTC | #2
On 2024/1/23 0:07, Steven Rostedt wrote:
> On Mon, 22 Jan 2024 15:40:09 +0800
> Ye Bin <yebin10@huawei.com> wrote:
>
>> str_has_suffix() is test string if ends with specify string.
>>
>> Signed-off-by: Ye Bin <yebin10@huawei.com>
>> ---
>>   include/linux/string.h | 20 ++++++++++++++++++++
>>   1 file changed, 20 insertions(+)
>>
>> diff --git a/include/linux/string.h b/include/linux/string.h
>> index 433c207a01da..e47e9597af27 100644
>> --- a/include/linux/string.h
>> +++ b/include/linux/string.h
>> @@ -405,4 +405,24 @@ static __always_inline size_t str_has_prefix(const char *str, const char *prefix
>>   	return strncmp(str, prefix, len) == 0 ? len : 0;
>>   }
>>   
>> +/**
>> + * str_has_suffix - Test if a string has a given suffix
>> + * @str: The string to test
>> + * @suffix: The string to see if @str ends with
>> + *
>> + * Returns:
>> + * * strlen(@suffix) if @str ends with @suffix
>> + * * 0 if @str does not end with @suffix
>> + */
>> +static __always_inline size_t str_has_suffix(const char *str, const char *suffix)
>> +{
>> +	size_t len = strlen(suffix);
>> +	size_t str_len = strlen(str);
>> +
>> +	if (len > str_len)
>> +		return 0;
>> +
>> +	return strncmp(str + str_len - len, suffix, len) == 0 ? len : 0;
>> +}
> I understand why you are returning the length of the suffix, as it matches
> str_has_prefix(). But as the user of this will likely need the length of
> the "str" too, we should either return the length of the string, or the
> index of where the suffix was found.
>
> /**
>   [..]
>   @index: The index into @str of where @suffix is if found (NULL to ignore)
>   [..]
> */
> static __always_inline size_t str_has_suffix(const char *str, const char *suffix, size_t *index)
> {
> 	size_t len = strlen(suffix);
> 	size_t str_len = strlen(str);
>
> 	if (len > str_len)
> 		return 0;
>
> 	if (index)
> 		*index = str_len - len;
>
> 	return strncmp(str + str_len - len, suffix, len) == 0 ? len : 0;
> }
>
> -- Steve
Thank you for your advice.
Return the index of where the suffix was found is useful. I'll modify it 
according to your suggestion.

-- Ye Bin
>> +
>>   #endif /* _LINUX_STRING_H_ */
> .
>
  

Patch

diff --git a/include/linux/string.h b/include/linux/string.h
index 433c207a01da..e47e9597af27 100644
--- a/include/linux/string.h
+++ b/include/linux/string.h
@@ -405,4 +405,24 @@  static __always_inline size_t str_has_prefix(const char *str, const char *prefix
 	return strncmp(str, prefix, len) == 0 ? len : 0;
 }
 
+/**
+ * str_has_suffix - Test if a string has a given suffix
+ * @str: The string to test
+ * @suffix: The string to see if @str ends with
+ *
+ * Returns:
+ * * strlen(@suffix) if @str ends with @suffix
+ * * 0 if @str does not end with @suffix
+ */
+static __always_inline size_t str_has_suffix(const char *str, const char *suffix)
+{
+	size_t len = strlen(suffix);
+	size_t str_len = strlen(str);
+
+	if (len > str_len)
+		return 0;
+
+	return strncmp(str + str_len - len, suffix, len) == 0 ? len : 0;
+}
+
 #endif /* _LINUX_STRING_H_ */