From 1f9725e5fd82ce697b4c607e0fd32ddf70c320d2 Mon Sep 17 00:00:00 2001 From: abel-mak Date: Sat, 22 Jan 2022 12:17:51 +0100 Subject: [PATCH 1/3] add numeric conversion stoi --- include/fixed_string.hpp | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/include/fixed_string.hpp b/include/fixed_string.hpp index 92876e5..7ca9ddd 100644 --- a/include/fixed_string.hpp +++ b/include/fixed_string.hpp @@ -615,6 +615,44 @@ std::basic_ostream& operator<<(std::basic_ostream +constexpr int stoi(const fixed_string& str, int base = 10) +{ + int res = 0; + typename fixed_string::const_iterator it = str.begin(); + typename fixed_string::const_iterator ite = str.end(); + int sign = 1; + int tmp = 0; + + while (it != ite && std::isspace(*it) != 0) + { + it++; + } + if (it != ite && (*it == '-' || *it == '+')) + { + sign = (*it == '-') ? -1 : 1; + it++; + } + if (base == 16 && it != ite && it + 1 != ite && *it == '0' && std::tolower(*(it + 1)) == 'x') + { + it += 2; + } + while (it != ite && std::isalnum(*it) != 0 && (base > 1 && base < 37)) + { + tmp = res; + if (std::isdigit(*it) != 0 && ((*it) - '0') < base) + res = (res * base) + ((*it) - '0') * sign; + else if (std::isalpha(*it) != 0 && (9 + (std::tolower(*it) - 'a' + 1)) < base) + res = (res * base) + ((9 + (std::tolower(*it) - 'a' + 1))) * sign; + else + break; + if ((sign > 0 && tmp > res) || (sign < 0 && tmp < res)) + throw std::out_of_range("fixedstr stoi"); + it++; + } + return (res); +} + } // namespace fixstr // hash support From 7c6715fb0feb53a5144ba830b5898bb16d98310d Mon Sep 17 00:00:00 2001 From: abel-mak Date: Sat, 22 Jan 2022 17:54:30 +0100 Subject: [PATCH 2/3] replace long type with auto in stoi --- include/fixed_string.hpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/include/fixed_string.hpp b/include/fixed_string.hpp index 7ca9ddd..7585ba8 100644 --- a/include/fixed_string.hpp +++ b/include/fixed_string.hpp @@ -618,11 +618,11 @@ std::basic_ostream& operator<<(std::basic_ostream constexpr int stoi(const fixed_string& str, int base = 10) { - int res = 0; - typename fixed_string::const_iterator it = str.begin(); - typename fixed_string::const_iterator ite = str.end(); - int sign = 1; - int tmp = 0; + int res = 0; + auto it = str.cbegin(); + auto ite = str.cend(); + int sign = 1; + int tmp = 0; while (it != ite && std::isspace(*it) != 0) { From 30f078efcd539ccd345708553a1b333b1a98ca6a Mon Sep 17 00:00:00 2001 From: Abdelali El Makkioui Date: Sat, 5 Feb 2022 10:17:48 +0100 Subject: [PATCH 3/3] refactor code --- include/fixed_string.hpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/include/fixed_string.hpp b/include/fixed_string.hpp index 7585ba8..b64cd4b 100644 --- a/include/fixed_string.hpp +++ b/include/fixed_string.hpp @@ -618,16 +618,14 @@ std::basic_ostream& operator<<(std::basic_ostream constexpr int stoi(const fixed_string& str, int base = 10) { - int res = 0; auto it = str.cbegin(); auto ite = str.cend(); - int sign = 1; - int tmp = 0; while (it != ite && std::isspace(*it) != 0) { it++; } + int sign = 1; if (it != ite && (*it == '-' || *it == '+')) { sign = (*it == '-') ? -1 : 1; @@ -637,9 +635,11 @@ constexpr int stoi(const fixed_string& str, int base = 10) { it += 2; } + int res = 0; while (it != ite && std::isalnum(*it) != 0 && (base > 1 && base < 37)) { - tmp = res; + const int tmp = res; + if (std::isdigit(*it) != 0 && ((*it) - '0') < base) res = (res * base) + ((*it) - '0') * sign; else if (std::isalpha(*it) != 0 && (9 + (std::tolower(*it) - 'a' + 1)) < base)