1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 04:47:35 +00:00

LibWeb: Conversion from number to bijective-base with alphabet.

This allows us to convert a number to a String given a bijective
(zero-less) alphabet.
So you count A,B,C,...,Y,Z,AA,AB,...

This was surprisingly very tricky!

This allows the ListItemMarker to be displayed with different (simple)
alphabets in the future.
This commit is contained in:
Tobias Christiansen 2021-04-22 23:35:57 +02:00 committed by Andreas Kling
parent 0983cd9243
commit 7dd0fb0086

View file

@ -21,6 +21,21 @@ ListItemMarkerBox::~ListItemMarkerBox()
{
}
static const String number_to_alphabet(unsigned int number, const String alphabet)
{
auto base = alphabet.length();
StringBuilder output_string;
while (number > base) {
number--;
auto remainder = number % base;
number -= remainder;
output_string.append(alphabet[remainder]);
number /= base;
};
output_string.append(alphabet[number - 1]);
return output_string.to_string().reverse();
}
void ListItemMarkerBox::paint(PaintContext& context, PaintPhase phase)
{
if (phase != PaintPhase::Foreground)