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

Re: [PATCH v12 05/13] ref-filter: implement an `align` atom

From
Eric Sunshine <sunshine@sunshineco.com>
Date
Aug 20, 2015, 20:23 UTC
Message-ID
<CAPig+cRB86+1D_CrVjoLxopgf-Rd221Q49UP75hTsZSPdN6QrQ@mail.gmail.com>
In-Reply-To
<1439923052-7373-6-git-send-email-Karthik.188@gmail.com>
On Tue, Aug 18, 2015 at 2:37 PM, Karthik Nayak <karthik.188@gmail.com> wrote:
Show 42 quoted lines
> Implement an `align` atom which left-, middle-, or right-aligns the
> content between %(align:..) and %(end).
>
> It is followed by `:<width>,<position>`, where the `<position>` is
> either left, right or middle and `<width>` is the size of the area
> into which the content will be placed. If the content between
> %(align:) and %(end) is more than the width then no alignment is
> performed. e.g. to align a refname atom to the middle with a total
> width of 40 we can do: --format="%(align:middle,40)%(refname)%(end)".
>
> We now have a `handler()` for each atom_value which will be called
> when that atom_value is being parsed, and similarly an `at_end`
> function for each state which is to be called when the `end` atom is
> encountered. Using this implement the `align` atom which aligns the
> given strbuf by calling `strbuf_utf8_align()` from utf8.c
>
> This is done by assigning a 'handler' function to each atom_value and
> a related 'at_end' function for each state. The align_handler()
> defined uses strbuf_utf8_align() from utf8.c to perform the alignment.
>
> Add documentation and tests for the same.
>
> Signed-off-by: Karthik Nayak <karthik.188@gmail.com>
> ---
> diff --git a/ref-filter.c b/ref-filter.c
> index 74532d3..ecbcc5a 100644
> --- a/ref-filter.c
> +++ b/ref-filter.c
> @@ -626,6 +638,36 @@ static inline char *copy_advance(char *dst, const char *src)
>         return dst;
>  }
>
> +static void align_handler(struct ref_formatting_state *state)
> +{
> +       struct align *align = (struct align *)state->cb_data;
> +       struct strbuf s = STRBUF_INIT;
> +
> +       strbuf_utf8_align(&s, align->position, align->width, state->output.buf);
> +       strbuf_reset(&state->output);
> +       strbuf_addbuf(&state->output, &s);
> +       free(align);
> +}
Leaking 'strbuf s' here.
Also, this operation can be expressed more concisely as:
    strbuf_utf8_align(&s, ...);
    strbuf_swap(&state->output, &s);
    strbuf_release(&s);

Seeing this is also making me question my earlier suggestion of making pop_state() responsible for appending the current state's output to the previous state's output. The reason is that if align_handler() is responsible for appending to the previous state's output, then all the above string handling collapses to the one line:

    strbuf_utf8_align(&state->prev->output, ..., state->output.buf);

which is even simpler, and doesn't involve a temporary strbuf or swapping of strbuf contents.

On the other hand, it won't always be the case that all handlers can get by with such simple code, and they might end up creating temporary strbuf(s) and such anyhow, so I don't feel too strongly about it, and it can always be changed at a later date (by someone) if that approach ends up being better.

Previous: Karthik NayakNext: Karthik Nayak
Message 23 of 38 in “port tag.c to use ref-filter.c APIs”
  1. 00/13 port tag.c to use ref-filter.c APIsKarthik Nayak, Aug 18, 2015
  2. 01/13 ref-filter: move `struct atom_value` to ref-filter.cKarthik Nayak, Aug 18, 2015
  3. Matthieu MoyAug 19, 2015
  4. Karthik NayakAug 19, 2015
  5. 02/13 ref-filter: introduce ref_formatting_stateKarthik Nayak, Aug 18, 2015
  6. 03/13 ref-filter: introduce the ref_formatting_state stack machineryKarthik Nayak, Aug 18, 2015
  7. Matthieu MoyAug 19, 2015
  8. Karthik NayakAug 19, 2015
  9. Matthieu MoyAug 19, 2015
  10. Karthik NayakAug 19, 2015
  11. Karthik NayakAug 19, 2015
  12. Matthieu MoyAug 20, 2015
  13. Karthik NayakAug 20, 2015
  14. Junio C HamanoAug 20, 2015
  15. Matthieu MoyAug 20, 2015
  16. Junio C HamanoAug 20, 2015
  17. Junio C HamanoAug 19, 2015
  18. Karthik NayakAug 20, 2015
  19. Junio C HamanoAug 20, 2015
  20. 04/13 utf8: add function to align a string into given strbufKarthik Nayak, Aug 18, 2015
  21. 05/13 ref-filter: implement an `align` atomKarthik Nayak, Aug 18, 2015
  22. Karthik NayakAug 18, 2015
  23. Eric SunshineAug 20, 2015
  24. Karthik NayakAug 21, 2015
  25. 06/13 ref-filter: add option to filter out tags, branches and remotesKarthik Nayak, Aug 18, 2015
  26. 07/13 ref-filter: support printing N lines from tag annotationKarthik Nayak, Aug 18, 2015
  27. 08/13 ref-filter: add support to sort by versionKarthik Nayak, Aug 18, 2015
  28. 09/13 ref-filter: add option to match literal patternKarthik Nayak, Aug 18, 2015
  29. 10/13 tag.c: use 'ref-filter' data structuresKarthik Nayak, Aug 18, 2015
  30. Matthieu MoyAug 19, 2015
  31. Karthik NayakAug 19, 2015
  32. 11/13 tag.c: use 'ref-filter' APIsKarthik Nayak, Aug 18, 2015
  33. 12/13 tag.c: implement '--format' optionKarthik Nayak, Aug 18, 2015
  34. Matthieu MoyAug 19, 2015
  35. Karthik NayakAug 20, 2015
  36. 13/13 tag.c: implement '--merged' and '--no-merged' optionsKarthik Nayak, Aug 18, 2015
  37. Eric SunshineAug 18, 2015
  38. Karthik NayakAug 18, 2015

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.