AK: Add reinterpret_as_octal()

This is useful for parsing user-provided integers that should be
interpreted as octals.
This commit is contained in:
implicitfield 2024-04-29 18:12:36 +04:00 committed by Andrew Kaster
parent 57f0ea186e
commit f923016e0b
Notes: sideshowbarker 2024-07-17 07:11:12 +09:00

View file

@ -75,4 +75,16 @@ constexpr bool is_power_of(U x)
return true;
}
template<Unsigned T>
constexpr T reinterpret_as_octal(T decimal)
{
T result = 0;
T n = 0;
while (decimal > 0) {
result += pow<T>(8, n++) * (decimal % 10);
decimal /= 10;
}
return result;
}
}