aboutsummaryrefslogtreecommitdiff
path: root/gdb/utils.c
diff options
context:
space:
mode:
authorPhilipp Rudo <prudo@linux.vnet.ibm.com>2017-02-07 14:17:02 +0100
committerAndreas Arnez <arnez@linux.vnet.ibm.com>2017-02-07 16:25:53 +0100
commitc48c4ca2318c992e196c48eb96060a7405c4a814 (patch)
treea2b51e1e7a61321013b65f7879a4c8432075a2b6 /gdb/utils.c
parent3d044c0c78c11968b4fe3c5c019523e3177b1710 (diff)
downloadgdb-c48c4ca2318c992e196c48eb96060a7405c4a814.zip
gdb-c48c4ca2318c992e196c48eb96060a7405c4a814.tar.gz
gdb-c48c4ca2318c992e196c48eb96060a7405c4a814.tar.bz2
Convert substitute_path_component to C++
Simplify the code of utils.c:substiute_path_component by converting it to C++. gdb/ChangeLog: * utils.c (substitute_path_component): Convert to C++. * utils.h (substitute_path_componetn): Adjust declatation. * auto-load.c (auto_load_expand_dir_vars): Adjust.
Diffstat (limited to 'gdb/utils.c')
-rw-r--r--gdb/utils.c50
1 files changed, 14 insertions, 36 deletions
diff --git a/gdb/utils.c b/gdb/utils.c
index 3dc2f03..2b947aa 100644
--- a/gdb/utils.c
+++ b/gdb/utils.c
@@ -66,6 +66,8 @@
#include "interps.h"
#include "gdb_regex.h"
+#include <string>
+
#if !HAVE_DECL_MALLOC
extern PTR malloc (); /* ARI: PTR */
#endif
@@ -3156,49 +3158,25 @@ make_cleanup_free_char_ptr_vec (VEC (char_ptr) *char_ptr_vec)
return make_cleanup (do_free_char_ptr_vec, char_ptr_vec);
}
-/* Substitute all occurences of string FROM by string TO in *STRINGP. *STRINGP
- must come from xrealloc-compatible allocator and it may be updated. FROM
- needs to be delimited by IS_DIR_SEPARATOR or DIRNAME_SEPARATOR (or be
- located at the start or end of *STRINGP. */
+/* See utils.h. */
void
-substitute_path_component (char **stringp, const char *from, const char *to)
+substitute_path_component (std::string &str, const std::string &from,
+ const std::string &to)
{
- char *string = *stringp, *s;
- const size_t from_len = strlen (from);
- const size_t to_len = strlen (to);
-
- for (s = string;;)
+ for (size_t pos = str.find (from); pos != std::string::npos;
+ pos = str.find (from, pos + 1))
{
- s = strstr (s, from);
- if (s == NULL)
- break;
-
- if ((s == string || IS_DIR_SEPARATOR (s[-1])
- || s[-1] == DIRNAME_SEPARATOR)
- && (s[from_len] == '\0' || IS_DIR_SEPARATOR (s[from_len])
- || s[from_len] == DIRNAME_SEPARATOR))
+ char start, end;
+ start = str[pos - 1];
+ end = str[pos + from.length ()];
+ if ((pos == 0 || IS_DIR_SEPARATOR (start) || start == DIRNAME_SEPARATOR)
+ && (end == '\0' || IS_DIR_SEPARATOR (end)
+ || end == DIRNAME_SEPARATOR))
{
- char *string_new;
-
- string_new
- = (char *) xrealloc (string, (strlen (string) + to_len + 1));
-
- /* Relocate the current S pointer. */
- s = s - string + string_new;
- string = string_new;
-
- /* Replace from by to. */
- memmove (&s[to_len], &s[from_len], strlen (&s[from_len]) + 1);
- memcpy (s, to, to_len);
-
- s += to_len;
+ str.replace (pos, from.length (), to);
}
- else
- s++;
}
-
- *stringp = string;
}
#ifdef HAVE_WAITPID