From e8667f99be49d4bdce8f0f776bed7ce887a3b3fe Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Mon, 4 Jul 2022 19:01:11 +0200 Subject: [PATCH] util/OptionParser: use std::span instead of ConstBuffer --- src/util/OptionParser.cxx | 16 ++++++++++++---- src/util/OptionParser.hxx | 12 ++++++------ test/RunChromaprint.cxx | 2 +- test/run_convert.cxx | 2 +- test/run_decoder.cxx | 2 +- test/run_input.cxx | 2 +- test/run_output.cxx | 4 ++-- 7 files changed, 24 insertions(+), 16 deletions(-) diff --git a/src/util/OptionParser.cxx b/src/util/OptionParser.cxx index 30dcc1585..b211cfb3c 100644 --- a/src/util/OptionParser.cxx +++ b/src/util/OptionParser.cxx @@ -22,6 +22,14 @@ #include "util/RuntimeError.hxx" #include "util/StringCompare.hxx" +static const char * +Shift(std::span &s) noexcept +{ + const char *value = s.front(); + s = s.subspan(1); + return value; +} + inline const char * OptionParser::CheckShiftValue(const char *s, const OptionDef &option) { @@ -31,7 +39,7 @@ OptionParser::CheckShiftValue(const char *s, const OptionDef &option) if (args.empty()) throw FormatRuntimeError("Value expected after %s", s); - return args.shift(); + return Shift(args); } inline OptionParser::Result @@ -58,14 +66,14 @@ OptionParser::IdentifyOption(const char *s) else continue; - return {int(&i - options.data), value}; + return {int(&i - options.data()), value}; } } else if (s[1] != 0 && s[2] == 0) { const char ch = s[1]; for (const auto &i : options) { if (i.HasShortOption() && ch == i.GetShortOption()) { const char *value = CheckShiftValue(s, i); - return {int(&i - options.data), value}; + return {int(&i - options.data()), value}; } } } @@ -77,7 +85,7 @@ OptionParser::Result OptionParser::Next() { while (!args.empty()) { - const char *arg = args.shift(); + const char *arg = Shift(args); if (arg[0] == '-') return IdentifyOption(arg); diff --git a/src/util/OptionParser.hxx b/src/util/OptionParser.hxx index 8371fa76f..e687bdf3c 100644 --- a/src/util/OptionParser.hxx +++ b/src/util/OptionParser.hxx @@ -20,18 +20,18 @@ #ifndef MPD_UTIL_OPTIONPARSER_HXX #define MPD_UTIL_OPTIONPARSER_HXX -#include "util/ConstBuffer.hxx" +#include "OptionDef.hxx" -class OptionDef; +#include /** * Command line option parser. */ class OptionParser { - ConstBuffer options; + std::span options; - ConstBuffer args; + std::span args; const char **const remaining_head, **remaining_tail; @@ -39,7 +39,7 @@ public: /** * Constructs #OptionParser. */ - OptionParser(ConstBuffer _options, + OptionParser(std::span _options, int _argc, char **_argv) noexcept :options(_options), args(_argv + 1, _argc - 1), remaining_head(const_cast(_argv + 1)), @@ -66,7 +66,7 @@ public: /** * Returns the remaining non-option arguments. */ - ConstBuffer GetRemaining() const noexcept { + std::span GetRemaining() const noexcept { return {remaining_head, remaining_tail}; } diff --git a/test/RunChromaprint.cxx b/test/RunChromaprint.cxx index c54a62185..d1869847a 100644 --- a/test/RunChromaprint.cxx +++ b/test/RunChromaprint.cxx @@ -79,7 +79,7 @@ ParseCommandLine(int argc, char **argv) } auto args = option_parser.GetRemaining(); - if (args.size != 2) + if (args.size() != 2) throw std::runtime_error("Usage: RunChromaprint [--verbose] [--config=FILE] DECODER URI"); c.decoder = args[0]; diff --git a/test/run_convert.cxx b/test/run_convert.cxx index 263522cf3..b0398bbcf 100644 --- a/test/run_convert.cxx +++ b/test/run_convert.cxx @@ -82,7 +82,7 @@ ParseCommandLine(int argc, char **argv) } auto args = option_parser.GetRemaining(); - if (args.size != 2) + if (args.size() != 2) throw std::runtime_error("Usage: run_convert IN_FORMAT OUT_FORMAT OUT"); c.in_audio_format = ParseAudioFormat(args[0], false); diff --git a/test/run_decoder.cxx b/test/run_decoder.cxx index cdfa852b4..64d1aa6c6 100644 --- a/test/run_decoder.cxx +++ b/test/run_decoder.cxx @@ -87,7 +87,7 @@ ParseCommandLine(int argc, char **argv) } auto args = option_parser.GetRemaining(); - if (args.size != 2) + if (args.size() != 2) throw std::runtime_error("Usage: run_decoder [--verbose] [--config=FILE] DECODER URI"); c.decoder = args[0]; diff --git a/test/run_input.cxx b/test/run_input.cxx index 5fc1e044e..2077d88ec 100644 --- a/test/run_input.cxx +++ b/test/run_input.cxx @@ -124,7 +124,7 @@ ParseCommandLine(int argc, char **argv) } auto args = option_parser.GetRemaining(); - if (args.size != 1) + if (args.size() != 1) throw std::runtime_error("Usage: run_input [--verbose] [--config=FILE] [--scan] [--chunk-size=BYTES] URI"); c.uri = args.front(); diff --git a/test/run_output.cxx b/test/run_output.cxx index e67ec25d3..53010bc2f 100644 --- a/test/run_output.cxx +++ b/test/run_output.cxx @@ -76,13 +76,13 @@ ParseCommandLine(int argc, char **argv) } auto args = option_parser.GetRemaining(); - if (args.size < 2 || args.size > 3) + if (args.size() < 2 || args.size() > 3) throw std::runtime_error("Usage: run_output CONFIG NAME [FORMAT] 2) + if (args.size() > 2) c.audio_format = ParseAudioFormat(args[2], false); return c;