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 ;