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

Re: [PATCH] strbuf: add and use strbuf_insertstr()

From
René Scharfe <l.s.r@web.de>
Date
Feb 9, 2020, 10:23 UTC
Message-ID
<5566a63d-f713-7580-6fc1-83286bc8955d@web.de>
In-Reply-To
<CAPig+cRmzHEQPhTzHrqqS9So63pJqQVGa1kGoWARmGU_0tn0_A@mail.gmail.com>
Am 09.02.20 um 01:53 schrieb Eric Sunshine:
Show 16 quoted lines
> On Sat, Feb 8, 2020 at 2:57 PM René Scharfe <l.s.r@web.de> wrote:
>> Add a function for inserting a C string into a strbuf.  Use it
>> throughout the source to get rid of magic string length constants and
>> explicit strlen() calls.
>>
>> Like strbuf_addstr(), implement it as an inline function to avoid the
>> implicit strlen() calls to cause runtime overhead.
>>
>> Signed-off-by: René Scharfe <l.s.r@web.de>
>> ---
>> diff --git a/notes-utils.c b/notes-utils.c
>> @@ -52,7 +52,7 @@ void commit_notes(struct repository *r, struct notes_tree *t, const char *msg)
>> -       strbuf_insert(&buf, 0, "notes: ", 7); /* commit message starts at index 7 */
>> +       strbuf_insertstr(&buf, 0, "notes: "); /* commit message starts at index 7 */
>
> Is there a reason to retain the comment which talks about magic number 7?

I didn't understand its usefulness, so I didn't dare touch it. Looking deeper, I think it already became obsolete with 13f8b72d8c (Convert commit_tree() to take strbuf as message, 2011-12-15), which included:

diff --git a/builtin/notes.c b/builtin/notes.c
index f8e437db01..5e32548cdd 100644
--- a/builtin/notes.c
+++ b/builtin/notes.c
@@ -301,12 +301,12 @@ void commit_notes(struct notes_tree *t, const char *msg)
 		return; /* don't have to commit an unchanged tree */

 	/* Prepare commit message and reflog message */
-	strbuf_addstr(&buf, "notes: "); /* commit message starts at index 7 */
 	strbuf_addstr(&buf, msg);
 	if (buf.buf[buf.len - 1] != '\n')
 		strbuf_addch(&buf, '\n'); /* Make sure msg ends with newline */

-	create_notes_commit(t, NULL, buf.buf + 7, commit_sha1);
+	create_notes_commit(t, NULL, &buf, commit_sha1);
+	strbuf_insert(&buf, 0, "notes: ", 7); /* commit message starts at index 7 */
 	update_ref(buf.buf, t->ref, commit_sha1, NULL, 0, DIE_ON_ERR);

 	strbuf_release(&buf);

René
Previous: Eric SunshineNext: René Scharfe
Message 5 of 23 in “strbuf: add and use strbuf_insertstr()”
  1. strbuf: add and use strbuf_insertstr()René Scharfe, Feb 8, 2020
  2. Taylor BlauFeb 8, 2020
  3. René ScharfeFeb 9, 2020
  4. Eric SunshineFeb 9, 2020
  5. René ScharfeFeb 9, 2020
  6. strbuf: add and use strbuf_insertstr()René Scharfe, Feb 9, 2020
  7. Eric SunshineFeb 9, 2020
  8. René ScharfeFeb 9, 2020
  9. Eric SunshineFeb 9, 2020
  10. Taylor BlauFeb 9, 2020
  11. Jeff KingFeb 10, 2020
  12. Junio C HamanoFeb 11, 2020
  13. 0/4 some more mailinfo cleanupsJeff King, Feb 11, 2020
  14. 1/4 mailinfo: treat header values as C stringsJeff King, Feb 11, 2020
  15. Eric SunshineFeb 11, 2020
  16. 2/4 mailinfo: simplify parsing of header valuesJeff King, Feb 11, 2020
  17. 3/4 mailinfo: be more liberal with header whitespaceJeff King, Feb 11, 2020
  18. 4/4 mailinfo: factor out some repeated header handlingJeff King, Feb 11, 2020
  19. René ScharfeFeb 11, 2020
  20. Jeff KingFeb 11, 2020
  21. 2/1 mailinfo: don't insert header prefix for handle_content_type()René Scharfe, Feb 10, 2020
  22. Junio C HamanoFeb 10, 2020
  23. Taylor BlauFeb 10, 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.