/* * 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 */ #pragma once #include "../view.hpp" #include "../maths.hpp" #include #include /////////////////////////////////////////////////////////////////////////////// namespace cruft::encode { template ValueT decode36 (cruft::view &src) { ValueT accum = 0; auto cursor = src.begin (); for (auto last = src.end (); cursor != last; ++cursor) { switch (auto i = *cursor; i) { case '0'...'9': accum = accum * 36 + i - '0'; continue; case 'a'...'z': accum = accum * 36 + i - 'a' + 10; continue; case 'A'...'Z': accum = accum * 36 + i - 'A' + 10; continue; } break; } src = { cursor, src.end () }; return accum; } template OutputT encode36 (OutputT out, ValueT val) { constexpr auto count = digits(std::numeric_limits::max (), 36u); std::array res; auto cursor = res.begin (); // Ensure we do at least one iteration so we output a zero. do { switch (auto const d = val % 36; d) { case 0 ... 9: *cursor = d + '0'; break; case 10 ...35: *cursor = d - 10 + 'a'; break; } val /= 36; ++cursor; } while (val); return std::copy ( std::make_reverse_iterator (cursor), res.rend (), out ); } }