• R/O
  • HTTP
  • SSH
  • HTTPS

Commit

Tags
Keine Tags

Frequently used words (click to add to your profile)

javac++androidlinuxc#windowsobjective-ccocoa誰得qtpythonphprubygameguibathyscaphec計画中(planning stage)翻訳omegatframeworktwitterdomtestvb.netdirectxゲームエンジンbtronarduinopreviewer

GNU Binutils with patches for OS216


Commit MetaInfo

Revision75d79af4135482bb3cb4b9c348439c64a8e29ca2 (tree)
Zeit2017-02-08 00:25:53
AutorPhilipp Rudo <prudo@linu...>
CommiterAndreas Arnez

Log Message

Add libiberty/concat styled concat_path function

This commit adds concat_path function to concatenate an arbitrary number of
path elements. The function automatically adds an directory separator between
two elements as needed.
gdb/ChangeLog:

* common/common-utils.h (endswith): New function.
* utils.c (_concat_path, approx_path_length): New function.
* utils.h (_concat_path): New export.
(concat_path): New define.

Ändern Zusammenfassung

Diff

--- a/gdb/common/common-utils.h
+++ b/gdb/common/common-utils.h
@@ -83,6 +83,17 @@ startswith (const char *string, const char *pattern)
8383 return strncmp (string, pattern, strlen (pattern)) == 0;
8484 }
8585
86+/* Return non-zero if the end of STRING matches PATTERN, zero
87+ otherwise. */
88+
89+static inline int
90+endswith (const char *string, const char *pattern)
91+{
92+ return (strlen (string) > strlen (pattern)
93+ && strncmp (string + strlen (string) - strlen (pattern), pattern,
94+ strlen (pattern)) == 0);
95+}
96+
8697 ULONGEST strtoulst (const char *num, const char **trailer, int base);
8798
8899 /* Skip leading whitespace characters in INP, returning an updated
--- a/gdb/utils.c
+++ b/gdb/utils.c
@@ -3179,6 +3179,52 @@ substitute_path_component (std::string &str, const std::string &from,
31793179 }
31803180 }
31813181
3182+/* Approximate length of final path. Helper for concat_path. */
3183+
3184+static inline unsigned long
3185+approx_path_length (std::initializer_list<std::string> args,
3186+ std::string dir_separator)
3187+{
3188+ size_t length = 0;
3189+
3190+ for (const std::string &arg: args)
3191+ length += arg.length () + dir_separator.length ();
3192+
3193+ return length;
3194+}
3195+
3196+/* See utils.h. */
3197+
3198+std::string
3199+_concat_path (std::initializer_list<std::string> args,
3200+ std::string dir_separator)
3201+{
3202+ std::string dst;
3203+ dst.reserve (approx_path_length (args, dir_separator));
3204+
3205+ for (const std::string &arg : args)
3206+ {
3207+ if (arg.empty ())
3208+ continue;
3209+
3210+ if (startswith (arg.c_str (), dir_separator.c_str ())
3211+ && endswith (dst.c_str (), dir_separator.c_str ()))
3212+ dst.erase (dst.length () - dir_separator.length (),
3213+ dir_separator.length ());
3214+
3215+ else if (!dst.empty ()
3216+ && !startswith (arg.c_str (), dir_separator.c_str ())
3217+ && !endswith (dst.c_str (), dir_separator.c_str ())
3218+ && dst != TARGET_SYSROOT_PREFIX)
3219+ dst += dir_separator;
3220+
3221+ dst += arg;
3222+ }
3223+
3224+ dst.shrink_to_fit ();
3225+ return dst;
3226+}
3227+
31823228 #ifdef HAVE_WAITPID
31833229
31843230 #ifdef SIGALRM
--- a/gdb/utils.h
+++ b/gdb/utils.h
@@ -24,6 +24,7 @@
2424 #include "exceptions.h"
2525 #include "common/scoped_restore.h"
2626 #include <chrono>
27+#include <string>
2728
2829 extern void initialize_utils (void);
2930
@@ -140,6 +141,22 @@ extern void substitute_path_component (std::string &str,
140141 const std::string &from,
141142 const std::string &to);
142143
144+/* Concatenate an arbitrary number of path elements. Adds and removes
145+ directory separators as needed.
146+
147+ concat_path (/first, second) => /first/second
148+ concat_path (first, second) => first/second
149+ concat_path (first/, second) => first/second
150+ concat_path (first, /second) => first/second
151+ concat_path (first/, /second) => first/second
152+ concat_path (target:, second) => target:second
153+ */
154+
155+extern std::string _concat_path (std::initializer_list<std::string> args,
156+ std::string dir_separator);
157+
158+#define concat_path(...) _concat_path ({__VA_ARGS__}, SLASH_STRING)
159+
143160 char *ldirname (const char *filename);
144161
145162 extern int count_path_elements (const char *path);