From 2c44f2dc3cb7274bd5ddac220727bc9f11330048 Mon Sep 17 00:00:00 2001 From: Mandar Kulkarni Date: Wed, 4 Aug 2021 23:22:55 +0530 Subject: [PATCH] Spreadsheet: Fix column index string to number conversion Fixed convert_from_string() function to return correct output. The value of a number is equal to sum of each digit multiplied by it's positional weight. --- Userland/Applications/Spreadsheet/Spreadsheet.cpp | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/Userland/Applications/Spreadsheet/Spreadsheet.cpp b/Userland/Applications/Spreadsheet/Spreadsheet.cpp index 12d2670c3ae..9025af40dec 100644 --- a/Userland/Applications/Spreadsheet/Spreadsheet.cpp +++ b/Userland/Applications/Spreadsheet/Spreadsheet.cpp @@ -90,12 +90,13 @@ static size_t convert_from_string(StringView str, unsigned base = 26, StringView VERIFY(base >= 2 && base <= map.length()); size_t value = 0; - for (size_t i = str.length(); i > 0; --i) { - auto digit_value = map.find(str[i - 1]).value_or(0); + auto const len = str.length(); + for (auto i = 0u; i < len; i++) { + size_t digit_value = map.find(str[i]).value_or(0); // NOTE: Refer to the note in `String::bijective_base_from()'. - if (i == str.length() && str.length() > 1) + if (i == 0 && len > 1) ++digit_value; - value = value * base + digit_value; + value += digit_value * AK::pow(base, len - 1 - i); } return value;