Added From_String generator. Thinking about moving generators to subpackage. Removed To_Wide_String; decided that AdaID will only deal in Strings. It is up to the user to handle the case of stuffing them into Wide strings. Fixed bullet lists in README.md
This commit is contained in:
parent
7868f1d648
commit
3c1665b46f
14
README.md
14
README.md
|
@ -2,10 +2,10 @@
|
|||
|
||||
**AdaID** is a simple Ada library for generating [UUIDs](http://en.wikipedia.org/wiki/Universally_unique_identifier). Supported operations include:
|
||||
|
||||
- Generating random UUIDs
|
||||
- Generating name-based UUIDs
|
||||
- Restoring UUIDs from a string representation (Not yet implemented).
|
||||
- Converting a UUID to a string
|
||||
- Generating random UUIDs
|
||||
- Generating name-based UUIDs
|
||||
- Restoring UUIDs from a string representation (Not yet implemented).
|
||||
- Converting a UUID to a string
|
||||
|
||||
Included is [Jonh Halleck's NIST-validated implementation of SHA1](http://home.utah.edu/~nahaj/ada/sha/) (distrubted under the GNU GPLv3).
|
||||
|
||||
|
@ -24,7 +24,7 @@ Here's an example usage:
|
|||
|
||||
Future improvements include:
|
||||
|
||||
- Moving the test program to a proper unit test framework
|
||||
- Including an install option in the build process
|
||||
- General make file improvements
|
||||
- Moving the test program to a proper unit test framework
|
||||
- Including an install option in the build process
|
||||
- General make file improvements
|
||||
|
||||
|
|
|
@ -3,8 +3,8 @@
|
|||
-- Author: Anthony Arnold
|
||||
-- License: http://www.gnu.org/licenses/gpl.txt
|
||||
|
||||
with SHA.Process_Data;
|
||||
with Ada.Numerics.Discrete_Random;
|
||||
with SHA.Process_Data; use SHA.Process_Data;
|
||||
package body AdaID is
|
||||
|
||||
-- For RNG
|
||||
|
@ -112,29 +112,6 @@ package body AdaID is
|
|||
return result;
|
||||
end To_String;
|
||||
|
||||
|
||||
function To_Wide_String(This: in UUID) return Wide_String is
|
||||
result : Wide_String(1 .. 36);
|
||||
index : Integer := 1;
|
||||
base : constant := 16;
|
||||
chars : constant Wide_String(1 .. base) := "0123456789ABCDEF";
|
||||
b : Integer;
|
||||
begin
|
||||
|
||||
for i in ByteArray'Range loop
|
||||
b := Integer(This.data(i));
|
||||
result(index) := chars(b / base + 1);
|
||||
result(index + 1) := chars(b mod base + 1);
|
||||
index := index + 2;
|
||||
|
||||
if i = 3 or i = 5 or i = 7 or i = 9 then
|
||||
result(index) := '-';
|
||||
index := index + 1;
|
||||
end if;
|
||||
end loop;
|
||||
return result;
|
||||
end To_Wide_String;
|
||||
|
||||
--=============== GENERATORS ===============--
|
||||
|
||||
--Reset a UUID to Nil
|
||||
|
@ -174,6 +151,8 @@ package body AdaID is
|
|||
|
||||
--Generate a UUID based on a name
|
||||
procedure From_Name(namespace: in UUID; name: in String; id: in out UUID) is
|
||||
use SHA.Process_Data;
|
||||
|
||||
c : Context;
|
||||
d : SHA.Digest;
|
||||
begin
|
||||
|
@ -208,5 +187,52 @@ package body AdaID is
|
|||
end From_Name;
|
||||
|
||||
|
||||
-- Generate a UUID from a string.
|
||||
-- This is not so much generation, but reconstruction
|
||||
procedure From_String(str : in String; id : in out UUID) is
|
||||
delim: constant Character := '-';
|
||||
open : constant Character := '{';
|
||||
close : constant Character := '}';
|
||||
|
||||
-- expect dashes if str is 36 or 38 in length
|
||||
dashed: constant Boolean := str'Length = 36 or str'Length = 38;
|
||||
braced: constant Boolean := str(str'First) = open and
|
||||
str(str'Last) = close;
|
||||
|
||||
idx : Integer := 0;
|
||||
start, rel : Integer;
|
||||
|
||||
begin
|
||||
|
||||
-- Check that length is valid
|
||||
if not dashed and str'Length /= 32 and str'Length /= 34 then
|
||||
raise Invalid_String;
|
||||
end if;
|
||||
|
||||
-- Check that brace are valid
|
||||
start := str'First;
|
||||
if not braced and
|
||||
(str(str'First) = open or str(str'Last) = close) then
|
||||
raise Invalid_String; -- only one brace present
|
||||
elsif braced then
|
||||
start := str'First + 1;
|
||||
end if;
|
||||
|
||||
idx := start;
|
||||
|
||||
-- Grab each pair and stuff into byte
|
||||
for i in ByteArray'Range loop
|
||||
rel := idx - start;
|
||||
if dashed and (rel = 8 or rel = 13 or rel = 18 or rel = 23) then
|
||||
if str(idx) /= delim then
|
||||
raise Invalid_String; -- expected '-'
|
||||
end if;
|
||||
idx := idx + 1;
|
||||
end if;
|
||||
-- Convert to byte
|
||||
id.data(i) := Byte'Value("16#" & str(idx .. idx+1) & "#");
|
||||
idx := idx + 2;
|
||||
end loop;
|
||||
end From_String;
|
||||
|
||||
end AdaID;
|
||||
|
|
|
@ -36,6 +36,9 @@ package AdaID is
|
|||
Microsoft,
|
||||
Future
|
||||
);
|
||||
|
||||
-- Exception for string parsing
|
||||
Invalid_String : exception;
|
||||
|
||||
-- The main type for the package
|
||||
type UUID is new Ada.Finalization.Controlled with
|
||||
|
@ -60,7 +63,6 @@ package AdaID is
|
|||
|
||||
--Convert the UUID to a string
|
||||
function To_String(This: in UUID) return String;
|
||||
function To_Wide_String(This: in UUID) return Wide_String;
|
||||
|
||||
-------------------- GENERATORS -----------------------
|
||||
-- Set a UUID to Nil
|
||||
|
@ -74,8 +76,7 @@ package AdaID is
|
|||
|
||||
-- Generate a UUID from a string.
|
||||
-- This is not so much generation, but reconstruction
|
||||
--procedure From_String(str : in String; id : in out UUID);
|
||||
--procedure From_Wide_String(str : in Wide_String; id : in out UUID);
|
||||
procedure From_String(str : in String; id : in out UUID);
|
||||
|
||||
private
|
||||
--Default "constructor", initializes to NIL
|
||||
|
|
|
@ -48,7 +48,7 @@ begin
|
|||
|
||||
|
||||
Ada.Text_IO.Put("Testing GetHashValue: ");
|
||||
if Get_Has_Value(id) /= Get_Hash_Value(id2) then
|
||||
if Get_Hash_Value(id) /= Get_Hash_Value(id2) then
|
||||
Ada.Text_IO.Put_Line("Failed");
|
||||
else
|
||||
Ada.Text_IO.Put_Line("Passed");
|
||||
|
@ -77,16 +77,24 @@ begin
|
|||
|
||||
|
||||
|
||||
Ada.Text_IO.Put("Testing ToString: ");
|
||||
From_Name(id3, "UUID", id);
|
||||
Ada.Text_IO.Put_Line(To_String(id));
|
||||
--if /= "6ba7b810-9dad-11d1-80b4-00c04fd430c8" then
|
||||
-- Ada.Text_IO.Put_Line("Failed");
|
||||
--else
|
||||
-- Ada.Text_IO.Put_Line("Passed");
|
||||
--end if;
|
||||
Ada.Text_IO.Put("Testing To_String: ");
|
||||
if To_String(id)'Length /= 36 then
|
||||
Ada.Text_IO.Put_Line("Failed");
|
||||
else
|
||||
Ada.Text_IO.Put_Line("Passed");
|
||||
end if;
|
||||
|
||||
|
||||
|
||||
|
||||
Ada.Text_IO.Put("Testing From_String: ");
|
||||
From_String("{"&To_String(id)&"}", id2);
|
||||
if id /= id2 then
|
||||
Ada.Text_IO.Put_Line("Failed");
|
||||
else
|
||||
Ada.Text_IO.Put_Line("Passed");
|
||||
end if;
|
||||
|
||||
Ada.Text_IO.Put_Line("Testing Complete");
|
||||
|
||||
end;
|
||||
|
||||
|
|
Loading…
Reference in New Issue