Untangle some pointers in wgetopt

wgetopt had a "nameend" parameter which was a confusing pointer. Make it
into a slightly less confusing size_t.
This commit is contained in:
ridiculousfish
2022-12-04 14:48:20 -08:00
parent 962d1083d3
commit 35bad1f94c
2 changed files with 15 additions and 16 deletions

View File

@@ -246,14 +246,14 @@ int wgetopter_t::_handle_short_opt(int argc, string_array_t argv) {
}
void wgetopter_t::_update_long_opt(int argc, string_array_t argv, const struct woption *pfound,
const wchar_t *nameend, int *longind, int option_index,
int *retval) {
size_t nameend, int *longind, int option_index, int *retval) {
woptind++;
if (*nameend) {
assert(nextchar[nameend] == '\0' || nextchar[nameend] == '=');
if (nextchar[nameend] == '=') {
// Don't test has_arg with >, because some C compilers don't allow it to be used on
// enums.
if (pfound->has_arg)
woptarg = nameend + 1;
woptarg = &nextchar[nameend + 1];
else {
nextchar += std::wcslen(nextchar);
*retval = '?';
@@ -276,16 +276,15 @@ void wgetopter_t::_update_long_opt(int argc, string_array_t argv, const struct w
// Find a matching long opt.
const struct woption *wgetopter_t::_find_matching_long_opt(const struct woption *longopts,
const wchar_t *nameend, int *exact,
int *ambig, int *indfound) const {
size_t nameend, int *exact, int *ambig,
int *indfound) const {
const struct woption *pfound = nullptr;
int option_index = 0;
// Test all long options for either exact match or abbreviated matches.
for (const struct woption *p = longopts; p->name; p++, option_index++) {
if (!std::wcsncmp(p->name, nextchar, nameend - nextchar)) {
if (static_cast<unsigned int>(nameend - nextchar) ==
static_cast<unsigned int>(wcslen(p->name))) {
if (!std::wcsncmp(p->name, nextchar, nameend)) {
if (nameend == wcslen(p->name)) {
// Exact match found.
pfound = p;
*indfound = option_index;
@@ -311,9 +310,10 @@ bool wgetopter_t::_handle_long_opt(int argc, string_array_t argv, const struct w
int ambig = 0;
int indfound = 0;
const wchar_t *nameend;
for (nameend = nextchar; *nameend && *nameend != '='; nameend++)
; //!OCLINT(empty body)
size_t nameend = 0;
while (nextchar[nameend] && nextchar[nameend] != '=') {
nameend++;
}
const struct woption *pfound =
_find_matching_long_opt(longopts, nameend, &exact, &ambig, &indfound);

View File

@@ -124,11 +124,10 @@ class wgetopter_t {
int _handle_short_opt(int argc, string_array_t argv);
bool _handle_long_opt(int argc, string_array_t argv, const struct woption *longopts,
int *longind, int long_only, int *retval);
const struct woption *_find_matching_long_opt(const struct woption *longopts,
const wchar_t *nameend, int *exact, int *ambig,
int *indfound) const;
const struct woption *_find_matching_long_opt(const struct woption *longopts, size_t nameend,
int *exact, int *ambig, int *indfound) const;
void _update_long_opt(int argc, string_array_t argv, const struct woption *pfound,
const wchar_t *nameend, int *longind, int option_index, int *retval);
size_t nameend, int *longind, int option_index, int *retval);
bool initialized = false;
bool missing_arg_return_colon = false;
};