split messages by newline
This commit is contained in:
parent
5eae90751d
commit
15d057e92b
@ -9,6 +9,8 @@
|
||||
#include <libirc_rfcnumeric.h>
|
||||
#include <libircclient.h>
|
||||
|
||||
#include "./string_view_split.hpp"
|
||||
|
||||
#include <cstdint>
|
||||
#include <string_view>
|
||||
#include <vector>
|
||||
@ -120,7 +122,13 @@ bool IRCClientMessageManager::sendText(const Contact3 c, std::string_view messag
|
||||
}
|
||||
|
||||
{ // actually send
|
||||
std::string tmp_message{message}; // string_view might not be nul terminated
|
||||
// split message by line
|
||||
for (const auto& inner_str : MM::std_utils::split(message, "\n")) {
|
||||
if (inner_str.empty()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
std::string tmp_message{inner_str}; // string_view might not be nul terminated
|
||||
if (action) {
|
||||
if (irc_cmd_me(_ircc.getSession(), to_str.c_str(), tmp_message.c_str()) != 0) {
|
||||
std::cerr << "IRCCMM error: failed to send action\n";
|
||||
@ -137,6 +145,7 @@ bool IRCClientMessageManager::sendText(const Contact3 c, std::string_view messag
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const Contact3 c_self = _cr.get<Contact::Components::Self>(c).self;
|
||||
|
||||
|
49
src/solanaceae/ircclient_messages/string_view_split.hpp
Normal file
49
src/solanaceae/ircclient_messages/string_view_split.hpp
Normal file
@ -0,0 +1,49 @@
|
||||
#pragma once
|
||||
|
||||
#include <string_view>
|
||||
#include <vector>
|
||||
#include <cctype>
|
||||
|
||||
namespace MM::std_utils {
|
||||
|
||||
inline std::string_view trim_prefix(std::string_view sv) {
|
||||
while (!sv.empty() && std::isspace(sv.front())) {
|
||||
sv.remove_prefix(1);
|
||||
}
|
||||
|
||||
return sv;
|
||||
}
|
||||
|
||||
inline std::string_view trim_suffix(std::string_view sv) {
|
||||
while (!sv.empty() && std::isspace(sv.back())) {
|
||||
sv.remove_suffix(1);
|
||||
}
|
||||
|
||||
return sv;
|
||||
}
|
||||
|
||||
inline std::string_view trim(std::string_view sv) {
|
||||
return trim_suffix(trim_prefix(sv));
|
||||
}
|
||||
|
||||
// src : https://marcoarena.wordpress.com/2017/01/03/string_view-odi-et-amo/
|
||||
inline std::vector<std::string_view> split(std::string_view str, const char* delims) {
|
||||
std::vector<std::string_view> ret;
|
||||
|
||||
std::string_view::size_type start = 0;
|
||||
auto pos = str.find_first_of(delims, start);
|
||||
while (pos != std::string_view::npos) {
|
||||
if (pos != start) {
|
||||
ret.push_back(str.substr(start, pos - start));
|
||||
}
|
||||
start = pos + 1;
|
||||
pos = str.find_first_of(delims, start);
|
||||
}
|
||||
if (start < str.length())
|
||||
ret.push_back(str.substr(start, str.length() - start));
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
} // MM::std_utils
|
||||
|
Loading…
Reference in New Issue
Block a user