From b884b499dd58d47c6c9da5d7ee244dd8cb591288 Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Wed, 23 Nov 2016 23:31:17 +0000 Subject: [PATCH] Rework ensure_all_buffers_are_saved to remove an unneeded Vector --- src/commands.cc | 31 +++++++++++++++---------------- 1 file changed, 15 insertions(+), 16 deletions(-) diff --git a/src/commands.cc b/src/commands.cc index 0c2e42eb..c16d5dd9 100644 --- a/src/commands.cc +++ b/src/commands.cc @@ -365,24 +365,23 @@ const CommandDesc write_all_cmd = { static void ensure_all_buffers_are_saved() { - Vector names; - for (auto& buffer : BufferManager::instance()) + auto is_modified = [](const std::unique_ptr& buf) { + return (buf->flags() & Buffer::Flags::File) and buf->is_modified(); + }; + + auto it = find_if(BufferManager::instance(), is_modified); + const auto end = BufferManager::instance().end(); + if (it == end) + return; + + String message = "modified buffers remaining: ["; + while (it != end) { - if ((buffer->flags() & Buffer::Flags::File) and buffer->is_modified()) - names.push_back(buffer->name()); - } - if (not names.empty()) - { - String message = "modified buffers remaining: ["; - for (auto it = names.begin(); it != names.end(); ++it) - { - if (it != names.begin()) - message += ", "; - message += *it; - } - message += "]"; - throw runtime_error(message); + message += (*it)->name(); + it = std::find_if(it+1, end, is_modified); + message += (it != end) ? ", " : "]"; } + throw runtime_error(message); } const CommandDesc kill_cmd = {