[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
argmatch: use void* for raw memory pointers
From: |
Akim Demaille |
Subject: |
argmatch: use void* for raw memory pointers |
Date: |
Thu, 18 Apr 2019 22:41:24 +0200 |
This is certainly debatable. It started when I meant to
update some piece of code that was using argmatch_to_argument
incorrectly. But the signature of argmatch_to_argument was
really unsettling:
argmatch_to_argument (const void *value,
const char *const *arglist,
const char *vallist, size_t valsize)
vallist is a not a string, it's a pointer to the list of "values",
that can be anything, of size "valsize". char* makes this harder
to understand in my opinion, void* makes more sense, even if that
means that in its implementation argmatch_to_argument needs to
cast to char* to perform pointer arithmetics.
WDYT?
commit 2168926d033188dac94559493399d9d877734f26
Author: Akim Demaille <address@hidden>
Date: Thu Apr 18 22:23:02 2019 +0200
argmatch: use void* for raw memory pointers
* lib/argmatch.h, lib/argmatch.c (argmatch, argmatch_valid)
(__xargmatch_internal, argmatch_to_argument): Use void* for pointers
to "values", keep char* for strings.
diff --git a/ChangeLog b/ChangeLog
index 398c33968..7d6cb0b0a 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,10 @@
+2019-04-18 Akim Demaille <address@hidden>
+
+ argmatch: use void* for raw memory pointers
+ * lib/argmatch.h, lib/argmatch.c (argmatch, argmatch_valid)
+ (__xargmatch_internal, argmatch_to_argument): Use void* for pointers
+ to "values", keep char* for strings.
+
2019-04-14 Paul Eggert <address@hidden>
* lib/str-two-way.h: Fix comment typo.
diff --git a/lib/argmatch.c b/lib/argmatch.c
index b9a3e400c..434708504 100644
--- a/lib/argmatch.c
+++ b/lib/argmatch.c
@@ -82,7 +82,7 @@ argmatch_exit_fn argmatch_die = __argmatch_die;
ptrdiff_t
argmatch (const char *arg, const char *const *arglist,
- const char *vallist, size_t valsize)
+ const void *vallist, size_t valsize)
{
size_t i; /* Temporary index in ARGLIST. */
size_t arglen; /* Length of ARG. */
@@ -96,6 +96,8 @@ argmatch (const char *arg, const char *const *arglist,
{
if (!strncmp (arglist[i], arg, arglen))
{
+ fprintf (stderr, "'%s'(%ld) vs. '%s'(%ld)\n",
+ arglist[i], strlen (arglist[i]), arg, arglen);
if (strlen (arglist[i]) == arglen)
/* Exact match found. */
return i;
@@ -106,8 +108,8 @@ argmatch (const char *arg, const char *const *arglist,
{
/* Second nonexact match found. */
if (vallist == NULL
- || memcmp (vallist + valsize * matchind,
- vallist + valsize * i, valsize))
+ || memcmp ((char const *) vallist + valsize * matchind,
+ (char const *) vallist + valsize * i, valsize))
{
/* There is a real ambiguity, or we could not
disambiguate. */
@@ -144,7 +146,7 @@ argmatch_invalid (const char *context, const char *value,
ptrdiff_t problem)
VALSIZE is the size of the elements of VALLIST */
void
argmatch_valid (const char *const *arglist,
- const char *vallist, size_t valsize)
+ const void *vallist, size_t valsize)
{
size_t i;
const char *last_val = NULL;
@@ -154,10 +156,10 @@ argmatch_valid (const char *const *arglist,
fputs (_("Valid arguments are:"), stderr);
for (i = 0; arglist[i]; i++)
if ((i == 0)
- || memcmp (last_val, vallist + valsize * i, valsize))
+ || memcmp (last_val, (char const *) vallist + valsize * i, valsize))
{
fprintf (stderr, "\n - %s", quote (arglist[i]));
- last_val = vallist + valsize * i;
+ last_val = (char const *) vallist + valsize * i;
}
else
{
@@ -175,7 +177,7 @@ argmatch_valid (const char *const *arglist,
ptrdiff_t
__xargmatch_internal (const char *context,
const char *arg, const char *const *arglist,
- const char *vallist, size_t valsize,
+ const void *vallist, size_t valsize,
argmatch_exit_fn exit_fn)
{
ptrdiff_t res = argmatch (arg, arglist, vallist, valsize);
@@ -194,14 +196,14 @@ __xargmatch_internal (const char *context,
/* Look for VALUE in VALLIST, an array of objects of size VALSIZE and
return the first corresponding argument in ARGLIST */
const char *
-argmatch_to_argument (const char *value,
+argmatch_to_argument (const void *value,
const char *const *arglist,
- const char *vallist, size_t valsize)
+ const void *vallist, size_t valsize)
{
size_t i;
for (i = 0; arglist[i]; i++)
- if (!memcmp (value, vallist + valsize * i, valsize))
+ if (!memcmp (value, (char const *) vallist + valsize * i, valsize))
return arglist[i];
return NULL;
}
diff --git a/lib/argmatch.h b/lib/argmatch.h
index 51d288587..50de57f29 100644
--- a/lib/argmatch.h
+++ b/lib/argmatch.h
@@ -44,10 +44,10 @@ extern "C" {
to the same values in VALLIST). */
ptrdiff_t argmatch (char const *arg, char const *const *arglist,
- char const *vallist, size_t valsize) _GL_ATTRIBUTE_PURE;
+ void const *vallist, size_t valsize) _GL_ATTRIBUTE_PURE;
# define ARGMATCH(Arg, Arglist, Vallist) \
- argmatch (Arg, Arglist, (char const *) (Vallist), sizeof *(Vallist))
+ argmatch (Arg, Arglist, (void const *) (Vallist), sizeof *(Vallist))
/* xargmatch calls this function when it fails. This function should not
return. By default, this is a function that calls ARGMATCH_DIE which
@@ -70,10 +70,10 @@ void argmatch_invalid (char const *context, char const
*value,
/* Report on stderr the list of possible arguments. */
void argmatch_valid (char const *const *arglist,
- char const *vallist, size_t valsize);
+ void const *vallist, size_t valsize);
# define ARGMATCH_VALID(Arglist, Vallist) \
- argmatch_valid (Arglist, (char const *) (Vallist), sizeof *(Vallist))
+ argmatch_valid (Arglist, (void const *) (Vallist), sizeof *(Vallist))
@@ -82,27 +82,27 @@ void argmatch_valid (char const *const *arglist,
ptrdiff_t __xargmatch_internal (char const *context,
char const *arg, char const *const *arglist,
- char const *vallist, size_t valsize,
+ void const *vallist, size_t valsize,
argmatch_exit_fn exit_fn);
/* Programmer friendly interface to __xargmatch_internal. */
# define XARGMATCH(Context, Arg, Arglist, Vallist) \
((Vallist) [__xargmatch_internal (Context, Arg, Arglist, \
- (char const *) (Vallist), \
+ (void const *) (Vallist), \
sizeof *(Vallist), \
argmatch_die)])
/* Convert a value into a corresponding argument. */
-char const *argmatch_to_argument (char const *value,
+char const *argmatch_to_argument (void const *value,
char const *const *arglist,
- char const *vallist, size_t valsize)
+ void const *vallist, size_t valsize)
_GL_ATTRIBUTE_PURE;
# define ARGMATCH_TO_ARGUMENT(Value, Arglist, Vallist) \
argmatch_to_argument (Value, Arglist, \
- (char const *) (Vallist), sizeof *(Vallist))
+ (void const *) (Vallist), sizeof *(Vallist))
#ifdef __cplusplus
}
- argmatch: use void* for raw memory pointers,
Akim Demaille <=