2022-03-18 10:10:26

by liwei (GF)

[permalink] [raw]
Subject: [PATCH RESEND 1/2] perf string: Add strcmp_prefix()

Add a helper to check whether a string has the given prefix.
This function is stolen from fs/xattr.c

Signed-off-by: Wei Li <[email protected]>
---
tools/perf/util/string.c | 17 +++++++++++++++++
tools/perf/util/string2.h | 1 +
2 files changed, 18 insertions(+)

diff --git a/tools/perf/util/string.c b/tools/perf/util/string.c
index f6d90cdd9225..0ed3e2d0b70f 100644
--- a/tools/perf/util/string.c
+++ b/tools/perf/util/string.c
@@ -209,6 +209,23 @@ int strtailcmp(const char *s1, const char *s2)
return 0;
}

+/**
+ * strcmp_prefix - check string for given prefix
+ * @str: the target string to check
+ * @prefix: the given prefix to match
+ *
+ * Return the rest string in @str if @str has the given @prefix, return NULL
+ * otherwise.
+ */
+const char *strcmp_prefix(const char *str, const char *prefix)
+{
+ while (*prefix && *str == *prefix) {
+ str++;
+ prefix++;
+ }
+ return *prefix ? NULL : str;
+}
+
char *asprintf_expr_inout_ints(const char *var, bool in, size_t nints, int *ints)
{
/*
diff --git a/tools/perf/util/string2.h b/tools/perf/util/string2.h
index 56c30fef9682..58929ad928f7 100644
--- a/tools/perf/util/string2.h
+++ b/tools/perf/util/string2.h
@@ -20,6 +20,7 @@ static inline bool strisglob(const char *str)
return strpbrk(str, "*?[") != NULL;
}
int strtailcmp(const char *s1, const char *s2);
+const char *strcmp_prefix(const char *str, const char *prefix);

char *asprintf_expr_inout_ints(const char *var, bool in, size_t nints, int *ints);

--
2.25.1


2022-03-23 08:50:02

by Arnaldo Carvalho de Melo

[permalink] [raw]
Subject: Re: [PATCH RESEND 1/2] perf string: Add strcmp_prefix()

Em Fri, Mar 18, 2022 at 05:22:44PM +0800, Wei Li escreveu:
> Add a helper to check whether a string has the given prefix.
> This function is stolen from fs/xattr.c

Can't you use strstarts()? See tools/include/linux/string.h.

- Arnaldo

> Signed-off-by: Wei Li <[email protected]>
> ---
> tools/perf/util/string.c | 17 +++++++++++++++++
> tools/perf/util/string2.h | 1 +
> 2 files changed, 18 insertions(+)
>
> diff --git a/tools/perf/util/string.c b/tools/perf/util/string.c
> index f6d90cdd9225..0ed3e2d0b70f 100644
> --- a/tools/perf/util/string.c
> +++ b/tools/perf/util/string.c
> @@ -209,6 +209,23 @@ int strtailcmp(const char *s1, const char *s2)
> return 0;
> }
>
> +/**
> + * strcmp_prefix - check string for given prefix
> + * @str: the target string to check
> + * @prefix: the given prefix to match
> + *
> + * Return the rest string in @str if @str has the given @prefix, return NULL
> + * otherwise.
> + */
> +const char *strcmp_prefix(const char *str, const char *prefix)
> +{
> + while (*prefix && *str == *prefix) {
> + str++;
> + prefix++;
> + }
> + return *prefix ? NULL : str;
> +}
> +
> char *asprintf_expr_inout_ints(const char *var, bool in, size_t nints, int *ints)
> {
> /*
> diff --git a/tools/perf/util/string2.h b/tools/perf/util/string2.h
> index 56c30fef9682..58929ad928f7 100644
> --- a/tools/perf/util/string2.h
> +++ b/tools/perf/util/string2.h
> @@ -20,6 +20,7 @@ static inline bool strisglob(const char *str)
> return strpbrk(str, "*?[") != NULL;
> }
> int strtailcmp(const char *s1, const char *s2);
> +const char *strcmp_prefix(const char *str, const char *prefix);
>
> char *asprintf_expr_inout_ints(const char *var, bool in, size_t nints, int *ints);
>
> --
> 2.25.1

--

- Arnaldo

2022-03-25 19:04:07

by liwei (GF)

[permalink] [raw]
Subject: Re: [PATCH RESEND 1/2] perf string: Add strcmp_prefix()



On 2022/3/23 5:22, Arnaldo Carvalho de Melo wrote:
> Em Fri, Mar 18, 2022 at 05:22:44PM +0800, Wei Li escreveu:
>> Add a helper to check whether a string has the given prefix.
>> This function is stolen from fs/xattr.c
>
> Can't you use strstarts()? See tools/include/linux/string.h.
>

Sorry, i haven't known that before, will use strstarts() in v2.

Thanks,
Wei

>
>> Signed-off-by: Wei Li <[email protected]>
>> ---
>> tools/perf/util/string.c | 17 +++++++++++++++++
>> tools/perf/util/string2.h | 1 +
>> 2 files changed, 18 insertions(+)
>>
>> diff --git a/tools/perf/util/string.c b/tools/perf/util/string.c
>> index f6d90cdd9225..0ed3e2d0b70f 100644
>> --- a/tools/perf/util/string.c
>> +++ b/tools/perf/util/string.c
>> @@ -209,6 +209,23 @@ int strtailcmp(const char *s1, const char *s2)
>> return 0;
>> }
>>
>> +/**
>> + * strcmp_prefix - check string for given prefix
>> + * @str: the target string to check
>> + * @prefix: the given prefix to match
>> + *
>> + * Return the rest string in @str if @str has the given @prefix, return NULL
>> + * otherwise.
>> + */
>> +const char *strcmp_prefix(const char *str, const char *prefix)
>> +{
>> + while (*prefix && *str == *prefix) {
>> + str++;
>> + prefix++;
>> + }
>> + return *prefix ? NULL : str;
>> +}
>> +
>> char *asprintf_expr_inout_ints(const char *var, bool in, size_t nints, int *ints)
>> {
>> /*
>> diff --git a/tools/perf/util/string2.h b/tools/perf/util/string2.h
>> index 56c30fef9682..58929ad928f7 100644
>> --- a/tools/perf/util/string2.h
>> +++ b/tools/perf/util/string2.h
>> @@ -20,6 +20,7 @@ static inline bool strisglob(const char *str)
>> return strpbrk(str, "*?[") != NULL;
>> }
>> int strtailcmp(const char *s1, const char *s2);
>> +const char *strcmp_prefix(const char *str, const char *prefix);
>>
>> char *asprintf_expr_inout_ints(const char *var, bool in, size_t nints, int *ints);
>>
>> --
>> 2.25.1
>