Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save leoliu/1900091 to your computer and use it in GitHub Desktop.
Save leoliu/1900091 to your computer and use it in GitHub Desktop.
make-uuid for emacs 23.4
From ca4c29de33756ae459928188b442f4b1c4c66ef9 Mon Sep 17 00:00:00 2001
From: Leo <sdl.web@gmail.com>
Date: Tue, 12 Apr 2011 17:28:30 +0800
Subject: [PATCH] New primitive make-uuid for generating UUID v1 and v4
---
src/fns.c | 44 ++++++++++++++++++++++++++++++++++++++++++++
1 files changed, 44 insertions(+), 0 deletions(-)
diff --git a/src/fns.c b/src/fns.c
index c07c135a..7fbf2bf8 100644
--- a/src/fns.c
+++ b/src/fns.c
@@ -5200,6 +5200,49 @@ return a string in binary form. */)
return secure_hash (algorithm, object, start, end, Qnil, Qnil, binary);
}
+/************************************************************************
+ UUID
+************************************************************************/
+
+#include <uuid/uuid.h>
+
+DEFUN ("make-uuid", Fmake_uuid, Smake_uuid, 0, 1, 0,
+ doc: /* Generate a time-based or all-random uuid.
+VERSION is an integer specifying the algorithm version. UUID v1 and v4
+are currently supported.
+
+If VERSION is not specified or 0, the UUID will be generated based on
+high-quality randomness from /dev/urandom, if available. If it is not
+available, then an alternative algorithm which uses the current time,
+the local ethernet MAC address (if available), and random data
+generated using a pseudo-random generator. */)
+ (version)
+ Lisp_Object version;
+{
+ uuid_t uu;
+ char str[37];
+
+ if (NILP(version))
+ version = make_number (0);
+
+ CHECK_NUMBER (version);
+
+ switch (XINT(version))
+ {
+ case 0:
+ uuid_generate (uu); break;
+ case 1:
+ uuid_generate_time (uu); break;
+ case 4:
+ uuid_generate_random (uu); break;
+ default:
+ error ("Invalid UUID version");
+ }
+ uuid_unparse_lower (uu, str);
+ return make_string (str, 36);
+}
+
+
void
syms_of_fns ()
{
@@ -5385,6 +5428,7 @@ this variable. */);
defsubr (&Sbase64_decode_string);
defsubr (&Smd5);
defsubr (&Ssecure_hash);
+ defsubr (&Smake_uuid);
defsubr (&Slocale_info);
}
--
1.7.8
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment