changeset 729:e08bfc940c54

Core: remove string_util::sprintf
author David Demelier <markand@malikania.fr>
date Wed, 18 Jul 2018 13:09:22 +0200
parents ae88795feb99
children 2496ebc42b07
files irccd-test/main.cpp irccdctl/main.cpp irccdctl/rule_add_cli.cpp libirccd-core/irccd/string_util.hpp libirccd/irccd/daemon/dynlib_plugin.cpp libirccd/irccd/daemon/server.cpp libirccd/irccd/daemon/service/plugin_service.cpp tests/src/plugins/plugin/main.cpp
diffstat 8 files changed, 72 insertions(+), 89 deletions(-) [+]
line wrap: on
line diff
--- a/irccd-test/main.cpp	Tue Jul 17 23:45:00 2018 +0200
+++ b/irccd-test/main.cpp	Wed Jul 18 13:09:22 2018 +0200
@@ -26,6 +26,7 @@
 
 #include <boost/algorithm/string/trim.hpp>
 #include <boost/filesystem.hpp>
+#include <boost/format.hpp>
 
 #if defined(IRCCD_HAVE_LIBEDIT)
 #   include <histedit.h>
@@ -46,6 +47,9 @@
 #   include <irccd/js/js_plugin.hpp>
 #endif
 
+using boost::format;
+using boost::str;
+
 namespace irccd {
 
 namespace su = string_util;
@@ -607,7 +611,7 @@
         try {
             daemon->set_config(it->second);
         } catch (const std::exception& ex) {
-            throw std::runtime_error(su::sprintf("%s: %s", it->second, ex.what()));
+            throw std::runtime_error(str(format("%1%: %2%") % it->second % ex.what()));
         }
     }
 }
--- a/irccdctl/main.cpp	Tue Jul 17 23:45:00 2018 +0200
+++ b/irccdctl/main.cpp	Wed Jul 18 13:09:22 2018 +0200
@@ -21,8 +21,9 @@
 #include <iostream>
 #include <unordered_map>
 
+#include <boost/filesystem.hpp>
+#include <boost/format.hpp>
 #include <boost/predef/os.h>
-#include <boost/filesystem.hpp>
 
 #include <irccd/config.hpp>
 #include <irccd/json_util.hpp>
@@ -71,6 +72,9 @@
 #include "alias.hpp"
 #include "cli.hpp"
 
+using boost::format;
+using boost::str;
+
 using boost::asio::ip::tcp;
 
 #if !BOOST_OS_WINDOWS
@@ -214,7 +218,7 @@
     else if (it->value() == "unix")
         connector = read_connect_local(sc);
     else
-        throw std::invalid_argument(string_util::sprintf("invalid type given: %s", it->value()));
+        throw std::invalid_argument(str(format("invalid type given: %1%") % it->value()));
 
     if (connector) {
         ctl = std::make_unique<controller>(std::move(connector));
@@ -267,8 +271,8 @@
          * argument is a command name.
          */
         if (option.size() == 1 && option[0].empty())
-            throw std::runtime_error(string_util::sprintf("alias %s: missing command name in '%s'",
-                name, option.key()));
+            throw std::runtime_error(str(format("alias %1%: missing command name in '%2%'")
+                % name % option.key()));
 
         std::string command = option[0];
         std::vector<alias_arg> args(option.begin() + 1, option.end());
@@ -379,7 +383,7 @@
     if (it->second == "unix")
         connector = parse_connect_local(options);
     else
-        throw std::invalid_argument(string_util::sprintf("invalid type given: %s", it->second));
+        throw std::invalid_argument(str(format("invalid type given: %1%") % it->second));
 
     if (connector)
         ctl = std::make_unique<controller>(std::move(connector));
@@ -439,7 +443,7 @@
             if (arg.is_placeholder()) {
                 if (args.size() < arg.index() + 1)
                     throw std::invalid_argument(
-                        string_util::sprintf("missing argument for placeholder %d", arg.index()));
+                        str(format("missing argument for placeholder %1%") % arg.index()));
 
                 cmd_args.push_back(args[arg.index()]);
 
@@ -540,8 +544,10 @@
             if (!major || !minor || !patch)
                 std::cout << "connected to irccd (unknown version)" << std::endl;
             else
-                std::cout << string_util::sprintf("connected to irccd %d.%d.%d\n",
-                    *major, *minor, *patch);
+                std::cout << "connected to irccd "
+                          << *major << "."
+                          << *minor << "."
+                          << *patch << std::endl;
         }
     });
 
--- a/irccdctl/rule_add_cli.cpp	Tue Jul 17 23:45:00 2018 +0200
+++ b/irccdctl/rule_add_cli.cpp	Wed Jul 18 13:09:22 2018 +0200
@@ -16,11 +16,16 @@
  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  */
 
+#include <boost/format.hpp>
+
 #include <irccd/options.hpp>
 #include <irccd/string_util.hpp>
 
 #include "rule_add_cli.hpp"
 
+using boost::format;
+using boost::str;
+
 using irccd::string_util::to_uint;
 
 namespace irccd {
@@ -86,7 +91,7 @@
 
     // And action.
     if (copy[0] != "accept" && copy[0] != "drop")
-        throw std::runtime_error(string_util::sprintf("invalid action '%s'", copy[0]));
+        throw std::runtime_error(str(format("invalid action '%1%'") % copy[0]));
 
     json["action"] = copy[0];
 
--- a/libirccd-core/irccd/string_util.hpp	Tue Jul 17 23:45:00 2018 +0200
+++ b/libirccd-core/irccd/string_util.hpp	Wed Jul 18 13:09:22 2018 +0200
@@ -36,8 +36,7 @@
 #include <string_view>
 #include <type_traits>
 #include <unordered_map>
-
-#include <boost/format.hpp>
+#include <vector>
 
 /**
  * \brief String utilities.
@@ -295,52 +294,6 @@
 
 // }}}
 
-// {{{ sprintf
-
-/**
- * \cond HIDDEN_SYMBOLS
- */
-
-namespace detail {
-
-inline void sprintf(boost::format&)
-{
-}
-
-template <typename Arg, typename... Args>
-inline void sprintf(boost::format& fmter, const Arg& arg, const Args&... args)
-{
-    fmter % arg;
-    sprintf(fmter, args...);
-}
-
-} // !detail
-
-/**
- * \endcond
- */
-
-/**
- * Convenient wrapper arount boost::format in sprintf style.
- *
- * This is identical as calling boost::format(format) % arg1 % arg2 % argN.
- *
- * \param format the format string
- * \param args the arguments
- * \return the string
- */
-template <typename Format, typename... Args>
-auto sprintf(const Format& format, const Args&... args) -> std::string
-{
-    boost::format fmter(format);
-
-    detail::sprintf(fmter, args...);
-
-    return fmter.str();
-}
-
-// }}}
-
 // {{{ to_int
 
 /**
--- a/libirccd/irccd/daemon/dynlib_plugin.cpp	Tue Jul 17 23:45:00 2018 +0200
+++ b/libirccd/irccd/daemon/dynlib_plugin.cpp	Wed Jul 18 13:09:22 2018 +0200
@@ -21,8 +21,7 @@
 #include <boost/dll.hpp>
 #include <boost/filesystem.hpp>
 #include <boost/predef/os.h>
-
-#include <irccd/string_util.hpp>
+#include <boost/format.hpp>
 
 #include "dynlib_plugin.hpp"
 
@@ -34,6 +33,9 @@
 #   define DYNLIB_EXTENSION ".so"
 #endif
 
+using boost::format;
+using boost::str;
+
 namespace irccd {
 
 namespace {
@@ -53,8 +55,8 @@
     });
 
     return {
-        string_util::sprintf("irccd_abi_%s", id),
-        string_util::sprintf("irccd_init_%s", id)
+        str(format("irccd_abi_%1%") % id),
+        str(format("irccd_init_%1%") % id)
     };
 }
 
--- a/libirccd/irccd/daemon/server.cpp	Tue Jul 17 23:45:00 2018 +0200
+++ b/libirccd/irccd/daemon/server.cpp	Wed Jul 18 13:09:22 2018 +0200
@@ -16,9 +16,10 @@
  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  */
 
-#include <boost/predef/os.h>
+#include <irccd/sysconfig.hpp>
 
-#include <irccd/sysconfig.hpp>
+#include <boost/predef/os.h>
+#include <boost/format.hpp>
 
 #include <algorithm>
 #include <cerrno>
@@ -38,6 +39,9 @@
 
 #include "server.hpp"
 
+using boost::format;
+using boost::str;
+
 namespace irccd {
 
 namespace {
@@ -284,7 +288,7 @@
 {
     assert(msg.command() == "PING");
 
-    send(string_util::sprintf("PONG %s", msg.arg(0)));
+    send(str(format("PONG %1%") % msg.arg(0)));
 }
 
 void server::dispatch_privmsg(const irc::message& msg)
@@ -431,10 +435,10 @@
     jchannels_.clear();
 
     if (!password_.empty())
-        send(string_util::sprintf("PASS %s", password_));
+        send(str(format("PASS %1%") % password_));
 
-    send(string_util::sprintf("NICK %s", nickname_));
-    send(string_util::sprintf("USER %s unknown unknown :%s", username_, realname_));
+    send(str(format("NICK %1%") % nickname_));
+    send(str(format("USER %1% unknown unknown :%2%") % username_ % realname_));
 }
 
 void server::wait()
@@ -488,7 +492,7 @@
 void server::set_nickname(std::string nickname)
 {
     if (state_ == state::connected)
-        send(string_util::sprintf("NICK %s", nickname));
+        send(str(format("NICK %1%") % nickname));
     else
         nickname_ = std::move(nickname);
 }
@@ -552,7 +556,7 @@
     assert(!target.empty());
     assert(!channel.empty());
 
-    send(string_util::sprintf("INVITE %s %s", target, channel));
+    send(str(format("INVITE %1% %2%") % target % channel));
 }
 
 void server::join(std::string channel, std::string password)
@@ -570,9 +574,9 @@
 
     if (state_ == state::connected) {
         if (password.empty())
-            send(string_util::sprintf("JOIN %s", channel));
+            send(str(format("JOIN %1%") % channel));
         else
-            send(string_util::sprintf("JOIN %s :%s", channel, password));
+            send(str(format("JOIN %1% :%2%") % channel % password));
     }
 }
 
@@ -582,9 +586,9 @@
     assert(!channel.empty());
 
     if (!reason.empty())
-        send(string_util::sprintf("KICK %s %s :%s", channel, target, reason));
+        send(str(format("KICK %1% %2% :%3%") % channel % target % reason));
     else
-        send(string_util::sprintf("KICK %s %s", channel, target));
+        send(str(format("KICK %1% %2%") % channel % target));
 }
 
 void server::me(std::string target, std::string message)
@@ -592,7 +596,7 @@
     assert(!target.empty());
     assert(!message.empty());
 
-    send(string_util::sprintf("PRIVMSG %s :\x01" "ACTION %s\x01", target, message));
+    send(str(format("PRIVMSG %1% :\x01" "ACTION %2%\x01") % target % message));
 }
 
 void server::message(std::string target, std::string message)
@@ -600,7 +604,7 @@
     assert(!target.empty());
     assert(!message.empty());
 
-    send(string_util::sprintf("PRIVMSG %s :%s", target, message));
+    send(str(format("PRIVMSG %1% :%2%") % target % message));
 }
 
 void server::mode(std::string channel,
@@ -630,7 +634,7 @@
 {
     assert(channel.c_str());
 
-    send(string_util::sprintf("NAMES %s", channel));
+    send(str(format("NAMES %1%") % channel));
 }
 
 void server::notice(std::string target, std::string message)
@@ -638,7 +642,7 @@
     assert(!target.empty());
     assert(!message.empty());
 
-    send(string_util::sprintf("NOTICE %s :%s", target, message));
+    send(str(format("NOTICE %1% :%2%") % target % message));
 }
 
 void server::part(std::string channel, std::string reason)
@@ -646,9 +650,9 @@
     assert(!channel.empty());
 
     if (!reason.empty())
-        send(string_util::sprintf("PART %s :%s", channel, reason));
+        send(str(format("PART %1% :%2%") % channel % reason));
     else
-        send(string_util::sprintf("PART %s", channel));
+        send(str(format("PART %1%") % channel));
 }
 
 void server::send(std::string raw)
@@ -671,16 +675,16 @@
     assert(!channel.empty());
 
     if (!topic.empty())
-        send(string_util::sprintf("TOPIC %s :%s", channel, topic));
+        send(str(format("TOPIC %1% :%2%") % channel % topic));
     else
-        send(string_util::sprintf("TOPIC %s", channel));
+        send(str(format("TOPIC %1%") % channel));
 }
 
 void server::whois(std::string target)
 {
     assert(!target.empty());
 
-    send(string_util::sprintf("WHOIS %s %s", target, target));
+    send(str(format("WHOIS %1% %2%") % target % target));
 }
 
 server_error::server_error(error code) noexcept
--- a/libirccd/irccd/daemon/service/plugin_service.cpp	Tue Jul 17 23:45:00 2018 +0200
+++ b/libirccd/irccd/daemon/service/plugin_service.cpp	Wed Jul 18 13:09:22 2018 +0200
@@ -16,6 +16,8 @@
  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  */
 
+#include <boost/format.hpp>
+
 #include <irccd/config.hpp>
 #include <irccd/string_util.hpp>
 #include <irccd/system.hpp>
@@ -25,6 +27,9 @@
 
 #include <irccd/daemon/service/plugin_service.hpp>
 
+using boost::format;
+using boost::str;
+
 namespace irccd {
 
 namespace {
@@ -99,18 +104,18 @@
 
 auto plugin_service::get_options(const std::string& id) -> plugin::map
 {
-    return to_map(irccd_.get_config(), string_util::sprintf("plugin.%s", id));
+    return to_map(irccd_.get_config(), str(format("plugin.%1%") % id));
 }
 
 auto plugin_service::get_formats(const std::string& id) -> plugin::map
 {
-    return to_map(irccd_.get_config(), string_util::sprintf("format.%s", id));
+    return to_map(irccd_.get_config(), str(format("format.%1%") % id));
 }
 
 auto plugin_service::get_paths(const std::string& id) -> plugin::map
 {
     auto defaults = to_map(irccd_.get_config(), "paths");
-    auto paths = to_map(irccd_.get_config(), string_util::sprintf("paths.%s", id));
+    auto paths = to_map(irccd_.get_config(), str(format("paths.%1%") % id));
 
     // Fill defaults paths.
     if (!defaults.count("cache"))
@@ -201,10 +206,10 @@
         throw plugin_error(plugin_error::not_found, id);
 
     // Erase first, in case of throwing.
-    const auto save = it->second;
+    const auto plg = it->second;
 
     plugins_.erase(it);
-    exec(it->second, &plugin::handle_unload, irccd_);
+    exec(plg, &plugin::handle_unload, irccd_);
 }
 
 void plugin_service::load(const config& cfg) noexcept
--- a/tests/src/plugins/plugin/main.cpp	Tue Jul 17 23:45:00 2018 +0200
+++ b/tests/src/plugins/plugin/main.cpp	Wed Jul 18 13:09:22 2018 +0200
@@ -18,6 +18,7 @@
 
 #define BOOST_TEST_MODULE "Plugin plugin"
 #include <boost/test/unit_test.hpp>
+#include <boost/format.hpp>
 
 #include <irccd/string_util.hpp>
 
@@ -27,6 +28,9 @@
 
 #include <irccd/test/plugin_test.hpp>
 
+using boost::format;
+using boost::str;
+
 namespace irccd {
 
 namespace {
@@ -129,7 +133,7 @@
 BOOST_AUTO_TEST_CASE(format_too_long)
 {
     for (int i = 0; i < 100; ++i)
-        irccd_.plugins().add(string_util::sprintf("plugin-n-%d", i), std::make_shared<fake_plugin>());
+        irccd_.plugins().add(str(format("plugin-n-%1%") % i), std::make_shared<fake_plugin>());
 
     plugin_->handle_command(irccd_, {server_, "jean!jean@localhost", "#staff", "list"});