util/OptionParser: use std::span instead of ConstBuffer

This commit is contained in:
Max Kellermann
2022-07-04 19:01:11 +02:00
parent 9b427b3171
commit e8667f99be
7 changed files with 24 additions and 16 deletions

View File

@@ -22,6 +22,14 @@
#include "util/RuntimeError.hxx"
#include "util/StringCompare.hxx"
static const char *
Shift(std::span<const char *const> &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);

View File

@@ -20,18 +20,18 @@
#ifndef MPD_UTIL_OPTIONPARSER_HXX
#define MPD_UTIL_OPTIONPARSER_HXX
#include "util/ConstBuffer.hxx"
#include "OptionDef.hxx"
class OptionDef;
#include <span>
/**
* Command line option parser.
*/
class OptionParser
{
ConstBuffer<OptionDef> options;
std::span<const OptionDef> options;
ConstBuffer<const char *> args;
std::span<const char *const> args;
const char **const remaining_head, **remaining_tail;
@@ -39,7 +39,7 @@ public:
/**
* Constructs #OptionParser.
*/
OptionParser(ConstBuffer<OptionDef> _options,
OptionParser(std::span<const OptionDef> _options,
int _argc, char **_argv) noexcept
:options(_options), args(_argv + 1, _argc - 1),
remaining_head(const_cast<const char **>(_argv + 1)),
@@ -66,7 +66,7 @@ public:
/**
* Returns the remaining non-option arguments.
*/
ConstBuffer<const char *> GetRemaining() const noexcept {
std::span<const char *const> GetRemaining() const noexcept {
return {remaining_head, remaining_tail};
}