aboutsummaryrefslogtreecommitdiff
path: root/src/Message.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/Message.cpp')
-rw-r--r--src/Message.cpp40
1 files changed, 36 insertions, 4 deletions
diff --git a/src/Message.cpp b/src/Message.cpp
index 2740c11..19630b3 100644
--- a/src/Message.cpp
+++ b/src/Message.cpp
@@ -1,4 +1,5 @@
#include "../include/Message.hpp"
+#include "../include/StringView.hpp"
using namespace std;
@@ -18,18 +19,49 @@ namespace dchat
}
}
- void Message::addText(const string &text)
+ void Message::addText(const string &text, bool newLine)
{
- messageParts.push_back(new MessagePartText(text));
+ messageParts.push_back(new MessagePartText(text, newLine));
}
- void Message::addImage(const string &url)
+ void Message::addEmoji(const string &url, bool newLine)
{
- messageParts.push_back(new MessagePartEmoji(url));
+ messageParts.push_back(new MessagePartEmoji(url, newLine));
}
vector<MessagePart*>& Message::getParts()
{
return messageParts;
}
+
+ StringView getNextNewLine(const StringView &str)
+ {
+ for(usize i = 0; i < str.size; ++i)
+ {
+ if(str[i] == '\n')
+ return StringView(str.data, i);
+ }
+ return StringView();
+ }
+
+ Message* Message::buildFromString(User *user, const std::string &str)
+ {
+ Message *message = new Message(user);
+ usize strOffset = 0;
+ while(strOffset < str.size())
+ {
+ usize foundIndex = str.find('\n', strOffset);
+ usize lineEnd = foundIndex;
+ if(foundIndex == string::npos)
+ lineEnd = str.size();
+
+ message->addText(str.substr(strOffset, lineEnd - strOffset), foundIndex != string::npos);
+
+ if(foundIndex == string::npos)
+ break;
+ else
+ strOffset = lineEnd + 1;
+ }
+ return message;
+ }
}