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

Re: [PATCH v3 1/2] ref-filter: add multiple-option parsing functions

From
Junio C Hamano <gitster@pobox.com>
Date
Jul 20, 2023, 17:59 UTC
Message-ID
<xmqq351i4g6s.fsf@gitster.g>
In-Reply-To
<ZLlmXNt2crTEIXLg@five231003>
Kousik Sanagavarapu <five231003@gmail.com> writes:
Show 8 quoted lines
> Going off in a tangent here---In yesterday's review club which discussed
> the %(decorate:<options>) patch[1], Glen suggested the possibility of
> having a single kind of a framework (used this word very loosely here)
> for parsing these multiple options since we are beginning to see them so
> often (might also help new formats which maybe added in the future). The
> fact that this wasn't done already says something about its difficulty
> as Jacob mentioned yesterday. The difficulty being we don't exactly know
> which options to parse as they differ from format to format.

Yes, while writing the sample in-code documentation for the match_atom_arg_value() function, I found its interface to force the callers to do "parse if it is 'tags'; else parse if it is 'abbrev'; and so on" hard to use. I wondered if the primitive to parse these should be modeled after config.c:parse_config_key(), i.e. the helper function takes the input and returns the split point and lengths of the components it finds in the return parameter.

As the contract between the caller and the callee is that the caller passes the beginning of "key1[=val1],key2[=val2],...", the interface may look like

	int parse_cskv(const char *to_parse,
		       const char **key, size_t *keylen,
		       const char **val, size_t *vallen,
		       const char **end);
and would be used like so:
	const char *cskv = "key1,key2=val2";
	const char *key, *val;
	size_t keylen, vallen;
	while (parse_cskv(cskv, &key, &keylen, &val, &vallen, &cskv) {
		if (!val)
			printf("valueless key '%.*s'\n",
			       (int)keylen, key);
		else
			printf("key-value pair '%.*s=%.*s'\n",
			       (int)keylen, key, (int)vallen, val);
	}
	if (*cskv)
		fprintf(stderr, "error - trailing garbage seen '%s'\n",
			cskv);
The helper's contract to the caller may look like this:
 - It expects the string to be "key" or "key=val" followed by ',' or
   '\0' (the latter ends the input, i.e. the last element of comma
   separated list).  The out variables key, val, keylen, vallen are
   used to convey to the caller where key and val are found and how
   long they are.
 - If it is a valueless "key", the out variable val is set to NULL.
 - The out variable end is updated to point at one byte after the
   element that has just been parsed.

The need for the caller to check against the list of keys it knows about in the loop still exists, but the parser may become simpler that way. I dunno.

Previous: Kousik SanagavarapuNext: Glen Choo
Message 18 of 38 in “Add new "describe" atom”
  1. 0/2 Add new "describe" atomKousik Sanagavarapu, Jul 5, 2023
  2. 1/2 ref-filter: add new "describe" atomKousik Sanagavarapu, Jul 5, 2023
  3. Junio C HamanoJul 6, 2023
  4. Kousik SanagavarapuJul 9, 2023
  5. 2/2 t6300: run describe atom tests on a different repoKousik Sanagavarapu, Jul 5, 2023
  6. 0/3 Add new "describe" atomKousik Sanagavarapu, Jul 14, 2023
  7. 1/3 ref filter: add multiple-option parsing functionsKousik Sanagavarapu, Jul 14, 2023
  8. 2/3 ref-filter: add new "describe" atomKousik Sanagavarapu, Jul 14, 2023
  9. Junio C HamanoJul 14, 2023
  10. Kousik SanagavarapuJul 15, 2023
  11. Junio C HamanoJul 15, 2023
  12. 3/3 t6300: run describe atom tests on a different repoKousik Sanagavarapu, Jul 14, 2023
  13. 0/2 Add new "describe" atomKousik Sanagavarapu, Jul 19, 2023
  14. 1/2 ref-filter: add multiple-option parsing functionsKousik Sanagavarapu, Jul 19, 2023
  15. Junio C HamanoJul 19, 2023
  16. Junio C HamanoJul 20, 2023
  17. Kousik SanagavarapuJul 20, 2023
  18. Junio C HamanoJul 20, 2023
  19. Glen ChooJul 20, 2023
  20. Junio C HamanoJul 20, 2023
  21. Glen ChooJul 21, 2023
  22. 2/2 ref-filter: add new "describe" atomKousik Sanagavarapu, Jul 19, 2023
  23. Junio C HamanoJul 19, 2023
  24. Junio C HamanoJul 20, 2023
  25. Junio C HamanoJul 20, 2023
  26. Kousik SanagavarapuJul 21, 2023
  27. 0/2 Add new "describe" atomKousik Sanagavarapu, Jul 23, 2023
  28. 1/2 ref-filter: add multiple-option parsing functionsKousik Sanagavarapu, Jul 23, 2023
  29. Junio C HamanoJul 24, 2023
  30. Kousik SanagavarapuJul 24, 2023
  31. Junio C HamanoJul 24, 2023
  32. Junio C HamanoJul 25, 2023
  33. 2/2 ref-filter: add new "describe" atomKousik Sanagavarapu, Jul 23, 2023
  34. Junio C HamanoJul 24, 2023
  35. 0/2 Add new "describe" atomKousik Sanagavarapu, Jul 25, 2023
  36. 1/2 ref-filter: add multiple-option parsing functionsKousik Sanagavarapu, Jul 25, 2023
  37. 2/2 ref-filter: add new "describe" atomKousik Sanagavarapu, Jul 25, 2023
  38. Junio C HamanoJul 25, 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.