Line data Source code
1 :
2 : /* Copyright (c) 2011-2013, Stefan Eilemann <eile@eyescale.ch>
3 : *
4 : * This library is free software; you can redistribute it and/or modify it under
5 : * the terms of the GNU Lesser General Public License version 2.1 as published
6 : * by the Free Software Foundation.
7 : *
8 : * This library is distributed in the hope that it will be useful, but WITHOUT
9 : * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
10 : * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
11 : * details.
12 : *
13 : * You should have received a copy of the GNU Lesser General Public License
14 : * along with this library; if not, write to the Free Software Foundation, Inc.,
15 : * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16 : */
17 :
18 : #include "uint128_t.h"
19 : #include "debug.h"
20 : #include "rng.h"
21 : #include "md5/md5.hh"
22 :
23 : #include <cstdlib> // for strtoull
24 : #include <cstring> // for strcmp
25 :
26 : #ifdef _MSC_VER
27 : # define strtoull _strtoui64
28 : #endif
29 :
30 : namespace lunchbox
31 : {
32 : #ifdef LUNCHBOX_USE_V1_API
33 29 : const uint128_t uint128_t::ZERO; //!< Special identifier values
34 :
35 0 : uint128_t::uint128_t( const bool generate )
36 : : _high( 0 )
37 0 : , _low( 0 )
38 : {
39 0 : while( generate && high() == 0 )
40 : {
41 0 : RNG rng;
42 0 : high() = rng.get< uint64_t >();
43 0 : low() = rng.get< uint64_t >();
44 0 : }
45 0 : }
46 : #endif
47 :
48 90002 : uint128_t& uint128_t::operator = ( const std::string& from )
49 : {
50 90002 : if( from.empty( ))
51 : {
52 0 : _high = 0;
53 0 : _low = 0;
54 0 : return *this;
55 : }
56 :
57 90002 : char* next = 0;
58 90002 : _high = ::strtoull( from.c_str(), &next, 16 );
59 90002 : LBASSERT( next != from.c_str( ));
60 :
61 90002 : if( *next == '\0' ) // short representation, high was 0
62 : {
63 1 : _low = _high;
64 1 : _high = 0;
65 : }
66 : else
67 : {
68 90001 : if( strncmp( next, "\\058" /* utf-8 ':' */, 4 ) == 0 )
69 0 : next += 4;
70 : else
71 : {
72 90001 : LBASSERTINFO( *next == ':', from << ", " << next );
73 90001 : ++next;
74 : }
75 90001 : _low = ::strtoull( next, 0, 16 );
76 : }
77 90002 : return *this;
78 : }
79 :
80 3 : uint128_t make_uint128( const char* string )
81 : {
82 3 : const MD5 md5( (unsigned char*)string );
83 3 : uint128_t value;
84 3 : md5.raw_digest( value.high(), value.low( ));
85 3 : return value;
86 : }
87 :
88 99191 : uint128_t make_UUID()
89 : {
90 99191 : uint128_t value;
91 298652 : while( value.high() == 0 )
92 : {
93 99499 : RNG rng;
94 99398 : value.high() = rng.get< uint64_t >();
95 99990 : value.low() = rng.get< uint64_t >();
96 99999 : }
97 99978 : return value;
98 : }
99 :
100 87 : }
|