-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbisc.hpp
223 lines (182 loc) · 6.59 KB
/
bisc.hpp
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
#include <algorithm>
#include <cctype>
#include <exception>
#include <sstream>
#include <string>
#include <string_view>
namespace bisc {
//If I wanted to depend on boost, I could use make_function_iterator.
struct counting_iterator {
using iterator_category = std::output_iterator_tag;
using value_type = void;
using difference_type = void;
using pointer = void;
using reference = void;
size_t count{0};
counting_iterator& operator=(const counting_iterator&)
{ return *this; }
template <typename T>
void operator=(const T&) {}
counting_iterator& operator*() { return *this; }
counting_iterator& operator++() { ++count; return *this; }
counting_iterator& operator++(int) { ++count; return *this; }
};
//The converter class provides for custom digit sets,
//which can support larger bases.
struct converter {
static converter& converter36();
explicit converter(std::string_view digits): my_digits(digits) {}
size_t max_base() const { return my_digits.size(); }
template <typename N>
size_t count_digits(N n) const { return count_digits(n, max_base()); }
template <typename N>
size_t count_digits(N n, const unsigned base) const;
//TODO: Make ston overload that works with string literals.
template <typename N, typename Range>
N ston(Range s) const { return ston<N>(s, max_base()); }
//TODO: Should do a move return if possible?
template <typename N, typename Range>
N ston(Range s, const unsigned base) const;
#if 0
//Need way to disambiguate between this & (s, base) overload.
template <typename N, typename First, typename Last>
N ston(First first, Last last) const
{ return ston<N>(first, last, max_base()); }
#endif
template <typename N, typename First, typename Last>
N ston(First first, Last last, const unsigned base) const;
template <typename N>
std::string ntos(N n) const { return ntos(n, max_base()); }
template <typename N>
std::string ntos(N n, const unsigned base) const;
#if 0
//Need way to disambiguate between this & (n, base) overload.
template <typename N, typename Out>
Out ntos(N n, Out out) const { return ntos(n, out, max_base()); }
#endif
template <typename N, typename Out>
Out ntos(N n, Out out, const unsigned base) const;
private:
template <typename N, typename Out>
Out ntos_(N n, Out out, const unsigned base) const;
const std::string my_digits {"0123456789abcdefghijklmnopqrstuvwxyz"};
};
converter& converter::converter36()
{
static converter base36{"0123456789abcdefghijklmnopqrstuvwxyz"};
return base36;
}
template <typename N>
size_t converter::count_digits(N n, const unsigned base) const
{
auto end { ntos_(n, counting_iterator{}, base) };
return end.count;
}
template <typename N, typename First, typename Last>
N converter::ston(First first, Last last, const unsigned base) const
{
//TODO: Don't allow leading '-' for unsigned types.
if (first == last) {
throw std::invalid_argument("Empty string not allowed");
}
if ((base < 2) or (base > max_base())) {
std::ostringstream message;
message << __func__ << ": bad base = " << base;
throw std::invalid_argument(message.str());
}
auto cton = [&digits = this->my_digits, base](char c)
{
char lower = std::tolower(c);
auto n {
(c == lower)
? digits.find(c)
: digits.find_first_of({c, lower, '\0'})
};
if (n >= base) {
std::ostringstream message;
message << __func__ << ": bad digit = " << c;
throw std::invalid_argument(message.str());
}
return n;
};
bool negative{false};
if ('+' == *first) {
++first;
} else if ('-' == *first) {
negative = true;
++first;
}
if (first == last) {
throw std::invalid_argument("No digits found");
}
N n{0};
for (; first != last; ++first) {
n = (n * base) + cton(*first);
}
//TODO: Throw exception for negative string with unsigned integer
if constexpr (std::numeric_limits<N>::is_signed) {
if (negative) return -n;
}
return n;
}
template <typename N, typename Range>
N converter::ston(Range s, const unsigned base) const
{
using std::begin, std::end; //Enable ADL
return ston<N>(begin(s), end(s), base);
}
//TODO: It would be nice if this didn't have to allocate.
template <typename N, typename Out>
Out converter::ntos(N n, Out out, const unsigned base) const
{
std::string s { ntos_(n, base) };
return std::copy(s.rbegin(), s.rend(), out);
}
template <typename N>
std::string converter::ntos(N n, const unsigned base) const
{
std::string s;
ntos_(n, std::back_inserter(s), base);
std::reverse(s.begin(), s.end());
return s;
}
template <typename N, typename Out>
Out converter::ntos_(N n, Out out, const unsigned base) const
{
if ((base < 2) or (base > max_base())) {
std::ostringstream message;
message << __func__ << ": bad base = " << base;
throw std::invalid_argument(message.str());
}
bool negative{false};
if constexpr (std::numeric_limits<N>::is_signed) {
if (n < 0) {
negative = true;
n = -n;
}
}
for (; n > 0; n /= base) {
size_t i { n % base };
*out++ = my_digits[i];
}
if (negative) *out++ = '-';
return out;
}
//TODO: ston that will infer base from prefix
//TODO: ston that will skip leading whitespace
//TODO: ntos that will add leading +
//TODO: ntos that will add base prefix
//TODO: ntos that will upcase
//TODO: ntos overflow detection?
//TODO: ston that will skip (specified?) separator characters?
template <typename N>
N ston(std::string_view s, const unsigned base = 10)
{
return converter::converter36().ston<N>(s, base);
}
template <typename N>
std::string ntos(N n, const unsigned base = 10)
{
return converter::converter36().ntos(n, base);
}
}