-
Notifications
You must be signed in to change notification settings - Fork 15
/
generator.cpp
76 lines (62 loc) · 2.08 KB
/
generator.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// 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 "generator.hpp"
#include <cassert>
#include <cstring>
#include <utility>
#include "error.hpp"
namespace sid = foonathan::string_id;
bool sid::detail::handle_generation_error(std::size_t counter, const char *name, const string_id &result)
{
return get_generation_error_handler()(counter, name, result.hash_code(), result.string());
}
namespace
{
sid::string_info to_string(sid::counter_generator::state s, char *begin, char *end,
std::size_t length)
{
auto cur = end;
std::size_t i = 0;
do
{
*--cur = '0' + (s % 10);
s /= 10;
++i;
} while (s != 0u);
if (i < length)
for (; cur - 1 != begin && i < length; ++i)
*--cur = '0';
else if (length && i > length)
cur += i - length;
return sid::string_info(cur, end - cur);
}
}
sid::string_id sid::counter_generator::operator()()
{
// 4 times sizeof(state) is enough for the integer representation
static FOONATHAN_CONSTEXPR auto max_size = 4 * sizeof(state);
char string[max_size];
return detail::try_generate("foonathan::string_id::counter_generator",
[&]()
{
return to_string(counter_++, string, string + max_size, length_);
}, prefix_);
}
void sid::counter_generator::discard(unsigned long long n) FOONATHAN_NOEXCEPT
{
counter_ += n;
}
namespace
{
static FOONATHAN_CONSTEXPR char table[]
= "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxzy0123456789";
}
sid::character_table sid::character_table::alnum() FOONATHAN_NOEXCEPT
{
return {table, sizeof(table) - 1};
}
sid::character_table sid::character_table::alpha() FOONATHAN_NOEXCEPT
{
return {table, sizeof(table) - 1 - 10};
}