libcruft-util/encode/number.hpp

35 lines
940 B
C++

/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* Copyright 2018 Danny Robson <danny@nerdcruft.net>
*/
#pragma once
#include "../view.hpp"
///////////////////////////////////////////////////////////////////////////////
namespace cruft::encode {
template <typename ValueT>
ValueT
decode36 (cruft::view<char const*> &src)
{
ValueT accum = 0;
for (auto const i: src) {
accum *= 36;
switch (i) {
case '0'...'9': accum += i - '0'; continue;
case 'a'...'z': accum += i - 'a' + 10; continue;
case 'A'...'Z': accum += i - 'A' + 10; continue;
default:
throw std::invalid_argument ("invalid character");
}
}
return accum;
}
}