-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathstring_id.cpp
55 lines (45 loc) · 1.56 KB
/
string_id.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// Copyright (C) 2014-2015 Jonathan Müller <jonathanmueller.dev@gmail.com>
// This file is subject to the license terms in the LICENSE file
// found in the top-level directory of this distribution.
#include "string_id.hpp"
#include "error.hpp"
namespace sid = foonathan::string_id;
namespace
{
void handle_collision(sid::basic_database &db, sid::hash_type hash, const char *str)
{
auto handler = sid::get_collision_handler();
auto second = db.lookup(hash);
handler(hash, str, second);
}
}
sid::string_id::string_id(string_info str, basic_database &db)
{
basic_database::insert_status status;
*this = string_id(str, db, status);
if (!status)
handle_collision(*db_, id_, str.string);
}
sid::string_id::string_id(string_info str, basic_database &db,
basic_database::insert_status &status)
: id_(detail::sid_hash(str.string)), db_(&db)
{
status = db_->insert(id_, str.string, str.length);
}
sid::string_id::string_id(const string_id &prefix, string_info str)
{
basic_database::insert_status status;
*this = string_id(prefix, str, status);
if (!status)
handle_collision(*db_, id_, str.string);
}
sid::string_id::string_id(const string_id &prefix, string_info str,
basic_database::insert_status &status)
: id_(detail::sid_hash(str.string, prefix.hash_code())), db_(prefix.db_)
{
status = db_->insert_prefix(id_, prefix.hash_code(), str.string, str.length);
}
const char* sid::string_id::string() const FOONATHAN_NOEXCEPT
{
return db_->lookup(id_);
}