From a6b1d142fa1aef8824318c8647a74503d5fef015 Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Sun, 25 Mar 2018 16:53:27 +1100 Subject: [PATCH] Cleanup client name validation code --- src/client_manager.cc | 7 +++---- src/client_manager.hh | 2 +- src/commands.cc | 12 ++++++++---- 3 files changed, 12 insertions(+), 9 deletions(-) diff --git a/src/client_manager.cc b/src/client_manager.cc index 8fe19ece1..cfc44cbc9 100644 --- a/src/client_manager.cc +++ b/src/client_manager.cc @@ -33,7 +33,7 @@ String ClientManager::generate_name() const for (int i = 0; true; ++i) { String name = format("unnamed{}", i); - if (validate_client_name(name)) + if (not client_name_exists(name)) return name; } } @@ -172,10 +172,9 @@ void ClientManager::clear_client_trash() m_client_trash.clear(); } -bool ClientManager::validate_client_name(StringView name) const +bool ClientManager::client_name_exists(StringView name) const { - return all_of(name, is_identifier) and - const_cast(this)->get_client_ifp(name) == nullptr; + return const_cast(this)->get_client_ifp(name) != nullptr; } Client* ClientManager::get_client_ifp(StringView name) diff --git a/src/client_manager.hh b/src/client_manager.hh index f56fefd4c..465b74b51 100644 --- a/src/client_manager.hh +++ b/src/client_manager.hh @@ -40,7 +40,7 @@ public: Client* get_client_ifp(StringView name); Client& get_client(StringView name); - bool validate_client_name(StringView name) const; + bool client_name_exists(StringView name) const; void remove_client(Client& client, bool graceful, int status); using ClientList = Vector, MemoryDomain::Client>; diff --git a/src/commands.cc b/src/commands.cc index 889a53070..6a09d27fd 100644 --- a/src/commands.cc +++ b/src/commands.cc @@ -2034,10 +2034,14 @@ const CommandDesc rename_client_cmd = { CommandCompleter{}, [](const ParametersParser& parser, Context& context, const ShellContext&) { - if (ClientManager::instance().validate_client_name(parser[0])) - context.set_name(parser[0]); - else if (context.name() != parser[0]) - throw runtime_error(format("client name '{}' is not unique", parser[0])); + const String& name = parser[0]; + if (not all_of(name, is_identifier)) + throw runtime_error{format("Invalid client name '{}'", name)}; + else if (ClientManager::instance().client_name_exists(name) and + context.name() != name) + throw runtime_error{format("client name '{}' is not unique", name)}; + else + context.set_name(name); } };