From 7dd0fb0086e451f38e27d01eea397004075f7f9d Mon Sep 17 00:00:00 2001 From: Tobias Christiansen Date: Thu, 22 Apr 2021 23:35:57 +0200 Subject: [PATCH] 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. --- .../Libraries/LibWeb/Layout/ListItemMarkerBox.cpp | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/Userland/Libraries/LibWeb/Layout/ListItemMarkerBox.cpp b/Userland/Libraries/LibWeb/Layout/ListItemMarkerBox.cpp index ad99a0b851..646ffe9b04 100644 --- a/Userland/Libraries/LibWeb/Layout/ListItemMarkerBox.cpp +++ b/Userland/Libraries/LibWeb/Layout/ListItemMarkerBox.cpp @@ -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)