Use C++14 string_literals

This commit is contained in:
Florent Le Coz
2014-04-29 21:50:13 +02:00
parent 594bac1e84
commit f6e6b8905b
4 changed files with 22 additions and 15 deletions
+5 -3
View File
@@ -10,6 +10,8 @@
#include <iostream> #include <iostream>
#include <tuple> #include <tuple>
using namespace std::string_literals;
static const char* action_prefix = "\01ACTION "; static const char* action_prefix = "\01ACTION ";
Bridge::Bridge(const std::string& user_jid, XmppComponent* xmpp, Poller* poller): Bridge::Bridge(const std::string& user_jid, XmppComponent* xmpp, Poller* poller):
@@ -243,9 +245,9 @@ void Bridge::send_xmpp_message(const std::string& from, const std::string& autho
if (!author.empty()) if (!author.empty())
{ {
IrcUser user(author); IrcUser user(author);
body = std::string("\u000303") + user.nick + (user.host.empty()? body = "\u000303"s + user.nick + (user.host.empty()?
std::string("\u0003: "): "\u0003: ":
(" (\u000310" + user.host + std::string("\u000303)\u0003: "))) + msg; (" (\u000310" + user.host + "\u000303)\u0003: ")) + msg;
} }
else else
body = msg; body = msg;
+4 -2
View File
@@ -6,6 +6,8 @@
#include <string.h> #include <string.h>
using namespace std::string_literals;
static const char IRC_NUM_COLORS = 16; static const char IRC_NUM_COLORS = 16;
static const char* irc_colors_to_css[IRC_NUM_COLORS] = { static const char* irc_colors_to_css[IRC_NUM_COLORS] = {
@@ -130,10 +132,10 @@ Xmpp::body irc_format_to_xhtmlim(const std::string& s)
if (styles.italic) if (styles.italic)
styles_str += "font-style:italic;"; styles_str += "font-style:italic;";
if (styles.fg != -1) if (styles.fg != -1)
styles_str += std::string("color:") + styles_str += "color:"s +
irc_colors_to_css[styles.fg % IRC_NUM_COLORS] + ";"; irc_colors_to_css[styles.fg % IRC_NUM_COLORS] + ";";
if (styles.bg != -1) if (styles.bg != -1)
styles_str += std::string("background-color:") + styles_str += "background-color:"s +
irc_colors_to_css[styles.bg % IRC_NUM_COLORS] + ";"; irc_colors_to_css[styles.bg % IRC_NUM_COLORS] + ";";
if (!styles_str.empty()) if (!styles_str.empty())
{ {
+11 -8
View File
@@ -10,6 +10,9 @@
#include <iostream> #include <iostream>
#include <stdexcept> #include <stdexcept>
#include <string>
using namespace std::string_literals;
IrcClient::IrcClient(const std::string& hostname, const std::string& username, Bridge* bridge): IrcClient::IrcClient(const std::string& hostname, const std::string& username, Bridge* bridge):
hostname(hostname), hostname(hostname),
username(username), username(username),
@@ -36,7 +39,7 @@ void IrcClient::start()
{ {
if (this->connected || this->connecting) if (this->connected || this->connecting)
return ; return ;
this->bridge->send_xmpp_message(this->hostname, "", std::string("Connecting to ") + this->bridge->send_xmpp_message(this->hostname, "", "Connecting to "s +
this->hostname + ":" + "6667"); this->hostname + ":" + "6667");
this->connect(this->hostname, "6667"); this->connect(this->hostname, "6667");
} }
@@ -44,7 +47,7 @@ void IrcClient::start()
void IrcClient::on_connection_failed(const std::string& reason) void IrcClient::on_connection_failed(const std::string& reason)
{ {
this->bridge->send_xmpp_message(this->hostname, "", this->bridge->send_xmpp_message(this->hostname, "",
std::string("Connection failed: ") + reason); "Connection failed: "s + reason);
} }
void IrcClient::on_connected() void IrcClient::on_connected()
@@ -244,7 +247,7 @@ void IrcClient::on_notice(const IrcMessage& message)
this->bridge->send_xmpp_message(this->hostname, from, body); this->bridge->send_xmpp_message(this->hostname, from, body);
else else
{ {
IrcMessage modified_message(std::move(from), "PRIVMSG", {to, std::string("\u000303[notice]\u0003 ") + body}); IrcMessage modified_message(std::move(from), "PRIVMSG", {to, "\u000303[notice]\u0003 "s + body});
this->on_channel_message(modified_message); this->on_channel_message(modified_message);
} }
} }
@@ -344,7 +347,7 @@ void IrcClient::on_channel_message(const IrcMessage& message)
{ {
if (body.substr(1, 6) == "ACTION") if (body.substr(1, 6) == "ACTION")
this->bridge->send_message(iid, nick, this->bridge->send_message(iid, nick,
std::string("/me") + body.substr(7, body.size() - 8), muc); "/me"s + body.substr(7, body.size() - 8), muc);
} }
else else
this->bridge->send_message(iid, nick, body, muc); this->bridge->send_message(iid, nick, body, muc);
@@ -486,7 +489,7 @@ void IrcClient::on_error(const IrcMessage& message)
this->bridge->send_muc_leave(std::move(iid), std::move(own_nick), leave_message, true); this->bridge->send_muc_leave(std::move(iid), std::move(own_nick), leave_message, true);
} }
this->channels.clear(); this->channels.clear();
this->send_gateway_message(std::string("ERROR: ") + leave_message); this->send_gateway_message("ERROR: "s + leave_message);
} }
void IrcClient::on_quit(const IrcMessage& message) void IrcClient::on_quit(const IrcMessage& message)
@@ -583,7 +586,7 @@ void IrcClient::on_channel_mode(const IrcMessage& message)
mode_arguments += message.arguments[i]; mode_arguments += message.arguments[i];
} }
} }
this->bridge->send_message(iid, "", std::string("Mode ") + iid.chan + this->bridge->send_message(iid, "", "Mode "s + iid.chan +
" [" + mode_arguments + "] by " + user.nick, " [" + mode_arguments + "] by " + user.nick,
true); true);
const IrcChannel* channel = this->get_channel(iid.chan); const IrcChannel* channel = this->get_channel(iid.chan);
@@ -661,7 +664,7 @@ void IrcClient::on_channel_mode(const IrcMessage& message)
void IrcClient::on_user_mode(const IrcMessage& message) void IrcClient::on_user_mode(const IrcMessage& message)
{ {
this->bridge->send_xmpp_message(this->hostname, "", this->bridge->send_xmpp_message(this->hostname, "",
std::string("User mode for ") + message.arguments[0] + "User mode for "s + message.arguments[0] +
" is [" + message.arguments[1] + "]"); " is [" + message.arguments[1] + "]");
} }
@@ -685,5 +688,5 @@ void IrcClient::leave_dummy_channel(const std::string& exit_message)
this->dummy_channel.joined = false; this->dummy_channel.joined = false;
this->dummy_channel.joining = false; this->dummy_channel.joining = false;
this->dummy_channel.remove_all_users(); this->dummy_channel.remove_all_users();
this->bridge->send_muc_leave(Iid(std::string("%") + this->hostname), std::string(this->current_nick), exit_message, true); this->bridge->send_muc_leave(Iid("%"s + this->hostname), std::string(this->current_nick), exit_message, true);
} }
+2 -2
View File
@@ -42,7 +42,7 @@ void SocketHandler::init_socket()
const int existing_flags = ::fcntl(this->socket, F_GETFL, 0); const int existing_flags = ::fcntl(this->socket, F_GETFL, 0);
if ((existing_flags == -1) || if ((existing_flags == -1) ||
(::fcntl(this->socket, F_SETFL, existing_flags | O_NONBLOCK) == -1)) (::fcntl(this->socket, F_SETFL, existing_flags | O_NONBLOCK) == -1))
throw std::runtime_error(std::string("Could not initialize socket: ") + strerror(errno)); throw std::runtime_error("Could not initialize socket: "s + strerror(errno));
} }
void SocketHandler::connect(const std::string& address, const std::string& port) void SocketHandler::connect(const std::string& address, const std::string& port)
@@ -70,7 +70,7 @@ void SocketHandler::connect(const std::string& address, const std::string& port)
if (res != 0) if (res != 0)
{ {
log_warning(std::string("getaddrinfo failed: ") + gai_strerror(res)); log_warning("getaddrinfo failed: "s + gai_strerror(res));
this->close(); this->close();
this->on_connection_failed(gai_strerror(res)); this->on_connection_failed(gai_strerror(res));
return ; return ;