From: Phillip Wood Date: Sun, 30 May 2021 13:05:55 GMT Subject: Re: [PATCH 1/2] [GSOC] ref-filter: add %(raw) atom Message-ID: <2c28515c-89b1-79dd-35a2-492eac6b7347@gmail.com> In-Reply-To: Hi ZheNing On 30/05/2021 07:29, ZheNing Hu wrote: > Felipe Contreras 于2021年5月29日周六 下午11:24写道: >> >> Phillip Wood wrote: >>> On 27/05/2021 17:36, Felipe Contreras wrote: >>>> ZheNing Hu via GitGitGadget wrote: >>>> [...] >>>>> +static int memcasecmp(const void *vs1, const void *vs2, size_t n) >>>> >>>> Why void *? We can delcare as char *. >>> >>> If you look at how this function is used you'll see >>> int (*cmp_fn)(const void *, const void *, size_t); >>> cmp_fn = s->sort_flags & REF_SORTING_ICASE >>> ? memcasecmp : memcmp; >> >> Yeah, but why? >> >> We know we are comparing two char *. Presumably the reason is that >> memcmp and memcasecmp use void *, but that could be remedied with: >> >> cmp_fn = (int (*)(const char *, const char *, size_t))memcmp; >> >> That way the same cmp_fn could be used for the two cases. >> >> Either way I don't care particularly much. It also could be possible to >> use void * and do the casting in tolower(). >> > > I agree with Phillip's point of view here: > It would be better for memcasecmp and memcmp to be consistent. > >>>> (and I personally prefer lower to upper) >>> >>> We should be using tolower() as that is what POSIX specifies for >>> strcasecmp() [1] which we are trying to emulate and there are cases[2] where >>> (tolower(c1) == tolower(c2)) != (toupper(c1) == toupper(c2)) >> >> That's true. >> > > How about something like this: > > static int memcasecmp(const void *vs1, const void *vs2, size_t n) > { > - size_t i; > - const char *s1 = (const char *)vs1; > - const char *s2 = (const char *)vs2; > - > - for (i = 0; i < n; i++) { > - unsigned char u1 = s1[i]; > - unsigned char u2 = s2[i]; > - int U1 = toupper (u1); > - int U2 = toupper (u2); > - int diff = (UCHAR_MAX <= INT_MAX ? U1 - U2 > - : U1 < U2 ? -1 : U2 < U1); > + const char *s1 = (const void *)vs1; > + const char *s2 = (const void *)vs2; I think the new version looks fine apart from these casts. vs1 declared as 'const void *' in the function signature so this cast does not do anything. You could cast using (const char *) instead if you wanted but that is not required as you can assign a 'const void *' to 'const whatever *' without a cast. Best Wishes Phillip > + const char *end = s1 + n; > + > + for (; s1 < end; s1++, s2++) { > + int diff = tolower(*s1) - tolower(*s2); > if (diff) > return diff; > } > } > >> -- >> Felipe Contreras > > Thanks. > -- > ZheNing Hu >