[libgit2-glib] Add ggit_oid_has_prefix
- From: Jesse van den Kieboom <jessevdk src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [libgit2-glib] Add ggit_oid_has_prefix
- Date: Mon, 5 Jan 2015 20:10:07 +0000 (UTC)
commit a606c69fac05d4610163a970f200edec8bc46dc5
Author: Jesse van den Kieboom <jessevdk gmail com>
Date: Mon Jan 5 21:09:46 2015 +0100
Add ggit_oid_has_prefix
libgit2-glib/ggit-oid.c | 78 +++++++++++++++++++++++++++++++++++++++++++++++
libgit2-glib/ggit-oid.h | 3 ++
2 files changed, 81 insertions(+), 0 deletions(-)
---
diff --git a/libgit2-glib/ggit-oid.c b/libgit2-glib/ggit-oid.c
index 303f2ed..c185eca 100644
--- a/libgit2-glib/ggit-oid.c
+++ b/libgit2-glib/ggit-oid.c
@@ -234,4 +234,82 @@ ggit_oid_is_zero (GgitOId const *oid)
return git_oid_iszero (&oid->oid) == 1;
}
+static gint
+c_to_h (gchar c)
+{
+ if (c >= '0' && c <= '9')
+ {
+ return c - '0';
+ }
+ else if (c >= 'A' && c <= 'F')
+ {
+ return 10 + (c - 'A');
+ }
+ else if (c >= 'a' && c <= 'f')
+ {
+ return 10 + (c - 'a');
+ }
+ else
+ {
+ return -1;
+ }
+}
+
+/**
+ * ggit_oid_has_prefix:
+ * @oid: a #GgitOId.
+ * @prefix: a prefix.
+ *
+ * Check whether the object id has a given prefix. Note that the prefix is
+ * specified in hexadecimal ASCII.
+ *
+ * Returns: %TRUE if the id has the given prefix, %FALSE otherwise.
+ */
+gboolean
+ggit_oid_has_prefix (GgitOId *oid,
+ const gchar *str)
+{
+ gint i;
+
+ for (i = 0; i < GIT_OID_RAWSZ; ++i)
+ {
+ gint v1;
+
+ if (*str == '\0')
+ {
+ return TRUE;
+ }
+
+ v1 = c_to_h (*str++);
+
+ if (v1 == -1)
+ {
+ return FALSE;
+ }
+
+ if (*str != '\0')
+ {
+ gint v2;
+
+ v2 = c_to_h (*str++);
+
+ if (v2 == -1)
+ {
+ return FALSE;
+ }
+
+ if (oid->oid.id[i] != (v1 << 4) + v2)
+ {
+ return FALSE;
+ }
+ }
+ else if (oid->oid.id[i] >> 4 != v1)
+ {
+ return FALSE;
+ }
+ }
+
+ return *str == '\0';
+}
+
/* ex:set ts=8 noet: */
diff --git a/libgit2-glib/ggit-oid.h b/libgit2-glib/ggit-oid.h
index b8e123c..aca417d 100644
--- a/libgit2-glib/ggit-oid.h
+++ b/libgit2-glib/ggit-oid.h
@@ -56,6 +56,9 @@ gboolean ggit_oid_equal (GgitOId const *a,
gboolean ggit_oid_is_zero (GgitOId const *oid);
+gboolean ggit_oid_has_prefix (GgitOId *oid,
+ const gchar *prefix);
+
G_END_DECLS
#endif /* __GGIT_OID_H__ */
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]