New function cli-utils.c:extract_arg_const
authorJoel Brobecker <brobecker@adacore.com>
Mon, 11 Nov 2013 05:19:32 +0000 (09:19 +0400)
committerJoel Brobecker <brobecker@adacore.com>
Thu, 14 Nov 2013 10:31:42 +0000 (14:31 +0400)
This function provides the exact same functionality as extract_arg,
except that it takes a "const char**" instead of a "char **".
It allows us also to re-implement extract_arg almost as a simple
wrapper around the new function.

gdb/ChangeLog:

        Pedro Alves  <palves@redhat.com>
        Joel Brobecker  <brobecker@adacore.com>

        * cli/cli-utils.h (extract_arg_const): Add declaration.
        * cli/cli-utils.c (extract_arg_const): New function.
        (extract_arg): Reimplement using extract_arg_const.

gdb/ChangeLog
gdb/cli/cli-utils.c
gdb/cli/cli-utils.h

index 64adfd275f7a5105a50d312199e118a41d74dea5..2dc6cca69876dfaf05541e86dd3f7f9e1b727e7f 100644 (file)
@@ -1,3 +1,10 @@
+2013-11-14  Pedro Alves  <palves@redhat.com>
+           Joel Brobecker  <brobecker@adacore.com>
+
+       * cli/cli-utils.h (extract_arg_const): Add declaration.
+       * cli/cli-utils.c (extract_arg_const): New function.
+       (extract_arg): Reimplement using extract_arg_const.
+
 2013-11-14  Joel Brobecker  <brobecker@adacore.com>
 
        * language.h: Add "symtab.h" #include.
index f74e6b1f3f4266940a123dd4d879d789a316d9f0..316cf4fcc79247ff6dc49f94f04e92c1862c4081 100644 (file)
@@ -261,30 +261,39 @@ remove_trailing_whitespace (const char *start, char *s)
 /* See documentation in cli-utils.h.  */
 
 char *
-extract_arg (char **arg)
+extract_arg_const (const char **arg)
 {
-  char *result, *copy;
+  const char *result;
 
   if (!*arg)
     return NULL;
 
   /* Find the start of the argument.  */
-  *arg = skip_spaces (*arg);
+  *arg = skip_spaces_const (*arg);
   if (!**arg)
     return NULL;
   result = *arg;
 
   /* Find the end of the argument.  */
-  *arg = skip_to_space (*arg + 1);
+  *arg = skip_to_space_const (*arg + 1);
 
   if (result == *arg)
     return NULL;
 
-  copy = xmalloc (*arg - result + 1);
-  memcpy (copy, result, *arg - result);
-  copy[*arg - result] = '\0';
+  return savestring (result, *arg - result);
+}
+
+/* See documentation in cli-utils.h.  */
+
+char *
+extract_arg (char **arg)
+{
+  const char *arg_const = *arg;
+  char *result;
 
-  return copy;
+  result = extract_arg_const (&arg_const);
+  *arg += arg_const - *arg;
+  return result;
 }
 
 /* See documentation in cli-utils.h.  */
index 152fb89671350d99994ec990c68b0bd873e925f9..ebae2d2b552e028c38538e1e76ae1d04f2e52e01 100644 (file)
@@ -118,6 +118,13 @@ extern char *remove_trailing_whitespace (const char *start, char *s);
 
 extern char *extract_arg (char **arg);
 
+/* A const-correct version of "extract_arg".
+
+   Since the returned value is xmalloc'd, it eventually needs to be
+   xfree'ed, which prevents us from making it const as well.  */
+
+extern char *extract_arg_const (const char **arg);
+
 /* A helper function that looks for an argument at the start of a
    string.  The argument must also either be at the end of the string,
    or be followed by whitespace.  Returns 1 if it finds the argument,
This page took 0.032411 seconds and 4 git commands to generate.