Cleanup client name validation code

This commit is contained in:
Maxime Coste 2018-03-25 16:53:27 +11:00
parent 49c52b025f
commit a6b1d142fa
3 changed files with 12 additions and 9 deletions

View File

@ -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<ClientManager*>(this)->get_client_ifp(name) == nullptr;
return const_cast<ClientManager*>(this)->get_client_ifp(name) != nullptr;
}
Client* ClientManager::get_client_ifp(StringView name)

View File

@ -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<std::unique_ptr<Client>, MemoryDomain::Client>;

View File

@ -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);
}
};