git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [GSoC][RFC][PATCH 2/2] STRBUF_INIT_CONST: Adapting strbuf_* functions

From
Junio C Hamano <gitster@pobox.com>
Date
Feb 18, 2020, 20:46 UTC
Message-ID
<xmqq36b7k4i6.fsf@gitster-ct.c.googlers.com>
In-Reply-To
<CAHk66fskrfcJ0YFDhfimVBTJZB4um7r=GdQuM8heJdZtF8D7UQ@mail.gmail.com>
Abhishek Kumar <abhishekkumar8222@gmail.com> writes:
> I would also prefer the term "immutable" over "const" since const
> already has implications in C programming.

As long as the implication the established word conveys is what the patch wants to do, it is *better* not to invent another phrase and instead use the well-known term, no?

>> STRBUF_INIT_CONST: a new way to initialize strbuf
>
> Use imperative mood and be more specific in the commit title -
> `strbuf: Teach strbuf to initialize immutable strings`
s/T/t/;
> I feel this is self-explanatory when you go through the diff.
True.
Show 5 quoted lines
>  void strbuf_grow(struct strbuf *sb, size_t extra)
>  {
> +    if (sb->len > sb->alloc)
> +        strbuf_make_var(sb);
>      int new_buf = !sb->alloc;
This introduces decl-after-stmt error.

Also, isn't "if (sb->alloc < sb->len)" too loose a check for the new feature? AFAICS in 1/2, a strbuf that is still borrowing a const string always has sb->alloc==0. Other instances of strbuf that happens to satisify the above condition, e.g. (sb->len == 5 && sb->alloc == 1), is an error. If we are to check the condition about sb->len, shouldn't we diagnose such a case as an error, no?

> +void strbuf_make_var(struct strbuf *sb)
> +{
> +    char* str_cpy;

Isn't make_var() an implementation detail that should not leak to the strbuf API users? IOW, does it have to be extern?

In our codebase (eh, rather, in C as opposed to C++), the asterisk sticks to the identifier, not to the type.

Show 7 quoted lines
>  void strbuf_trim_trailing_newline(struct strbuf *sb)
>  {
> +    if (sb->buf[sb->len - 1] == '\n')
>> +        if (sb->len > sb->alloc)
>> +            strbuf_make_var(sb);
>
> Enclose this explicitly in braces.

Yup. Also the repetition we see is a sign that something is wrong. Perhaps adding a small inline helper

	static inline void strbuf_premutate(sb) 
	{
		if (!sb->alloc) {
			... body of strbuf_make_var() comes here ...
		}
	}
and getting rid of strbuf_make_var() would help?

As Peff, I am a bit hesitant about leaving a strbuf that hasn't been made mutable around, though.

Previous: Robear SelwansNext: Robear Selwans
Message 3 of 8 in “Re: [GSoC][RFC][PATCH 2/2] STRBUF_INIT_CONST: Adapting strbuf_* functions”
  1. Abhishek KumarFeb 18, 2020
  2. Robear SelwansFeb 18, 2020
  3. Junio C HamanoFeb 18, 2020
  4. Robear SelwansFeb 19, 2020
  5. Jeff KingFeb 19, 2020
  6. Junio C HamanoFeb 19, 2020
  7. Robear SelwansFeb 19, 2020
  8. Junio C HamanoFeb 19, 2020

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.