From f9fb12d81c1697d8d95db8d04150f69a80d19709 Mon Sep 17 00:00:00 2001 From: John Benediktsson Date: Thu, 19 Apr 2012 14:04:42 -0700 Subject: [PATCH] shortuuid: implementing "shortuuid" concept. --- README | 3 +++ shortuuid/shortuuid.factor | 21 +++++++++++++++++++++ 2 files changed, 24 insertions(+) create mode 100644 shortuuid/shortuuid.factor diff --git a/README b/README index fef82e77..56488337 100644 --- a/README +++ b/README @@ -160,6 +160,9 @@ robohash: second-color: implements a "colored" seconds hash +shortuuid + implements "shortuuid" concept + simple-rpg: simple role-playing game example diff --git a/shortuuid/shortuuid.factor b/shortuuid/shortuuid.factor new file mode 100644 index 00000000..b5567937 --- /dev/null +++ b/shortuuid/shortuuid.factor @@ -0,0 +1,21 @@ +! Copyright (C) 2010 John Benediktsson +! See http://factorcode.org/license.txt for BSD license + +USING: kernel math sequences ; + +IN: shortuuid + +CONSTANT: alphabet +"23456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz" + +: (encode-uuid) ( n -- n c ) + alphabet [ length /mod ] [ nth ] bi ; + +: encode-uuid ( uuid -- shortuuid ) + [ dup 0 > ] [ (encode-uuid) ] "" produce-as nip ; + +: (decode-uuid) ( n c -- n ) + alphabet index [ alphabet length * ] dip + ; + +: decode-uuid ( shortuuid -- uuid ) + 0 [ (decode-uuid) ] reduce ;