archive mirror
 help / color / mirror / Atom feed
From: Junio C Hamano <>
To: Abhishek Kumar <>
Subject: Re: [GSoC][RFC][PATCH 2/2] STRBUF_INIT_CONST: Adapting strbuf_* functions
Date: Tue, 18 Feb 2020 12:46:41 -0800	[thread overview]
Message-ID: <> (raw)
In-Reply-To: <> (Abhishek Kumar's message of "Tue, 18 Feb 2020 15:00:00 +0530")

Abhishek Kumar <> 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`


> I feel this is self-explanatory when you go through the diff.


>  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.

>  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.

  parent reply	other threads:[~2020-02-18 20:46 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-02-18  9:30 [GSoC][RFC][PATCH 2/2] STRBUF_INIT_CONST: Adapting strbuf_* functions Abhishek Kumar
2020-02-18 14:42 ` Robear Selwans
2020-02-18 20:46 ` Junio C Hamano [this message]
2020-02-19  1:43   ` Robear Selwans
2020-02-19  2:05     ` Jeff King
2020-02-19  3:13     ` Junio C Hamano
2020-02-19  4:34       ` Robear Selwans
2020-02-19 10:44         ` Junio C Hamano
  -- strict thread matches above, loose matches on Subject: below --
2020-02-18  4:18 [GSoC][RFC][PATCH 0/2] STRBUF_INIT_CONST Cover Robear Selwans
2020-02-18  4:18 ` [GSoC][RFC][PATCH 2/2] STRBUF_INIT_CONST: Adapting strbuf_* functions Robear Selwans

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \ \ \ \ \ \ \ \ \ \

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).