diff options
author | Tim Ledbetter <timledbetter@gmail.com> | 2023-02-24 20:29:14 +0000 |
---|---|---|
committer | Ali Mohammad Pur <Ali.mpfard@gmail.com> | 2023-02-26 21:06:21 +0330 |
commit | 6b2f3ad6c83f255f00e73b8ec9b21aab5d4edfbd (patch) | |
tree | 5c536050c31467636552261a116e057681e7c3ac /AK/DeprecatedString.cpp | |
parent | fa34832297f006ba09af7bf376528e7f4d3a4a37 (diff) | |
download | serenity-6b2f3ad6c83f255f00e73b8ec9b21aab5d4edfbd.zip |
AK: Fix DeprecatedString::bijective_base_from for large numbers
The output of the DeprecatedString::bijective_base_from() is now
correct for numbers larger than base^2.
This makes column names display correctly in Spreadsheet.
Diffstat (limited to 'AK/DeprecatedString.cpp')
-rw-r--r-- | AK/DeprecatedString.cpp | 18 |
1 files changed, 10 insertions, 8 deletions
diff --git a/AK/DeprecatedString.cpp b/AK/DeprecatedString.cpp index 5877bc7426..437b27a71a 100644 --- a/AK/DeprecatedString.cpp +++ b/AK/DeprecatedString.cpp @@ -246,6 +246,7 @@ DeprecatedString DeprecatedString::repeated(StringView string, size_t count) DeprecatedString DeprecatedString::bijective_base_from(size_t value, unsigned base, StringView map) { + value++; if (map.is_null()) map = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"sv; @@ -255,15 +256,16 @@ DeprecatedString DeprecatedString::bijective_base_from(size_t value, unsigned ba Array<char, round_up_to_power_of_two(sizeof(size_t) * 8 + 1, 2)> buffer; size_t i = 0; do { - buffer[i++] = map[value % base]; - value /= base; - } while (value > 0); + auto remainder = value % base; + auto new_value = value / base; + if (remainder == 0) { + new_value--; + remainder = map.length(); + } - // NOTE: Weird as this may seem, the thing that comes after 'Z' is 'AA', which as a number would be '00' - // to make this work, only the most significant digit has to be in a range of (1..25) as opposed to (0..25), - // but only if it's not the only digit in the string. - if (i > 1) - --buffer[i - 1]; + buffer[i++] = map[remainder - 1]; + value = new_value; + } while (value > 0); for (size_t j = 0; j < i / 2; ++j) swap(buffer[j], buffer[i - j - 1]); |