unittests/ui-file-selftests.c \
unittests/unique_xmalloc_ptr_char.c \
unittests/unpack-selftests.c \
- unittests/utils-selftests.c \
unittests/vec-utils-selftests.c \
unittests/xml-utils-selftests.c
#include <algorithm>
#include "gdbsupport/pathstuff.h"
#include "cli/cli-style.h"
+#include "gdbsupport/selftest.h"
/* The section to look in for auto-loaded scripts (in file formats that
support sections).
value);
}
+/* Substitute all occurrences 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. */
+
+static void
+substitute_path_component (char **stringp, const char *from, const char *to)
+{
+ char *string = *stringp, *s;
+ const size_t from_len = strlen (from);
+ const size_t to_len = strlen (to);
+
+ for (s = string;;)
+ {
+ 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 *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;
+ }
+ else
+ s++;
+ }
+
+ *stringp = string;
+}
+
+#if GDB_SELF_TEST
+
+namespace selftests {
+namespace subst_path {
+
+static void
+test_substitute_path_component ()
+{
+ auto test = [] (std::string s, const char *from, const char *to,
+ const char *expected)
+ {
+ char *temp = xstrdup (s.c_str ());
+ substitute_path_component (&temp, from, to);
+ SELF_CHECK (strcmp (temp, expected) == 0);
+ xfree (temp);
+ };
+
+ test ("/abc/$def/g", "abc", "xyz", "/xyz/$def/g");
+ test ("abc/$def/g", "abc", "xyz", "xyz/$def/g");
+ test ("/abc/$def/g", "$def", "xyz", "/abc/xyz/g");
+ test ("/abc/$def/g", "g", "xyz", "/abc/$def/xyz");
+ test ("/abc/$def/g", "ab", "xyz", "/abc/$def/g");
+ test ("/abc/$def/g", "def", "xyz", "/abc/$def/g");
+ test ("/abc/$def/g", "abc", "abc", "/abc/$def/g");
+ test ("/abc/$def/g", "abc", "", "//$def/g");
+ test ("/abc/$def/g", "abc/$def", "xyz", "/xyz/g");
+ test ("/abc/$def/abc", "abc", "xyz", "/xyz/$def/xyz");
+}
+
+}
+}
+
+#endif /* GDB_SELF_TEST */
+
/* Directory list safe to hold auto-loaded files. It is not checked for
absolute paths but they are strongly recommended. It is initialized by
_initialize_auto_load. */
is displayed."),
NULL, show_debug_auto_load,
&setdebuglist, &showdebuglist);
+
+#if GDB_SELF_TEST
+ selftests::register_test ("substitute_path_component",
+ selftests::subst_path::test_substitute_path_component);
+#endif
}
+++ /dev/null
-/* Unit tests for the utils.c file.
-
- Copyright (C) 2018-2025 Free Software Foundation, Inc.
-
- This file is part of GDB.
-
- This program is free software; you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation; either version 3 of the License, or
- (at your option) any later version.
-
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
-
- You should have received a copy of the GNU General Public License
- along with this program. If not, see <http://www.gnu.org/licenses/>. */
-
-#include "utils.h"
-#include "gdbsupport/selftest.h"
-
-namespace selftests {
-namespace utils {
-
-static void
-test_substitute_path_component ()
-{
- auto test = [] (std::string s, const char *from, const char *to,
- const char *expected)
- {
- char *temp = xstrdup (s.c_str ());
- substitute_path_component (&temp, from, to);
- SELF_CHECK (strcmp (temp, expected) == 0);
- xfree (temp);
- };
-
- test ("/abc/$def/g", "abc", "xyz", "/xyz/$def/g");
- test ("abc/$def/g", "abc", "xyz", "xyz/$def/g");
- test ("/abc/$def/g", "$def", "xyz", "/abc/xyz/g");
- test ("/abc/$def/g", "g", "xyz", "/abc/$def/xyz");
- test ("/abc/$def/g", "ab", "xyz", "/abc/$def/g");
- test ("/abc/$def/g", "def", "xyz", "/abc/$def/g");
- test ("/abc/$def/g", "abc", "abc", "/abc/$def/g");
- test ("/abc/$def/g", "abc", "", "//$def/g");
- test ("/abc/$def/g", "abc/$def", "xyz", "/xyz/g");
- test ("/abc/$def/abc", "abc", "xyz", "/xyz/$def/xyz");
-}
-
-}
-}
-
-void _initialize_utils_selftests ();
-void
-_initialize_utils_selftests ()
-{
- selftests::register_test ("substitute_path_component",
- selftests::utils::test_substitute_path_component);
-}
return pid;
}
-/* Substitute all occurrences 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. */
-
-void
-substitute_path_component (char **stringp, const char *from, const char *to)
-{
- char *string = *stringp, *s;
- const size_t from_len = strlen (from);
- const size_t to_len = strlen (to);
-
- for (s = string;;)
- {
- 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 *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;
- }
- else
- s++;
- }
-
- *stringp = string;
-}
-
#ifdef HAVE_WAITPID
#ifdef SIGALRM
extern int gdb_filename_fnmatch (const char *pattern, const char *string,
int flags);
-extern void substitute_path_component (char **stringp, const char *from,
- const char *to);
-
std::string ldirname (const char *filename);
extern int count_path_elements (const char *path);