From 43bc8314fba746a05f607d71fa128ebeadf264b2 Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Wed, 27 Feb 2013 20:51:44 +0100 Subject: [PATCH] ParameterParser: refactoring, simplify --- src/parameters_parser.cc | 42 +++++++++++++--------------------------- src/parameters_parser.hh | 21 ++++++++------------ 2 files changed, 21 insertions(+), 42 deletions(-) diff --git a/src/parameters_parser.cc b/src/parameters_parser.cc index 1bf1e4d2..233f6a3f 100644 --- a/src/parameters_parser.cc +++ b/src/parameters_parser.cc @@ -5,12 +5,15 @@ namespace Kakoune ParametersParser::ParametersParser(const ParameterList& params, std::unordered_map options) - : m_params(params), m_positional(params.size(), true), + : m_params(params), m_options(std::move(options)) { + bool only_pos = false; for (size_t i = 0; i < params.size(); ++i) { - if (params[i][0] == '-') + if (params[i] == "--") + only_pos = true; + else if (not only_pos and params[i][0] == '-') { auto it = m_options.find(params[i].substr(1_byte)); if (it == m_options.end()) @@ -18,17 +21,13 @@ ParametersParser::ParametersParser(const ParameterList& params, if (it->second) { - if (i + 1 == params.size() or params[i+1][0] == '-') + ++i; + if (i == params.size() or params[i][0] == '-') throw missing_option_value(params[i]); - - m_positional[i+1] = false; } - m_positional[i] = false; } - - // all options following -- are positional - if (params[i] == "--") - break; + else + m_positional_indices.push_back(i); } } @@ -68,38 +67,23 @@ const String& ParametersParser::option_value(const String& name) const size_t ParametersParser::positional_count() const { - size_t res = 0; - for (bool positional : m_positional) - { - if (positional) - ++res; - } - return res; + return m_positional_indices.size(); } const String& ParametersParser::operator[] (size_t index) const { assert(index < positional_count()); - iterator it = begin(); - while (index) - { - ++it; - --index; - } - return *it; + return m_params[m_positional_indices[index]]; } ParametersParser::iterator ParametersParser::begin() const { - int index = 0; - while (index < m_positional.size() and not m_positional[index]) - ++index; - return iterator(*this, index); + return iterator(*this, 0); } ParametersParser::iterator ParametersParser::end() const { - return iterator(*this, m_params.size()); + return iterator(*this, m_positional_indices.size()); } } diff --git a/src/parameters_parser.hh b/src/parameters_parser.hh index 2ba77c76..d91f436f 100644 --- a/src/parameters_parser.hh +++ b/src/parameters_parser.hh @@ -61,31 +61,26 @@ struct ParametersParser const String& operator*() const { - assert(m_parser.m_positional[m_index]); - return m_parser.m_params[m_index]; + return m_parser.m_params[m_parser.m_positional_indices[m_index]]; } const String* operator->() const { - assert(m_parser.m_positional[m_index]); - return &m_parser.m_params[m_index]; + return &m_parser.m_params[m_parser.m_positional_indices[m_index]]; } - iterator& operator++() - { - while (m_index < m_parser.m_positional.size() and - not m_parser.m_positional[++m_index]) {} - return *this; - } + iterator& operator++() { ++m_index; return *this; } bool operator==(const iterator& other) const { - return &m_parser == &other.m_parser and m_index == other.m_index; + assert(&m_parser == &other.m_parser); + return m_index == other.m_index; } bool operator!=(const iterator& other) const { - return &m_parser != &other.m_parser or m_index != other.m_index; + assert(&m_parser == &other.m_parser); + return m_index != other.m_index; } bool operator<(const iterator& other) const @@ -108,7 +103,7 @@ struct ParametersParser private: ParameterList m_params; - std::vector m_positional; + std::vector m_positional_indices; std::unordered_map m_options; };