Subject: Re: [PATCH v2 2/4] string-list: replace negative index encoding with "exact_match" parameter

From: Patrick Steinhardt <[email-masked]>
Date: Tue, 23 Sep 2025 10:14:55 +0200

On Wed, Sep 17, 2025 at 05:19:54PM +0800, shejialuo wrote:
> We would return negative index to indicate exact match by converting the
> original positive index to be "-1 - index" in
> "string_list_find_insert_index", which requires callers to decode this
> information. This approach has several limitations:
> 
> 1. It prevents us from using the full range of size_t, which is
>    necessary for large string list.

I guess this is more of a theoretical concern. We probably wouldn't
handle it well when our list had 2 billion entries anyway.

> 2. Using int for indices while other parts of the codebase use size_t
>    creates signed comparison warnings when these values are compared.

Yup.

I think that the required juggling around negative indices is another
factor here. It's somewhat weird, and while existing callers all handle
this correct I think that it makes for a suboptimal interface.

> To address these limitations, change the function to return size_t for
> the index value and use a separate bool parameter to indicate whether
> the index refers to an existing entry or an insertion point.
> 
> In some cases, the callers of "string_list_find_insert_index" only need
> the index position and don't care whether an exact match is found.
> However, "get_entry_index" currently requires a non-NULL "exact_match"
> parameter, forcing these callers to declare unnecessary variables.
> Let's allow callers to pass NULL for the "exact_match" parameter when
> they don't need this information, reducing unnecessary variable
> declarations in calling code.

Makes sense.

I don't really think that my above comments need to be addressed, and
the other patches in this series look good to me. Thanks!

Patrick