|
Boost-Commit : |
Subject: [Boost-commit] svn:boost r58262 - in trunk: boost/uuid libs/uuid/test
From: atompkins_at_[hidden]
Date: 2009-12-09 23:53:49
Author: atompkins
Date: 2009-12-09 23:53:48 EST (Wed, 09 Dec 2009)
New Revision: 58262
URL: http://svn.boost.org/trac/boost/changeset/58262
Log:
Removed compiler warning in uuid::is_nil
Fixed name_generator for different sizes of wchar_t
Text files modified:
trunk/boost/uuid/uuid.hpp | 9 +++-
trunk/boost/uuid/uuid_generators.hpp | 68 ++++++++++++++++++++++++++++++++-------
trunk/libs/uuid/test/test_generators.cpp | 2
3 files changed, 62 insertions(+), 17 deletions(-)
Modified: trunk/boost/uuid/uuid.hpp
==============================================================================
--- trunk/boost/uuid/uuid.hpp (original)
+++ trunk/boost/uuid/uuid.hpp 2009-12-09 23:53:48 EST (Wed, 09 Dec 2009)
@@ -82,9 +82,12 @@
bool is_nil() const /* throw() */
{
- // could be more efficient by stopping at the firt
- // non zero
- return (std::count(begin(), end(), 0) == static_size());
+ for(size_t i=0; i<static_size(); i++) {
+ if (data[i] != 0U) {
+ return false;
+ }
+ }
+ return true;
}
enum variant_type
Modified: trunk/boost/uuid/uuid_generators.hpp
==============================================================================
--- trunk/boost/uuid/uuid_generators.hpp (original)
+++ trunk/boost/uuid/uuid_generators.hpp 2009-12-09 23:53:48 EST (Wed, 09 Dec 2009)
@@ -19,6 +19,7 @@
#include <limits>
#include <cstring>
#include <cwchar>
+#include <boost/cstdint.hpp>
#include <boost/assert.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/random/uniform_int.hpp>
@@ -208,32 +209,72 @@
};
// generate a name-based uuid
-//TODO: add in common namesspace uuids
+// TODO: add in common namesspace uuids
class name_generator {
public:
typedef uuid result_type;
-
+
explicit name_generator(uuid const& namespace_uuid)
: namespace_uuid(namespace_uuid)
{}
-
- uuid operator()(const char* name) const {
- return operator()(name, std::strlen(name));
+
+ uuid operator()(const char* name) {
+ reset();
+ process_characters(name, std::strlen(name));
+ return sha_to_uuid();
}
- uuid operator()(const wchar_t* name) const {
- return operator()(name, std::wcslen(name)*sizeof(wchar_t));
+ uuid operator()(const wchar_t* name) {
+ reset();
+ process_characters(name, std::wcslen(name));
+ return sha_to_uuid();
}
template <typename ch, typename char_traits, typename alloc>
- uuid operator()(std::basic_string<ch, char_traits, alloc> const& name) const {
- return operator()(name.c_str(), name.length()*sizeof(ch));
+ uuid operator()(std::basic_string<ch, char_traits, alloc> const& name) {
+ reset();
+ process_characters(name.c_str(), name.length());
+ return sha_to_uuid();
+ }
+
+ uuid operator()(void const* buffer, std::size_t byte_count) {
+ reset();
+ sha.process_bytes(buffer, byte_count);
+ return sha_to_uuid();
+ };
+
+private:
+ // we convert all characters to uint32_t so that each
+ // character is 4 bytes reguardless of sizeof(char) or
+ // sizeof(wchar_t). We want the name string on any
+ // platform / compiler to generate the same uuid
+ // except for char
+ template <typename char_type>
+ void process_characters(char_type const*const characters, size_t count) {
+ BOOST_ASSERT(sizeof(uint32_t) >= sizeof(char_type));
+
+ for (size_t i=0; i<count; i++) {
+ uint32_t c = characters[i];
+ sha.process_byte( (c >> 0) && 0xFF );
+ sha.process_byte( (c >> 8) && 0xFF );
+ sha.process_byte( (c >> 16) && 0xFF );
+ sha.process_byte( (c >> 24) && 0xFF );
+ }
+ }
+
+ template<>
+ void process_characters(char const*const characters, size_t count) {
+ sha.process_bytes(characters, count);
}
- uuid operator()(void const* buffer, std::size_t byte_count) const {
- detail::sha1 sha;
+ void reset()
+ {
+ sha.reset();
sha.process_bytes(namespace_uuid.begin(), namespace_uuid.size());
- sha.process_bytes(buffer, byte_count);
+ }
+
+ uuid sha_to_uuid()
+ {
unsigned int digest[5];
sha.get_digest(digest);
@@ -257,10 +298,11 @@
*(u.begin()+6) |= 0x50; //0b01010000
return u;
- };
+ }
private:
uuid namespace_uuid;
+ detail::sha1 sha;
};
// generate a random-based uuid
Modified: trunk/libs/uuid/test/test_generators.cpp
==============================================================================
--- trunk/libs/uuid/test/test_generators.cpp (original)
+++ trunk/libs/uuid/test/test_generators.cpp 2009-12-09 23:53:48 EST (Wed, 09 Dec 2009)
@@ -83,7 +83,7 @@
{ // test name_generator
uuid dns_namespace_uuid = {{0x6b, 0xa7, 0xb8, 0x10, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8}};
uuid correct = {{0x21, 0xf7, 0xf8, 0xde, 0x80, 0x51, 0x5b, 0x89, 0x86, 0x80, 0x01, 0x95, 0xef, 0x79, 0x8b, 0x6a}};
- uuid wcorrect = {{0x5b, 0xec, 0xf2, 0x07, 0x1c, 0xd2, 0x59, 0x66, 0x8b, 0x44, 0xf5, 0x7f, 0x23, 0x0a, 0x68, 0xb9}};
+ uuid wcorrect = {{0xb9, 0x50, 0x66, 0x13, 0x2c, 0x04, 0x51, 0x2d, 0xb8, 0xfe, 0xbf, 0x8d, 0x0b, 0xa1, 0xb2, 0x71}};
name_generator gen(dns_namespace_uuid);
Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk