[ccan] [PATCH] Add a set of simple version comparison helpers
Rusty Russell
rusty at rustcorp.com.au
Tue Mar 26 16:40:29 EST 2013
Peter Hutterer <peter.hutterer at who-t.net> writes:
> These version helpers help to compare major.minor style version numbers,
> without the need for open-coded and error-prone bitshifting, multiplication,
> etc.
>
> Signed-off-by: Peter Hutterer <peter.hutterer at who-t.net>
> ---
> This could be of use to others, I know I'm sick of doing the manual version
> comparison. Comments welcome.
Cool! This kind of tiny useful code is always great to have.
Random feedback, take any or none :)
1) I'd rename it from versioncmp to version, since you're using the
version prefix.
2) I'd rename version_new() to version().
3) Pass everything by value, so no need for version_cmp_numeric.
4) Avoid __ prefixes, which are reserved: CCAN_VERSION_H should be fine.
5) I prefer structs to typedefs, but that's me.
6) Please send your ssh key so I can give you commit access to your module.
IOW, something like this (didn't do rename to ccan/version).
Thanks!
Rusty.
diff --git a/ccan/versioncmp/_info b/ccan/versioncmp/_info
index d1bd62b..8d326dc 100644
--- a/ccan/versioncmp/_info
+++ b/ccan/versioncmp/_info
@@ -12,14 +12,14 @@
* License: BSD-MIT
*
* Example:
- * version_t a = version_new(1, 0);
- * version_t b = version_new(2, 2);
+ * version_t a = version(1, 0);
+ * version_t b = version(2, 2);
*
- * if (version_cmp(&a, &b) < 0)
+ * if (version_cmp(a, b) < 0)
* printf("Feature supported in version 2.2 but we have %d.%d\n",
- * version_major(&a), version_minor(&a));
+ * version_major(a), version_minor(a));
*
- * if (version_cmp_numeric(&a, 3, 4) < 0)
+ * if (version_cmp(a, version(3, 4)) < 0)
* printf("Feature only supported in version 3.4\n");
*
*/
diff --git a/ccan/versioncmp/test/run.c b/ccan/versioncmp/test/run.c
index e88513f..7f65ce0 100644
--- a/ccan/versioncmp/test/run.c
+++ b/ccan/versioncmp/test/run.c
@@ -8,62 +8,62 @@ int main(void)
plan_tests(26);
/* cmp with normal versions */
- a = version_new(1, 0);
- b = version_new(2, 0);
- ok1(version_cmp(&a, &b) < 0);
+ a = version(1, 0);
+ b = version(2, 0);
+ ok1(version_cmp(a, b) < 0);
- a = version_new(1, 1);
- ok1(version_cmp(&a, &b) < 0);
+ a = version(1, 1);
+ ok1(version_cmp(a, b) < 0);
- a = version_new(2, 0);
- ok1(version_cmp(&a, &b) == 0);
+ a = version(2, 0);
+ ok1(version_cmp(a, b) == 0);
- a = version_new(2, 1);
- ok1(version_cmp(&a, &b) > 0);
+ a = version(2, 1);
+ ok1(version_cmp(a, b) > 0);
- b = version_new(2, 1);
- ok1(version_cmp(&a, &b) == 0);
+ b = version(2, 1);
+ ok1(version_cmp(a, b) == 0);
- a = version_new(3, 0);
- ok1(version_cmp(&a, &b) > 0);
+ a = version(3, 0);
+ ok1(version_cmp(a, b) > 0);
- a = version_new(3, 1);
- ok1(version_cmp(&a, &b) > 0);
+ a = version(3, 1);
+ ok1(version_cmp(a, b) > 0);
- /* cmp_numeric */
- ok1(version_cmp_numeric(&a, 1, 0) > 0);
- ok1(version_cmp_numeric(&a, 1, 1) > 0);
- ok1(version_cmp_numeric(&a, 3, 0) > 0);
- ok1(version_cmp_numeric(&a, 3, 1) == 0);
- ok1(version_cmp_numeric(&a, 3, 2) < 0);
- ok1(version_cmp_numeric(&a, 4, 0) < 0);
- ok1(version_cmp_numeric(&a, 4, 1) < 0);
+ /* inline cmp */
+ ok1(version_cmp(a, version(1, 0)) > 0);
+ ok1(version_cmp(a, version(1, 1)) > 0);
+ ok1(version_cmp(a, version(3, 0)) > 0);
+ ok1(version_cmp(a, version(3, 1)) == 0);
+ ok1(version_cmp(a, version(3, 2)) < 0);
+ ok1(version_cmp(a, version(4, 0)) < 0);
+ ok1(version_cmp(a, version(4, 1)) < 0);
/* limits */
- a = version_new(0xFFFF, 0xFFFF);
- b = version_new(0xFFFE, 0xFFFF);
- ok1(version_cmp(&a, &b) > 0);
- ok1(version_cmp(&b, &a) < 0);
-
- b = version_new(0xFFFF, 0xFFFE);
- ok1(version_cmp(&a, &b) > 0);
- ok1(version_cmp(&b, &a) < 0);
-
- b = version_new(0xFFFF, 0xFFFF);
- ok1(version_cmp(&a, &b) == 0);
- ok1(version_cmp(&b, &a) == 0);
-
- b = version_new(0, 1);
- ok1(version_cmp(&a, &b) > 0);
- ok1(version_cmp(&b, &a) < 0);
-
- b = version_new(1, 0);
- ok1(version_cmp(&a, &b) > 0);
- ok1(version_cmp(&b, &a) < 0);
-
- b = version_new(0, 0);
- ok1(version_cmp(&a, &b) > 0);
- ok1(version_cmp(&b, &a) < 0);
+ a = version(0xFFFF, 0xFFFF);
+ b = version(0xFFFE, 0xFFFF);
+ ok1(version_cmp(a, b) > 0);
+ ok1(version_cmp(b, a) < 0);
+
+ b = version(0xFFFF, 0xFFFE);
+ ok1(version_cmp(a, b) > 0);
+ ok1(version_cmp(b, a) < 0);
+
+ b = version(0xFFFF, 0xFFFF);
+ ok1(version_cmp(a, b) == 0);
+ ok1(version_cmp(b, a) == 0);
+
+ b = version(0, 1);
+ ok1(version_cmp(a, b) > 0);
+ ok1(version_cmp(b, a) < 0);
+
+ b = version(1, 0);
+ ok1(version_cmp(a, b) > 0);
+ ok1(version_cmp(b, a) < 0);
+
+ b = version(0, 0);
+ ok1(version_cmp(a, b) > 0);
+ ok1(version_cmp(b, a) < 0);
return exit_status();
}
diff --git a/ccan/versioncmp/versioncmp.h b/ccan/versioncmp/versioncmp.h
index fe41a9b..dda8340 100644
--- a/ccan/versioncmp/versioncmp.h
+++ b/ccan/versioncmp/versioncmp.h
@@ -1,6 +1,6 @@
/*****************************************************************************
*
- * versioncmp - simple version handling functions for major.minor version
+ * version - simple version handling functions for major.minor version
* types
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
@@ -23,8 +23,8 @@
*
****************************************************************************/
-#ifndef __VERSIONCMP_H__
-#define __VERSIONCMP_H__
+#ifndef CCAN_VERSION_H
+#define CCAN_VERSION_H
#include <stdint.h>
@@ -36,24 +36,24 @@ typedef struct {
* version_major - return the major version of the given struct
* @v: the version number to obtain the major number from
*/
-static inline uint16_t version_major(const version_t* v) {
- return (v->_v & 0xFFFF0000) >> 16;
+static inline uint16_t version_major(version_t v) {
+ return (v._v & 0xFFFF0000) >> 16;
}
/**
* version_minor - return the minor version of the given struct
* @v: the version number to obtain the minor number from
*/
-static inline uint16_t version_minor(const version_t* v) {
- return v->_v & 0xFFFF;
+static inline uint16_t version_minor(const version_t v) {
+ return v._v & 0xFFFF;
}
/**
- * version_new - create a new version number
+ * version - create a new version number
* @major: major version number
* @minor: minor version number
*/
-static inline version_t version_new(uint16_t major, uint16_t minor)
+static inline version_t version(uint16_t major, uint16_t minor)
{
version_t v = { ._v = major << 16 | minor };
return v;
@@ -67,36 +67,13 @@ static inline version_t version_new(uint16_t major, uint16_t minor)
* less than b, respectively
*
* Example:
- * version_t a = version_new(1, 0);
- * version_t b = version_new(1, 3);
- * if (version_cmp(&a, &b) < 0)
+ * version_t a = version(1, 0);
+ * version_t b = version(1, 3);
+ * if (version_cmp(a, b) < 0)
* printf("b is smaller than b\n");
*/
-static inline int version_cmp(const version_t *a, const version_t *b)
+static inline int version_cmp(const version_t a, const version_t b)
{
- return (a->_v == b->_v) ? 0 : (a->_v > b->_v) ? 1 : - 1;
+ return (a._v == b._v) ? 0 : (a._v > b._v) ? 1 : -1;
}
-
-/**
- * version_cmp_numeric - compare a version number to a fixed version
- * @version: the version number
- * @major: fixed major version to compare to
- * @minor: fixed minor version to compare to
- *
- * @return a number greater, equal, or less than 0 if a is greater, equal or
- * less than major.minor, respectively
- *
- * Example:
- * version_t a = version_new(2, 3);
- * if (version_cmp_numeric(&a, 4, 0) < 0)
- * printf("a is less than version 4.0\n");
- *
- */
-static inline int
-version_cmp_numeric(version_t *version, uint16_t major, uint16_t minor)
-{
- version_t b = version_new(major, minor);
- return version_cmp(version, &b);
-}
-
-#endif /* __VERSIONCMP_H__ */
+#endif /* CCAN_VERSION_H */
More information about the ccan
mailing list