summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThorbjørn Lindeijer <bjorn@lindeijer.nl>2025-04-10 09:34:38 +0200
committerThorbjørn Lindeijer <bjorn@lindeijer.nl>2025-04-29 09:48:35 +0000
commit913cda59de309f967a24b10575dc3443f382a8f9 (patch)
treebe00679f3e1dadfdc77e707b827491f2447cbebb
parent09218ff19862a8e9595b6ccfba807315897d018d (diff)
downloadmana-913cda59de309f967a24b10575dc3443f382a8f9.tar.gz
mana-913cda59de309f967a24b10575dc3443f382a8f9.tar.bz2
mana-913cda59de309f967a24b10575dc3443f382a8f9.tar.xz
mana-913cda59de309f967a24b10575dc3443f382a8f9.zip
Changed removeColors to work in-place
Can avoid some memory allocations. Also simplified its implementation a little. Also made ChatLogger::getDateString use the full year.
-rw-r--r--src/being.cpp6
-rw-r--r--src/chatlogger.cpp32
-rw-r--r--src/chatlogger.h12
-rw-r--r--src/net/tmwa/chathandler.cpp5
-rw-r--r--src/utils/stringutils.cpp11
-rw-r--r--src/utils/stringutils.h6
6 files changed, 28 insertions, 44 deletions
diff --git a/src/being.cpp b/src/being.cpp
index 85289fc7..46b67f4b 100644
--- a/src/being.cpp
+++ b/src/being.cpp
@@ -263,10 +263,8 @@ void Being::setPath(const Path &path)
void Being::setSpeech(const std::string &text, int time)
{
- // Remove colors
- mSpeech = removeColors(text);
-
- // Trim whitespace
+ mSpeech = text;
+ removeColors(mSpeech);
trim(mSpeech);
// Check for links
diff --git a/src/chatlogger.cpp b/src/chatlogger.cpp
index 723d2e7b..119d3fc1 100644
--- a/src/chatlogger.cpp
+++ b/src/chatlogger.cpp
@@ -38,15 +38,7 @@
#include "utils/stringutils.h"
-ChatLogger::ChatLogger()
-{
-}
-
-ChatLogger::~ChatLogger()
-{
- if (mLogFile.is_open())
- mLogFile.close();
-}
+ChatLogger::~ChatLogger() = default;
void ChatLogger::setLogFile(const std::string &logFilename)
{
@@ -86,7 +78,7 @@ void ChatLogger::log(std::string str)
mServerName.c_str(), dateStr.c_str()));
}
- str = removeColors(str);
+ removeColors(str);
writeTo(mLogFile, str);
}
@@ -100,23 +92,19 @@ void ChatLogger::log(std::string name, std::string str)
if (!logFile.is_open())
return;
- str = removeColors(str);
+ removeColors(str);
writeTo(logFile, str);
if (logFile.is_open())
logFile.close();
}
-std::string ChatLogger::getDateString() const
+std::string ChatLogger::getDateString()
{
- time_t rawtime;
- struct tm *timeinfo;
- char buffer[80];
-
- time (&rawtime);
- timeinfo = localtime(&rawtime);
-
- strftime(buffer, 79, "%y-%m-%d", timeinfo);
+ time_t t = time(nullptr);
+ struct tm *now = localtime(&t);
+ char buffer[11];
+ strftime(buffer, sizeof(buffer), "%Y-%m-%d", now);
return buffer;
}
@@ -146,7 +134,7 @@ std::string ChatLogger::secureName(std::string &name)
return name;
}
-void ChatLogger::writeTo(std::ofstream &file, const std::string &str) const
+void ChatLogger::writeTo(std::ofstream &file, const std::string &str)
{
file << str << std::endl;
}
@@ -176,6 +164,6 @@ void ChatLogger::makeDir(const std::string &dir)
#ifdef _WIN32
mkdir(dir.c_str());
#else
- mkdir(dir.c_str(), 0750);
+ mkdir(dir.c_str(), S_IRWXU | S_IRGRP | S_IXGRP);
#endif
}
diff --git a/src/chatlogger.h b/src/chatlogger.h
index 45fe7da2..59d9f28c 100644
--- a/src/chatlogger.h
+++ b/src/chatlogger.h
@@ -26,11 +26,7 @@
class ChatLogger
{
public:
- ChatLogger();
-
- /**
- * Destructor, closes log file.
- */
+ ChatLogger() = default;
~ChatLogger();
void setLogDir(const std::string &logDir);
@@ -42,7 +38,7 @@ class ChatLogger
void log(std::string name, std::string str);
- std::string getDateString() const;
+ static std::string getDateString();
static std::string secureName(std::string &str);
@@ -54,9 +50,9 @@ class ChatLogger
*/
void setLogFile(const std::string &logFilename);
- void writeTo(std::ofstream &file, const std::string &str) const;
+ static void writeTo(std::ofstream &file, const std::string &str);
- void makeDir(const std::string &dir);
+ static void makeDir(const std::string &dir);
std::ofstream mLogFile;
std::string mLogDir;
diff --git a/src/net/tmwa/chathandler.cpp b/src/net/tmwa/chathandler.cpp
index f9061f63..cce5a41d 100644
--- a/src/net/tmwa/chathandler.cpp
+++ b/src/net/tmwa/chathandler.cpp
@@ -193,7 +193,10 @@ void ChatHandler::handleMessage(MessageIn &msg)
trim(chatMsg);
std::string reducedMessage = chatMsg;
- chatMsg = removeColors(sender_name) + " : " + reducedMessage;
+ chatMsg = sender_name;
+ removeColors(chatMsg);
+ chatMsg += " : ";
+ chatMsg += reducedMessage;
Event event(Event::Being);
event.setString("message", chatMsg);
diff --git a/src/utils/stringutils.cpp b/src/utils/stringutils.cpp
index ba5f575b..05b6982c 100644
--- a/src/utils/stringutils.cpp
+++ b/src/utils/stringutils.cpp
@@ -111,14 +111,13 @@ std::string &replaceCharacters(std::string &str,
return str;
}
-std::string removeColors(std::string msg)
+std::string &removeColors(std::string &msg)
{
- for (unsigned int f = 0; f < msg.length() - 2 && msg.length() > 2; f++)
+ auto pos = msg.find("##");
+ while (pos != std::string::npos && msg.length() - pos >= 3)
{
- while (msg.length() > f + 2 && msg.at(f) == '#' && msg.at(f + 1) == '#')
- {
- msg = msg.erase(f, 3);
- }
+ msg.erase(pos, 3);
+ pos = msg.find("##", pos);
}
return msg;
}
diff --git a/src/utils/stringutils.h b/src/utils/stringutils.h
index 1fa8e1e6..53c36bc6 100644
--- a/src/utils/stringutils.h
+++ b/src/utils/stringutils.h
@@ -107,12 +107,12 @@ std::string &replaceCharacters(std::string &str,
char replacement = '_');
/**
- * Removes colors from a string
+ * Removes colors from a string.
*
* @param msg the string to remove the colors from
- * @return string without colors
+ * @return reference to the modified string
*/
-std::string removeColors(std::string msg);
+std::string &removeColors(std::string &msg);
/**
* Returns whether a string starts with a given prefix.