From 541bd299d521666d7d02b4f36b75f31dd8a2d64f Mon Sep 17 00:00:00 2001 From: Andrei Karas Date: Sun, 12 Feb 2012 01:23:09 +0300 Subject: Fix some possible errors in string untils. --- src/utils/stringutils.cpp | 34 +++++++++++++++++++++++----------- src/utils/stringutils.h | 2 +- 2 files changed, 24 insertions(+), 12 deletions(-) (limited to 'src') diff --git a/src/utils/stringutils.cpp b/src/utils/stringutils.cpp index 798690602..9184ba79d 100644 --- a/src/utils/stringutils.cpp +++ b/src/utils/stringutils.cpp @@ -105,6 +105,7 @@ std::string strprintf(char const *format, ...) return res; } +/* std::string &removeBadChars(std::string &str) { std::string::size_type pos; @@ -118,6 +119,7 @@ std::string &removeBadChars(std::string &str) return str; } +*/ std::string removeColors(std::string msg) { @@ -316,27 +318,37 @@ void getSafeUtf8String(std::string text, char *buf) std::string getFileName(std::string path) { - size_t pos = path.rfind("/"); - if (pos == std::string::npos) - pos = path.rfind("\\"); - if (pos == std::string::npos) + size_t pos1 = path.rfind("/"); + size_t pos2 = path.rfind("\\"); + if (pos1 == std::string::npos) + pos1 = pos2; + else if (pos2 != std::string::npos && pos2 > pos1) + pos1 = pos2; + + if (pos1 == std::string::npos) return path; - return path.substr(pos + 1); + return path.substr(pos1 + 1); } std::string getFileDir(std::string path) { - size_t pos = path.rfind("/"); - if (pos == std::string::npos) - pos = path.rfind("\\"); - if (pos == std::string::npos) - return ""; - return path.substr(0, pos); + size_t pos1 = path.rfind("/"); + size_t pos2 = path.rfind("\\"); + if (pos1 == std::string::npos) + pos1 = pos2; + else if (pos2 != std::string::npos && pos2 > pos1) + pos1 = pos2; + + if (pos1 == std::string::npos) + return path; + return path.substr(0, pos1); } std::string& replaceAll(std::string& context, const std::string& from, const std::string& to) { + if (from.empty()) + return context; size_t lookHere = 0; size_t foundHere; while ((foundHere = context.find(from, lookHere)) != std::string::npos) diff --git a/src/utils/stringutils.h b/src/utils/stringutils.h index 398737179..47d495dd0 100644 --- a/src/utils/stringutils.h +++ b/src/utils/stringutils.h @@ -103,7 +103,7 @@ std::string strprintf(char const *, ...) * @param str the string to remove the bad chars from * @return a reference to the string without bad chars */ -std::string &removeBadChars(std::string &str); +//std::string &removeBadChars(std::string &str); /** * Removes colors from a string -- cgit v1.2.3-60-g2f50