Re: [PATCH v2 02/21] prefix_ref_iterator: break when we leave the prefix
- From
Michael Haggerty <mhagger@alum.mit.edu>
- Date
- Sep 21, 2017, 07:42 UTC
- Message-ID
- <32b3d338-6781-a600-2c7e-65dc2818aab5@alum.mit.edu>
- In-Reply-To
- <CAGZ79kbwCAidGR3cgukdjckZVYwj+qbOikqN-e934uP1yk9Cuw@mail.gmail.com>
On 09/20/2017 10:25 PM, Stefan Beller wrote:
Show 13 quoted lines
> On Mon, Sep 18, 2017 at 11:22 PM, Michael Haggerty <mhagger@alum.mit.edu> wrote:
>> [...]
>> +/* Return -1, 0, 1 if refname is before, inside, or after the prefix. */
>> +static int compare_prefix(const char *refname, const char *prefix)
>> +{
>> + while (*prefix) {
>> + if (*refname != *prefix)
>> + return ((unsigned char)*refname < (unsigned char)*prefix) ? -1 : +1;
>
> This looks interesting.
>
> We get a signed char* , cast it to unsigned char* and then
> compare byte by byte.Not quite. We get a `char *` of unknown signedness (it's implementation-dependent), dereference it into a `char`, then cast that value to `unsigned char`. What you described would be
*(const unsigned char *)refname < *(const unsigned char *)prefix
But I assume that these two variants would result in identical assembly code.
> [...]
Michael