/[eiffelstudio]/trunk/Src/library/uuid/uuid_generator.e
ViewVC logotype

Contents of /trunk/Src/library/uuid/uuid_generator.e

Parent Directory Parent Directory | Revision Log Revision Log


Revision 56806 - (show annotations)
Mon Jan 30 20:13:52 2006 UTC (14 years ago) by patrickr
File size: 3571 byte(s)
Added uuid format validation.

1 indexing
2 description: "Generates uuids according to RFC 4122, Variant 1 0, Version 4."
3 legal: "See notice at end of class."
4 status: "See notice at end of class."
5 date: "$Date$"
6 revision: "$Revision$"
7
8 class
9 UUID_GENERATOR
10
11 feature -- Access
12
13 uuid: STRING is
14 -- Get a random uuid.
15 local
16 l_b: ARRAY [NATURAL_8]
17 i: INTEGER
18 do
19 create Result.make (36)
20 create l_b.make (1, 16)
21 from
22 i := 1
23 until
24 i > 16
25 loop
26 l_b[i] := rand_byte
27 i := i + 1
28 end
29
30 -- put in version
31 l_b[7] := l_b[7] & 0x0f | 0x40
32 -- put in variant
33 l_b[9] := l_b[9] & 0x3f | 0x80
34
35 from
36 i := 1
37 until
38 i > 16
39 loop
40 Result.append((l_b[i]).to_hex_string)
41 if i = 4 or i = 6 or i = 8 or i = 10 then
42 Result.append_character ('-')
43 end
44 i := i + 1
45 end
46 Result.to_lower
47 ensure
48 Valid_uuid: is_valid_uuid (Result)
49 end
50
51 feature -- Validation
52
53 is_valid_uuid (a_string: STRING): BOOLEAN is
54 -- Is `a_string' a valid uuid?
55 require
56 a_string_not_void: a_string /= Void
57 local
58 i: INTEGER
59 do
60 if a_string.count /= 36 then
61 Result := False
62 else
63 Result := True
64 from
65 i := 1
66 until
67 not Result or i > 36
68 loop
69 if i = 9 or i = 14 or i = 19 or i = 24 then
70 Result := a_string.item (i) = '-'
71 else
72 Result := a_string.item (i).is_hexa_digit
73 end
74 i := i +1
75 end
76 end
77 end
78
79
80 feature {NONE} -- Implementation
81
82 initialize_seed is
83 -- Initialize the seed of the random number generator.
84 require
85 rand_not_void: rand /= Void
86 local
87 l_seed: NATURAL_64
88 l_date: C_DATE
89 do
90 create l_date
91 -- Compute the seed as number of milliseconds since EPOC (January 1st 1970)
92 l_seed := l_seed + (l_date.year_now-1970).to_natural_64*12*30*24*60*60*1000
93 l_seed := l_seed + l_date.month_now.to_natural_64*30*24*60*60*1000
94 l_seed := l_seed + l_date.day_now.to_natural_64*24*60*60*1000
95 l_seed := l_seed + l_date.hour_now.to_natural_64*60*60*1000
96 l_seed := l_seed + l_date.minute_now.to_natural_64*60*1000
97 l_seed := l_seed + l_date.second_now.to_natural_64*1000
98 l_seed := l_seed + l_date.millisecond_now.to_natural_64
99 -- Use RFC 4122 trick to preserve as much meaning of `l_seed' onto an INTEGER_32.
100 rand.set_seed ((l_seed |>> 32).bit_xor (l_seed).as_integer_32 & 0x7FFFFFFF)
101 end
102
103 rand_byte: NATURAL_8 is
104 -- Get a random byte.
105 do
106 if rand_count.item = rand_count.item + 1 then
107 initialize_seed
108 rand_count.put (0)
109 else
110 rand_count.put (rand_count.item + 1)
111 end
112 Result := rand.i_th (rand_count.item).as_natural_8
113 ensure
114 rand_count_changed: old rand_count /= rand_count
115 end
116
117 rand: RANDOM is
118 -- Random number generator.
119 once
120 create Result.make
121 initialize_seed
122 ensure
123 rand_not_void: Result /= Void
124 end
125
126 rand_count: CELL [INTEGER] is
127 -- The `rand_count'-th random number was used.
128 once
129 create Result.put (0)
130 ensure
131 rand_count_not_void: Result /= Void
132 end
133
134 indexing
135 copyright: "Copyright (c) 1984-2006, Eiffel Software and others"
136 license: "Eiffel Forum License v2 (see http://www.eiffel.com/licensing/forum.txt)"
137 source: "[
138 Eiffel Software
139 356 Storke Road, Goleta, CA 93117 USA
140 Telephone 805-685-1006, Fax 805-685-6869
141 Website http://www.eiffel.com
142 Customer support http://support.eiffel.com
143 ]"
144
145 end

Properties

Name Value
svn:eol-style native
svn:keywords Author Date ID Revision

  ViewVC Help
Powered by ViewVC 1.1.23