Guest User

Untitled

a guest
Aug 17th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.26 KB | None | 0 0
  1. #include <string>
  2. #include <string_view>
  3. #include <iostream>
  4. #include <sstream>
  5. #include <thread>
  6. #include <atomic>
  7. #include <array>
  8. #include <memory>
  9. #include <deque>
  10. #include <algorithm>
  11.  
  12. class IRCMessage
  13. {
  14. public:
  15. /*
  16.  
  17. */
  18. IRCMessage(std::string && buf) :
  19. m_data(std::move(buf))
  20. {
  21. parse();
  22. }
  23.  
  24. /*
  25. Getters.
  26. */
  27. const std::string & getData();
  28. const std::string_view & getNick();
  29. const std::string_view & getUser();
  30. const std::string_view & getHost();
  31. const std::string_view & getCommand();
  32. const std::vector<std::string_view> & getParams();
  33. const std::string_view & getBody();
  34.  
  35. /*
  36. */
  37. void print(std::ostream & stream);
  38.  
  39. private:
  40. /*
  41. Called by constructor.
  42. */
  43. void parse();
  44.  
  45. std::string m_data;
  46. std::string_view m_nick_view;
  47. std::string_view m_user_view;
  48. std::string_view m_host_view;
  49. std::string_view m_command_view;
  50. std::vector<std::string_view> m_params_vec;
  51. std::string_view m_body_view;
  52. };
  53.  
  54. void IRCMessage::parse()
  55. {
  56. std::string_view data_view(m_data);
  57. std::size_t space = 0;
  58.  
  59. //prefix
  60. if (data_view[0] == ':') {
  61. space = data_view.find_first_of(' ');
  62. std::string_view prefix_view = data_view.substr(1, space);
  63.  
  64. std::size_t at = prefix_view.find_first_of('@');
  65. if (at != prefix_view.npos) {
  66. std::size_t ex = prefix_view.find_first_of('!');
  67. if (ex != prefix_view.npos) {
  68. m_nick_view = prefix_view.substr(0, ex);
  69. m_user_view = prefix_view.substr(ex + 1, at - ex - 1);
  70. m_host_view = prefix_view.substr(at + 1);
  71. }
  72. else {
  73. m_nick_view = prefix_view.substr(0, at);
  74. m_host_view = prefix_view.substr(at + 1);
  75. }
  76. }
  77. else {
  78. m_nick_view = prefix_view;
  79. }
  80.  
  81. data_view.remove_prefix(space + 1);
  82. if (data_view.empty()) return;
  83. }
  84.  
  85. //command
  86. {
  87. space = data_view.find_first_of(' ');
  88. m_command_view = data_view.substr(0, space);
  89.  
  90. data_view.remove_prefix(space + 1);
  91. if (data_view.empty()) return;
  92. }
  93.  
  94. //params
  95. {
  96. while (!data_view.empty()) {
  97. if (data_view[0] == ':') break;
  98. space = data_view.find_first_of(' ');
  99. if (space == data_view.npos) {
  100. m_params_vec.push_back(data_view);
  101. data_view.remove_prefix(data_view.size());
  102. break;
  103. }
  104. else {
  105. m_params_vec.push_back(data_view.substr(0, space));
  106. data_view.remove_prefix(space + 1);
  107. }
  108. }
  109. if (data_view.empty()) return;
  110. }
  111.  
  112. //body
  113. if (data_view[0] == ':') {
  114. m_body_view = data_view.substr(1);
  115. }
  116. }
  117.  
  118. void IRCMessage::print(std::ostream & stream)
  119. {
  120. stream
  121. << "nick: " << m_nick_view << "\n"
  122. << "user: " << m_user_view << "\n"
  123. << "host: " << m_host_view << "\n"
  124. << "command: " << m_command_view << "\n"
  125. << "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"
  126. << "body: " << m_body_view << "\n";
  127. }
  128.  
  129. const std::string & IRCMessage::getData()
  130. {
  131. return m_data;
  132. }
  133.  
  134. const std::string_view & IRCMessage::getNick()
  135. {
  136. return m_nick_view;
  137. }
  138.  
  139. const std::string_view & IRCMessage::getUser()
  140. {
  141. return m_user_view;
  142. }
  143.  
  144. const std::string_view & IRCMessage::getHost()
  145. {
  146. return m_host_view;
  147. }
  148.  
  149. const std::string_view & IRCMessage::getCommand()
  150. {
  151. return m_command_view;
  152. }
  153.  
  154. const std::vector<std::string_view>& IRCMessage::getParams()
  155. {
  156. return m_params_vec;
  157. }
  158.  
  159. const std::string_view & IRCMessage::getBody()
  160. {
  161. return m_body_view;
  162. }
Add Comment
Please, Sign In to add comment