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

[PATCH 4/8] Add a utility function to make parsing hex values easier.

From
brian m. carlson <sandals@crustytoothpaste.net>
Date
Jun 9, 2015, 16:28 UTC
Message-ID
<1433867316-663554-5-git-send-email-sandals@crustytoothpaste.net>
In-Reply-To
<1433867316-663554-1-git-send-email-sandals@crustytoothpaste.net>

get_oid_hex is already available for parsing hex object IDs into struct object_id, but parsing code still must hard-code the number of bytes read. Introduce parse_oid_hex, which accepts an optional length, and also returns the number of bytes parsed on success, or 0 on failure. This makes it easier for code not to assume fixed values when parsing, and to move to larger hash functions in the future.

Signed-off-by: brian m. carlson <sandals@crustytoothpaste.net>
---
 cache.h | 9 +++++++++
 hex.c   | 7 +++++++
 2 files changed, 16 insertions(+)
diff --git a/cache.h b/cache.h
index fa1f067..f3b829f 100644
--- a/cache.h
+++ b/cache.h
@@ -1012,6 +1012,15 @@ extern int for_each_abbrev(const char *prefix, each_abbrev_fn, void *);
 extern int get_sha1_hex(const char *hex, unsigned char *sha1);
 extern int get_oid_hex(const char *hex, struct object_id *sha1);
 
+/*
+ * Like get_oid_hex, but accepts an optional length argument, which may be -1
+ * if the string is terminated by a non-hex character.  As with get_oid_hex,
+ * reading stops if a NUL is encountered.  Returns the number of characters
+ * read (40) on success and 0 on failure.  This is designed to be easier to
+ * use for parsing data than get_oid_hex.
+ */
+extern int parse_oid_hex(const char *hex, int len, struct object_id *oid);
+
 extern char *sha1_to_hex(const unsigned char *sha1);	/* static buffer result! */
 extern char *oid_to_hex(const struct object_id *oid);	/* same static buffer as sha1_to_hex */
 extern int read_ref_full(const char *refname, int resolve_flags,
diff --git a/hex.c b/hex.c
index 899b74a..ba196d7 100644
--- a/hex.c
+++ b/hex.c
@@ -61,6 +61,13 @@ int get_oid_hex(const char *hex, struct object_id *oid)
 	return get_sha1_hex(hex, oid->hash);
 }
 
+int parse_oid_hex(const char *hex, int len, struct object_id *oid)
+{
+	if (len != -1 && len < GIT_SHA1_HEXSZ)
+		return 0;
+	return get_sha1_hex(hex, oid->hash) ? GIT_SHA1_HEXSZ : 0;
+}
+
 char *sha1_to_hex(const unsigned char *sha1)
 {
 	static int bufno;
-- 
2.4.0
Previous: Michael HaggertyNext: Michael Haggerty
Message 8 of 23 in “object_id part 2”
  1. 0/8 object_id part 2brian m. carlson, Jun 9, 2015
  2. 1/8 refs: convert some internal functions to use object_idbrian m. carlson, Jun 9, 2015
  3. 2/8 sha1_file: introduce has_object_file helper.brian m. carlson, Jun 9, 2015
  4. Duy NguyenJun 10, 2015
  5. brian m. carlsonJun 10, 2015
  6. 3/8 Convert struct ref to use object_id.brian m. carlson, Jun 9, 2015
  7. Michael HaggertyJun 11, 2015
  8. 4/8 Add a utility function to make parsing hex values easier.brian m. carlson, Jun 9, 2015
  9. Michael HaggertyJun 11, 2015
  10. 5/8 add_sought_entry_mem: convert to struct object_idbrian m. carlson, Jun 9, 2015
  11. 6/8 parse_fetch: convert to use struct object_idbrian m. carlson, Jun 9, 2015
  12. 7/8 ref_newer: convert to use struct object_idbrian m. carlson, Jun 9, 2015
  13. Junio C HamanoJun 10, 2015
  14. brian m. carlsonJun 10, 2015
  15. brian m. carlsonJun 11, 2015
  16. Junio C HamanoJun 11, 2015
  17. brian m. carlsonJun 11, 2015
  18. Junio C HamanoJun 11, 2015
  19. brian m. carlsonJun 12, 2015
  20. Junio C HamanoJun 12, 2015
  21. brian m. carlsonJun 12, 2015
  22. Michael HaggertyJun 13, 2015
  23. brian m. carlsonJun 13, 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.