54 lines
866 B
C++
54 lines
866 B
C++
#include "series.h"
|
|
|
|
#include <algorithm>
|
|
#include <stdexcept>
|
|
|
|
namespace series {
|
|
|
|
using namespace std;
|
|
|
|
|
|
namespace {
|
|
|
|
inline int char_to_digit(char ch)
|
|
{
|
|
if (!isdigit(ch))
|
|
throw domain_error("not a digit");
|
|
|
|
return ch - '0';
|
|
}
|
|
|
|
}
|
|
|
|
|
|
digits_t digits(const string &input)
|
|
{
|
|
digits_t result(input.size());
|
|
|
|
transform(input.cbegin(), input.cend(), result.begin(), char_to_digit);
|
|
|
|
return result;
|
|
}
|
|
|
|
slice_t slice(const string &input, size_t size)
|
|
{
|
|
if (size > input.size())
|
|
throw domain_error("not enough digits to slice");
|
|
|
|
if (size == 0)
|
|
return slice_t();
|
|
|
|
digits_t input_digits = digits(input);
|
|
slice_t result;
|
|
|
|
auto begin = input_digits.cbegin();
|
|
auto end = begin + size - 1;
|
|
|
|
while (end != input_digits.cend())
|
|
result.push_back(digits_t(begin++, ++end));
|
|
|
|
return result;
|
|
}
|
|
|
|
|
|
}
|