Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <string>
- #include <string_view>
- #include <iostream>
- #include <sstream>
- #include <thread>
- #include <atomic>
- #include <array>
- #include <memory>
- #include <deque>
- #include <algorithm>
- class IRCMessage
- {
- public:
- /*
- */
- IRCMessage(std::string && buf) :
- m_data(std::move(buf))
- {
- parse();
- }
- /*
- Getters.
- */
- const std::string & getData();
- const std::string_view & getNick();
- const std::string_view & getUser();
- const std::string_view & getHost();
- const std::string_view & getCommand();
- const std::vector<std::string_view> & getParams();
- const std::string_view & getBody();
- /*
- */
- void print(std::ostream & stream);
- private:
- /*
- Called by constructor.
- */
- void parse();
- std::string m_data;
- std::string_view m_nick_view;
- std::string_view m_user_view;
- std::string_view m_host_view;
- std::string_view m_command_view;
- std::vector<std::string_view> m_params_vec;
- std::string_view m_body_view;
- };
- void IRCMessage::parse()
- {
- std::string_view data_view(m_data);
- std::size_t space = 0;
- //prefix
- if (data_view[0] == ':') {
- space = data_view.find_first_of(' ');
- std::string_view prefix_view = data_view.substr(1, space);
- std::size_t at = prefix_view.find_first_of('@');
- if (at != prefix_view.npos) {
- std::size_t ex = prefix_view.find_first_of('!');
- if (ex != prefix_view.npos) {
- m_nick_view = prefix_view.substr(0, ex);
- m_user_view = prefix_view.substr(ex + 1, at - ex - 1);
- m_host_view = prefix_view.substr(at + 1);
- }
- else {
- m_nick_view = prefix_view.substr(0, at);
- m_host_view = prefix_view.substr(at + 1);
- }
- }
- else {
- m_nick_view = prefix_view;
- }
- data_view.remove_prefix(space + 1);
- if (data_view.empty()) return;
- }
- //command
- {
- space = data_view.find_first_of(' ');
- m_command_view = data_view.substr(0, space);
- data_view.remove_prefix(space + 1);
- if (data_view.empty()) return;
- }
- //params
- {
- while (!data_view.empty()) {
- if (data_view[0] == ':') break;
- space = data_view.find_first_of(' ');
- if (space == data_view.npos) {
- m_params_vec.push_back(data_view);
- data_view.remove_prefix(data_view.size());
- break;
- }
- else {
- m_params_vec.push_back(data_view.substr(0, space));
- data_view.remove_prefix(space + 1);
- }
- }
- if (data_view.empty()) return;
- }
- //body
- if (data_view[0] == ':') {
- m_body_view = data_view.substr(1);
- }
- }
- void IRCMessage::print(std::ostream & stream)
- {
- stream
- << "nick: " << m_nick_view << "\n"
- << "user: " << m_user_view << "\n"
- << "host: " << m_host_view << "\n"
- << "command: " << m_command_view << "\n"
- << "params: " << [](auto & vec)->std::string {std::string str; std::for_each(vec.begin(), vec.end(), [&](auto & s) {str.append(std::string(s) + ", "); }); return str; }(m_params_vec) << "\n"
- << "body: " << m_body_view << "\n";
- }
- const std::string & IRCMessage::getData()
- {
- return m_data;
- }
- const std::string_view & IRCMessage::getNick()
- {
- return m_nick_view;
- }
- const std::string_view & IRCMessage::getUser()
- {
- return m_user_view;
- }
- const std::string_view & IRCMessage::getHost()
- {
- return m_host_view;
- }
- const std::string_view & IRCMessage::getCommand()
- {
- return m_command_view;
- }
- const std::vector<std::string_view>& IRCMessage::getParams()
- {
- return m_params_vec;
- }
- const std::string_view & IRCMessage::getBody()
- {
- return m_body_view;
- }
Add Comment
Please, Sign In to add comment