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

Re: [PATCH 1/1] pretty-formats: add hard truncation, without ellipsis, options

From
Taylor Blau <me@ttaylorr.com>
Date
Oct 30, 2022, 23:42 UTC
Message-ID
<Y18L9k0vAw1CArjw@nand.local>
In-Reply-To
<85f56b56-74c5-ffe5-703f-ef0c1e6d3b45@iee.email>
On Sun, Oct 30, 2022 at 10:01:47PM +0000, Philip Oakley wrote:
Show 17 quoted lines
> > If I remember correctly, strbuf_utf8_replace() supports taking NULL as
> > its last argument, though upon searching I couldn't find any callers
> > that behave that way. Can we use that instead of supplying the empty
> > string? If not, should we drop support for the NULL-as-last-argument?
>
> I wasalso  concerned about zero length strings but my brief look at the
> code indicated it could cope with a zero length string.
> The last param is `const char *subst`.
>
> I've just relooked at the code and it does have a
>
>      if (subst)
>         subst_len = strlen(subst);
>
> so it does look safe to pass NULL, though it would probably cause a
> pause for thought for readers, and isn't likely to be that much faster
> in these format scenarios.

I'm not worried at all about speed, I was just wondering if there was a more designated helper for "truncate these many UTF-8 characters from the end of a string".

It appears that passing NULL to that argument behaves the same as passing "", so I was wondering if it would be clearer to use that. But I'm fine either way. If there are no callers that pass NULL, then we should consider something like the following:

--- 8< ---
diff --git a/utf8.c b/utf8.c
index de4ce5c0e6..e8813f64fe 100644
--- a/utf8.c
+++ b/utf8.c
@@ -361,10 +361,9 @@ void strbuf_utf8_replace(struct strbuf *sb_src, int pos, int width,
 	char *src = sb_src->buf;
 	char *end = src + sb_src->len;
 	char *dst;
-	int w = 0, subst_len = 0;
+	int w = 0, subst_len;

-	if (subst)
-		subst_len = strlen(subst);
+	subst_len = strlen(subst);
 	strbuf_grow(&sb_dst, sb_src->len + subst_len);
 	dst = sb_dst.buf;
--- >8 ---

Below the context there is another "if (subst)", but that one needs to
stay since we only perform the replacement once.

Thanks,
Taylor
Previous: Philip OakleyNext: Philip Oakley
Message 5 of 30 in “extend the truncating pretty formats”
  1. 0/1 extend the truncating pretty formatsPhilip Oakley, Oct 30, 2022
  2. 1/1 pretty-formats: add hard truncation, without ellipsis, optionsPhilip Oakley, Oct 30, 2022
  3. Taylor BlauOct 30, 2022
  4. Philip OakleyOct 30, 2022
  5. Taylor BlauOct 30, 2022
  6. Philip OakleyOct 30, 2022
  7. 0/1 extend the truncating pretty formatsPhilip Oakley, Nov 1, 2022
  8. 1/1 pretty-formats: add hard truncation, without ellipsis, optionsPhilip Oakley, Nov 1, 2022
  9. Philip OakleyNov 1, 2022
  10. Taylor BlauNov 2, 2022
  11. pretty-formats: add hard truncation, without ellipsis, optionsPhilip Oakley, Nov 2, 2022
  12. pretty-formats: add hard truncation, without ellipsis, optionsPhilip Oakley, Nov 12, 2022
  13. Junio C HamanoNov 21, 2022
  14. Philip OakleyNov 21, 2022
  15. Junio C HamanoNov 22, 2022
  16. Philip OakleyNov 23, 2022
  17. Junio C HamanoNov 25, 2022
  18. Philip OakleyNov 26, 2022
  19. Philip OakleyNov 26, 2022
  20. Junio C HamanoNov 26, 2022
  21. Philip OakleyNov 28, 2022
  22. Junio C HamanoNov 29, 2022
  23. Philip OakleyDec 7, 2022
  24. Junio C HamanoDec 7, 2022
  25. 0/5 Pretty formats: Clarify column alignmentPhilip Oakley, Jan 19, 2023
  26. 3/5 doc: pretty-formats document negative column alignmentsPhilip Oakley, Jan 19, 2023
  27. 1/5 doc: pretty-formats: separate parameters from placeholdersPhilip Oakley, Jan 19, 2023
  28. 2/5 doc: pretty-formats: delineate `%<|(` parameter valuesPhilip Oakley, Jan 19, 2023
  29. 4/5 doc: pretty-formats describe use of ellipsis in truncationPhilip Oakley, Jan 19, 2023
  30. 5/5 doc: pretty-formats note wide char limitations, and add testsPhilip Oakley, Jan 19, 2023

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.