public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
From: Philipp Rudo <prudo@linux.vnet.ibm.com>
To: gdb-patches@sourceware.org
Cc: peter.griffin@linaro.org, yao.qi@arm.com, arnez@linux.vnet.ibm.com
Subject: [RFC 2/7] Add libiberty/concat styled concat_path function
Date: Thu, 12 Jan 2017 11:32:00 -0000	[thread overview]
Message-ID: <20170112113217.48852-3-prudo@linux.vnet.ibm.com> (raw)
In-Reply-To: <20170112113217.48852-1-prudo@linux.vnet.ibm.com>

    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 (startswith): Convert to C++.
	(endswith): New function.
	* utils.c (_concat_path, approx_path_length): New function.
	* utils.h (_concat_path): New export.
	(concat_path): New define.
---
 gdb/common/common-utils.h | 16 +++++++++++++---
 gdb/utils.c               | 46 ++++++++++++++++++++++++++++++++++++++++++++++
 gdb/utils.h               | 17 +++++++++++++++++
 3 files changed, 76 insertions(+), 3 deletions(-)

diff --git a/gdb/common/common-utils.h b/gdb/common/common-utils.h
index 618d266..be3c106 100644
--- a/gdb/common/common-utils.h
+++ b/gdb/common/common-utils.h
@@ -74,13 +74,23 @@ char *savestring (const char *ptr, size_t len);
 
 extern char *safe_strerror (int);
 
-/* Return non-zero if the start of STRING matches PATTERN, zero
+/* Return non-zero if the start of STR matches PATTERN, zero
    otherwise.  */
 
 static inline int
-startswith (const char *string, const char *pattern)
+startswith (std::string str, std::string pattern)
 {
-  return strncmp (string, pattern, strlen (pattern)) == 0;
+  return (str.find (pattern) == 0);
+}
+
+/* Return non-zero if the end of STR matches PATTERN, zero
+   otherwise.  */
+
+static inline int
+endswith (std::string str, std::string pattern)
+{
+  return (str.rfind (pattern) != std::string::npos
+	  && str.rfind (pattern) == (str.length () - pattern.length ()));
 }
 
 ULONGEST strtoulst (const char *num, const char **trailer, int base);
diff --git a/gdb/utils.c b/gdb/utils.c
index ee37e49..e924b2c 100644
--- a/gdb/utils.c
+++ b/gdb/utils.c
@@ -3193,6 +3193,52 @@ substitute_path_component (std::string &str, const std::string &from,
     }
 }
 
+/* Approximate length of final path.  Helper for concat_path.  */
+
+static inline unsigned long
+approx_path_length (std::initializer_list<std::string> args,
+		   std::string dir_separator)
+{
+  size_t length = 0;
+
+  for (std::string arg: args)
+      length += arg.length () + dir_separator.length ();
+
+  return length;
+}
+
+/* See utils.h.  */
+
+std::string
+_concat_path (std::initializer_list<std::string> args,
+	      std::string dir_separator)
+{
+  std::string dst;
+  dst.reserve (approx_path_length (args, dir_separator));
+
+  for (std::string arg: args)
+    {
+      if (arg.empty ())
+	continue;
+
+      if (startswith (arg, dir_separator)
+	  && endswith (dst, dir_separator))
+	dst.erase (dst.length () - dir_separator.length (),
+		   dir_separator.length ());
+
+      else if (!dst.empty ()
+	       && !startswith (arg, dir_separator)
+	       && !endswith (dst, dir_separator)
+	       && dst != TARGET_SYSROOT_PREFIX)
+	dst += dir_separator;
+
+      dst += arg;
+    }
+
+  dst.shrink_to_fit ();
+  return dst;
+}
+
 #ifdef HAVE_WAITPID
 
 #ifdef SIGALRM
diff --git a/gdb/utils.h b/gdb/utils.h
index 674f672..5d68a18 100644
--- a/gdb/utils.h
+++ b/gdb/utils.h
@@ -24,6 +24,7 @@
 #include "exceptions.h"
 #include "common/scoped_restore.h"
 #include <chrono>
+#include <string>
 
 extern void initialize_utils (void);
 
@@ -143,6 +144,22 @@ extern void substitute_path_component (std::string &str,
 				       const std::string &from,
 				       const std::string &to);
 
+/* Concatenate an arbitrary number of path elements.  Adds and removes
+   directory separators as needed.
+
+   concat_path (/first, second)		=> /first/second
+   concat_path (first, second)		=> first/second
+   concat_path (first/, second)		=> first/second
+   concat_path (first, /second)		=> first/second
+   concat_path (first/, /second)	=> first/second
+   concat_path (target:, second)	=> target:second
+   */
+
+extern std::string _concat_path (std::initializer_list<std::string> args,
+				 std::string dir_separator);
+
+#define concat_path(...) _concat_path ({__VA_ARGS__}, SLASH_STRING)
+
 char *ldirname (const char *filename);
 
 extern int count_path_elements (const char *path);
-- 
2.8.4

  parent reply	other threads:[~2017-01-12 11:32 UTC|newest]

Thread overview: 28+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-01-12 11:32 [RFC 0/7] Support for Linux kernel debugging Philipp Rudo
2017-01-12 11:32 ` [RFC 1/7] Convert substitute_path_component to C++ Philipp Rudo
2017-01-12 11:32 ` [RFC 7/7] Add S390 support for linux-kernel target Philipp Rudo
2017-01-12 17:09   ` Luis Machado
2017-01-13 11:46     ` Philipp Rudo
2017-02-06 15:52     ` Yao Qi
2017-02-06 18:48       ` Andreas Arnez
2017-01-12 11:32 ` [RFC 3/7] Add basic Linux kernel support Philipp Rudo
2017-02-07 10:54   ` Yao Qi
2017-02-07 15:04     ` Philipp Rudo
2017-02-07 17:39       ` Yao Qi
2017-02-09  9:54         ` Philipp Rudo
2017-02-09 13:06     ` Yao Qi
2017-02-09 15:09       ` Philipp Rudo
2017-01-12 11:32 ` [RFC 5/7] Add commands for linux-kernel target Philipp Rudo
2017-01-12 11:32 ` [RFC 6/7] Add privileged registers for s390x Philipp Rudo
2017-01-12 11:32 ` Philipp Rudo [this message]
2017-01-12 12:00   ` [RFC 2/7] Add libiberty/concat styled concat_path function Pedro Alves
2017-01-12 13:33     ` Philipp Rudo
2017-01-12 13:48       ` Pedro Alves
2017-01-12 15:09         ` Philipp Rudo
2017-01-12 15:42           ` Pedro Alves
2017-01-12 12:56 ` [RFC 0/7] Support for Linux kernel debugging Philipp Rudo
2017-01-12 13:02 ` [RFC 4/7] Add kernel module support for linux-kernel target Philipp Rudo
2017-01-25 18:10 ` [RFC 0/7] Support for Linux kernel debugging Peter Griffin
2017-01-26 13:12   ` Philipp Rudo
2017-02-03 17:45     ` Yao Qi
2017-02-03 19:46       ` Andreas Arnez

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20170112113217.48852-3-prudo@linux.vnet.ibm.com \
    --to=prudo@linux.vnet.ibm.com \
    --cc=arnez@linux.vnet.ibm.com \
    --cc=gdb-patches@sourceware.org \
    --cc=peter.griffin@linaro.org \
    --cc=yao.qi@arm.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).