encode/number: set the view's remainder at exit

This commit is contained in:
Danny Robson 2018-12-16 18:06:28 +11:00
parent 1bb566fcda
commit a2ef7cb75c

View File

@ -22,18 +22,19 @@ namespace cruft::encode {
decode36 (cruft::view<char const*> &src) decode36 (cruft::view<char const*> &src)
{ {
ValueT accum = 0; ValueT accum = 0;
for (auto const i: src) {
accum *= 36;
switch (i) { auto cursor = src.begin ();
case '0'...'9': accum += i - '0'; continue; for (auto last = src.end (); cursor != last; ++cursor) {
case 'a'...'z': accum += i - 'a' + 10; continue; switch (auto i = *cursor; i) {
case 'A'...'Z': accum += i - 'A' + 10; continue; case '0'...'9': accum = accum * 36 + i - '0'; continue;
default: case 'a'...'z': accum = accum * 36 + i - 'a' + 10; continue;
throw std::invalid_argument ("invalid character"); case 'A'...'Z': accum = accum * 36 + i - 'A' + 10; continue;
} }
break;
} }
src = { cursor, src.end () };
return accum; return accum;
} }