* [PATCH] fetch: convert strncmp() with strlen() to starts_with()
@ 2024-02-24 21:47 René Scharfe
2024-02-24 23:45 ` Junio C Hamano
0 siblings, 1 reply; 5+ messages in thread
From: René Scharfe @ 2024-02-24 21:47 UTC (permalink / raw)
To: Git List
Using strncmp() and strlen() to check whether a string starts with
another one requires repeating the prefix candidate. Use starts_with()
instead, which reduces repetition and is more readable.
Signed-off-by: René Scharfe <l.s.r@web.de>
---
builtin/fetch.c | 5 ++---
1 file changed, 2 insertions(+), 3 deletions(-)
diff --git a/builtin/fetch.c b/builtin/fetch.c
index 3aedfd1bb6..0a7a1a3476 100644
--- a/builtin/fetch.c
+++ b/builtin/fetch.c
@@ -448,9 +448,8 @@ static void filter_prefetch_refspec(struct refspec *rs)
continue;
if (!rs->items[i].dst ||
(rs->items[i].src &&
- !strncmp(rs->items[i].src,
- ref_namespace[NAMESPACE_TAGS].ref,
- strlen(ref_namespace[NAMESPACE_TAGS].ref)))) {
+ starts_with(rs->items[i].src,
+ ref_namespace[NAMESPACE_TAGS].ref))) {
int j;
free(rs->items[i].src);
--
2.44.0
^ permalink raw reply related [flat|nested] 5+ messages in thread
* Re: [PATCH] fetch: convert strncmp() with strlen() to starts_with()
2024-02-24 21:47 [PATCH] fetch: convert strncmp() with strlen() to starts_with() René Scharfe
@ 2024-02-24 23:45 ` Junio C Hamano
2024-02-26 17:11 ` Junio C Hamano
0 siblings, 1 reply; 5+ messages in thread
From: Junio C Hamano @ 2024-02-24 23:45 UTC (permalink / raw)
To: René Scharfe; +Cc: Git List
René Scharfe <l.s.r@web.de> writes:
> Using strncmp() and strlen() to check whether a string starts with
> another one requires repeating the prefix candidate. Use starts_with()
> instead, which reduces repetition and is more readable.
>
> Signed-off-by: René Scharfe <l.s.r@web.de>
> ---
> builtin/fetch.c | 5 ++---
> 1 file changed, 2 insertions(+), 3 deletions(-)
>
> diff --git a/builtin/fetch.c b/builtin/fetch.c
> index 3aedfd1bb6..0a7a1a3476 100644
> --- a/builtin/fetch.c
> +++ b/builtin/fetch.c
> @@ -448,9 +448,8 @@ static void filter_prefetch_refspec(struct refspec *rs)
> continue;
> if (!rs->items[i].dst ||
> (rs->items[i].src &&
> - !strncmp(rs->items[i].src,
> - ref_namespace[NAMESPACE_TAGS].ref,
> - strlen(ref_namespace[NAMESPACE_TAGS].ref)))) {
> + starts_with(rs->items[i].src,
> + ref_namespace[NAMESPACE_TAGS].ref))) {
The original tries to check that "namespace" fully matches the
initial part of .src string, which is exactly what starts_with()
does. Makes sense.
> int j;
>
> free(rs->items[i].src);
> --
> 2.44.0
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH] fetch: convert strncmp() with strlen() to starts_with()
2024-02-24 23:45 ` Junio C Hamano
@ 2024-02-26 17:11 ` Junio C Hamano
2024-02-26 20:04 ` René Scharfe
0 siblings, 1 reply; 5+ messages in thread
From: Junio C Hamano @ 2024-02-26 17:11 UTC (permalink / raw)
To: René Scharfe; +Cc: Git List, Patrick Steinhardt
Junio C Hamano <gitster@pobox.com> writes:
>> - !strncmp(rs->items[i].src,
>> - ref_namespace[NAMESPACE_TAGS].ref,
>> - strlen(ref_namespace[NAMESPACE_TAGS].ref)))) {
>> + starts_with(rs->items[i].src,
>> + ref_namespace[NAMESPACE_TAGS].ref))) {
>
> The original tries to check that "namespace" fully matches the
> initial part of .src string, which is exactly what starts_with()
> does. Makes sense.
There are two more such instances in the codebase, easily found with
$ cat >contrib/coccinelle/starts_with.cocci <<\-EOF
@@
expression string, prefix;
@@
- !strncmp(string, prefix, strlen(prefix))
+ starts_with(string, prefix)
@@
expression string, prefix;
@@
- strncmp(string, prefix, strlen(prefix))
+ !starts_with(string, prefix)
EOF
$ make contrib/coccinelle/starts_with.cocci.patch
which finds the one you just fixed (naturally, since the Cocci patch
was written by taking inspiration from your fix).
Here is one in the reftable code. The other one is in xdiff/ I'd
rather not touch (as starts_with() is probably not available there).
diff -u -p a/reftable/refname.c b/reftable/refname.c
--- a/reftable/refname.c
+++ b/reftable/refname.c
@@ -103,7 +103,7 @@ static int modification_has_ref_with_pre
}
}
- if (strncmp(ref.refname, prefix, strlen(prefix))) {
+ if (!starts_with(ref.refname, prefix)) {
err = 1;
goto done;
}
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH] fetch: convert strncmp() with strlen() to starts_with()
2024-02-26 17:11 ` Junio C Hamano
@ 2024-02-26 20:04 ` René Scharfe
2024-02-27 7:58 ` Patrick Steinhardt
0 siblings, 1 reply; 5+ messages in thread
From: René Scharfe @ 2024-02-26 20:04 UTC (permalink / raw)
To: Junio C Hamano; +Cc: Git List, Patrick Steinhardt
Am 26.02.24 um 18:11 schrieb Junio C Hamano:
> Junio C Hamano <gitster@pobox.com> writes:
>
>>> - !strncmp(rs->items[i].src,
>>> - ref_namespace[NAMESPACE_TAGS].ref,
>>> - strlen(ref_namespace[NAMESPACE_TAGS].ref)))) {
>>> + starts_with(rs->items[i].src,
>>> + ref_namespace[NAMESPACE_TAGS].ref))) {
>>
>> The original tries to check that "namespace" fully matches the
>> initial part of .src string, which is exactly what starts_with()
>> does. Makes sense.
>
> There are two more such instances in the codebase, easily found with
>
> $ cat >contrib/coccinelle/starts_with.cocci <<\-EOF
> @@
> expression string, prefix;
> @@
> - !strncmp(string, prefix, strlen(prefix))
> + starts_with(string, prefix)
>
> @@
> expression string, prefix;
> @@
> - strncmp(string, prefix, strlen(prefix))
> + !starts_with(string, prefix)
> EOF
> $ make contrib/coccinelle/starts_with.cocci.patch
>
> which finds the one you just fixed (naturally, since the Cocci patch
> was written by taking inspiration from your fix).
>
> Here is one in the reftable code. The other one is in xdiff/ I'd
> rather not touch (as starts_with() is probably not available there).
Indeed, starts_with() is not available in xdiff/xpatience.c. That whole
file is a Git-specific extension to libxdiff, though. We could add an
include or copy the function definition.
It might make sense for performance reasons alone if there are lots of
anchors or they are very long, as with starts_with() we no longer would
have to call strlen() on all of them for each line to diff. Never used
the anchored diff algorithm, though, so I don't know whether that's a
problem in practice.
>
> diff -u -p a/reftable/refname.c b/reftable/refname.c
> --- a/reftable/refname.c
> +++ b/reftable/refname.c
> @@ -103,7 +103,7 @@ static int modification_has_ref_with_pre
> }
> }
>
> - if (strncmp(ref.refname, prefix, strlen(prefix))) {
> + if (!starts_with(ref.refname, prefix)) {
> err = 1;
> goto done;
> }
That file has another one with reversed argument order:
diff --git a/reftable/refname.c b/reftable/refname.c
index 7570e4acf9..10fc8b872d 100644
--- a/reftable/refname.c
+++ b/reftable/refname.c
@@ -78,8 +78,7 @@ static int modification_has_ref_with_prefix(struct modification *mod,
.want = prefix,
};
int idx = binsearch(mod->add_len, find_name, &arg);
- if (idx < mod->add_len &&
- !strncmp(prefix, mod->add[idx], strlen(prefix)))
+ if (idx < mod->add_len && starts_with(mod->add[idx], prefix))
goto done;
}
err = reftable_table_seek_ref(&mod->tab, &it, prefix);
^ permalink raw reply related [flat|nested] 5+ messages in thread
* Re: [PATCH] fetch: convert strncmp() with strlen() to starts_with()
2024-02-26 20:04 ` René Scharfe
@ 2024-02-27 7:58 ` Patrick Steinhardt
0 siblings, 0 replies; 5+ messages in thread
From: Patrick Steinhardt @ 2024-02-27 7:58 UTC (permalink / raw)
To: René Scharfe; +Cc: Junio C Hamano, Git List
[-- Attachment #1: Type: text/plain, Size: 4023 bytes --]
On Mon, Feb 26, 2024 at 09:04:27PM +0100, René Scharfe wrote:
> Am 26.02.24 um 18:11 schrieb Junio C Hamano:
> > Junio C Hamano <gitster@pobox.com> writes:
> >
> >>> - !strncmp(rs->items[i].src,
> >>> - ref_namespace[NAMESPACE_TAGS].ref,
> >>> - strlen(ref_namespace[NAMESPACE_TAGS].ref)))) {
> >>> + starts_with(rs->items[i].src,
> >>> + ref_namespace[NAMESPACE_TAGS].ref))) {
> >>
> >> The original tries to check that "namespace" fully matches the
> >> initial part of .src string, which is exactly what starts_with()
> >> does. Makes sense.
> >
> > There are two more such instances in the codebase, easily found with
> >
> > $ cat >contrib/coccinelle/starts_with.cocci <<\-EOF
> > @@
> > expression string, prefix;
> > @@
> > - !strncmp(string, prefix, strlen(prefix))
> > + starts_with(string, prefix)
> >
> > @@
> > expression string, prefix;
> > @@
> > - strncmp(string, prefix, strlen(prefix))
> > + !starts_with(string, prefix)
> > EOF
> > $ make contrib/coccinelle/starts_with.cocci.patch
> >
> > which finds the one you just fixed (naturally, since the Cocci patch
> > was written by taking inspiration from your fix).
> >
> > Here is one in the reftable code. The other one is in xdiff/ I'd
> > rather not touch (as starts_with() is probably not available there).
>
> Indeed, starts_with() is not available in xdiff/xpatience.c. That whole
> file is a Git-specific extension to libxdiff, though. We could add an
> include or copy the function definition.
It's kind of the same for the reftable library code. It's in this weird
in-between state where it's supposed to be usable as a library, but it
already does use some of Git's infra. I would personally be happy to use
more bits of the Git library in the reftable library, but we do need to
acknowledge that this makes it harder for other projects to use the
code.
So we should think about how we want to proceed here:
- Is the reftable library now a part of the Git codebase that can use
all of the features that we already have?
- Is the reftable library an independent part that has the goal of
being reusable for other projects?
With the ongoing libification project I certainly think that the first
option becomes more viable, as we can essentially have the best of both
worlds. The reftable library can be reused by other projects, and at the
same time we can use our own internals. But it's still going to take
quite some time until that can be fully realized, I assume.
Patrick
> It might make sense for performance reasons alone if there are lots of
> anchors or they are very long, as with starts_with() we no longer would
> have to call strlen() on all of them for each line to diff. Never used
> the anchored diff algorithm, though, so I don't know whether that's a
> problem in practice.
>
> >
> > diff -u -p a/reftable/refname.c b/reftable/refname.c
> > --- a/reftable/refname.c
> > +++ b/reftable/refname.c
> > @@ -103,7 +103,7 @@ static int modification_has_ref_with_pre
> > }
> > }
> >
> > - if (strncmp(ref.refname, prefix, strlen(prefix))) {
> > + if (!starts_with(ref.refname, prefix)) {
> > err = 1;
> > goto done;
> > }
>
>
> That file has another one with reversed argument order:
>
> diff --git a/reftable/refname.c b/reftable/refname.c
> index 7570e4acf9..10fc8b872d 100644
> --- a/reftable/refname.c
> +++ b/reftable/refname.c
> @@ -78,8 +78,7 @@ static int modification_has_ref_with_prefix(struct modification *mod,
> .want = prefix,
> };
> int idx = binsearch(mod->add_len, find_name, &arg);
> - if (idx < mod->add_len &&
> - !strncmp(prefix, mod->add[idx], strlen(prefix)))
> + if (idx < mod->add_len && starts_with(mod->add[idx], prefix))
> goto done;
> }
> err = reftable_table_seek_ref(&mod->tab, &it, prefix);
>
[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2024-02-27 7:58 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-02-24 21:47 [PATCH] fetch: convert strncmp() with strlen() to starts_with() René Scharfe
2024-02-24 23:45 ` Junio C Hamano
2024-02-26 17:11 ` Junio C Hamano
2024-02-26 20:04 ` René Scharfe
2024-02-27 7:58 ` Patrick Steinhardt
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).