From 571ff6e1401876cb775b827f6af25e14b1f59c6f Mon Sep 17 00:00:00 2001 From: Andrei Karas Date: Mon, 8 Sep 2014 19:39:32 +0300 Subject: Improve string to int parsing. --- src/net/eathena/chathandler.cpp | 19 ++++--------------- src/net/tmwa/chathandler.cpp | 19 ++++--------------- src/utils/stringutils.cpp | 15 +++++++++++++++ src/utils/stringutils.h | 2 ++ 4 files changed, 25 insertions(+), 30 deletions(-) diff --git a/src/net/eathena/chathandler.cpp b/src/net/eathena/chathandler.cpp index eed77a52c..d55a93dbb 100644 --- a/src/net/eathena/chathandler.cpp +++ b/src/net/eathena/chathandler.cpp @@ -157,13 +157,13 @@ void ChatHandler::sendRaw(const std::string &args) const if (pos != std::string::npos) { str = line.substr(0, pos); - outMsg = new MessageOut(static_cast(atoi(str.c_str()))); + outMsg = new MessageOut(static_cast(parseNumber(str))); line = line.substr(pos + 1); pos = line.find(" "); } else { - outMsg = new MessageOut(static_cast(atoi(line.c_str()))); + outMsg = new MessageOut(static_cast(parseNumber(line))); delete outMsg; return; } @@ -186,19 +186,8 @@ void ChatHandler::processRaw(MessageOut &restrict outMsg, if (line.size() < 2) return; - char cmd = tolower(line[0]); - int i = 0; - std::string str = line.substr(1); - int idx = 0; - if (strStartWith(str, "0x")) - idx = 2; - else if (str[0] == 'h' || str[0] == 'x') - idx = 1; - if (idx > 0) - sscanf(str.substr(idx).c_str(), "%10x", &i); - else - i = atoi(str.c_str()); - switch (cmd) + const int i = parseNumber(line.substr(1)); + switch (tolower(line[0])) { case 'b': { diff --git a/src/net/tmwa/chathandler.cpp b/src/net/tmwa/chathandler.cpp index 3976aa723..3648197eb 100644 --- a/src/net/tmwa/chathandler.cpp +++ b/src/net/tmwa/chathandler.cpp @@ -178,13 +178,13 @@ void ChatHandler::sendRaw(const std::string &args) const if (pos != std::string::npos) { str = line.substr(0, pos); - outMsg = new MessageOut(static_cast(atoi(str.c_str()))); + outMsg = new MessageOut(static_cast(parseNumber(str))); line = line.substr(pos + 1); pos = line.find(" "); } else { - outMsg = new MessageOut(static_cast(atoi(line.c_str()))); + outMsg = new MessageOut(static_cast(parseNumber(line))); delete outMsg; return; } @@ -207,19 +207,8 @@ void ChatHandler::processRaw(MessageOut &restrict outMsg, if (line.size() < 2) return; - char cmd = tolower(line[0]); - int i = 0; - std::string str = line.substr(1); - int idx = 0; - if (strStartWith(str, "0x")) - idx = 2; - else if (str[0] == 'h' || str[0] == 'x') - idx = 1; - if (idx > 0) - sscanf(str.substr(idx).c_str(), "%10x", &i); - else - i = atoi(str.c_str()); - switch (cmd) + int i = parseNumber(line.substr(1)); + switch (tolower(line[0])) { case 'b': { diff --git a/src/utils/stringutils.cpp b/src/utils/stringutils.cpp index 11143206e..5674abfd9 100644 --- a/src/utils/stringutils.cpp +++ b/src/utils/stringutils.cpp @@ -746,3 +746,18 @@ bool parse2Int(const std::string &args, int &x, int &y) } return isValid; } + +int parseNumber(const std::string &str) +{ + int i = 0; + int idx = 0; + if (strStartWith(str, "0x")) + idx = 2; + else if (str[0] == 'h' || str[0] == 'x') + idx = 1; + if (idx > 0) + sscanf(str.substr(idx).c_str(), "%10x", &i); + else + i = atoi(str.c_str()); + return i; +} diff --git a/src/utils/stringutils.h b/src/utils/stringutils.h index 5cf685a89..cf729c345 100644 --- a/src/utils/stringutils.h +++ b/src/utils/stringutils.h @@ -240,4 +240,6 @@ void secureChatCommand(std::string &str); bool parse2Int(const std::string &args, int &x, int &y); +int parseNumber(const std::string &str); + #endif // UTILS_STRINGUTILS_H -- cgit v1.2.3-60-g2f50