public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
From: Lancelot SIX <lsix@lancelotsix.com>
To: Simon Marchi <simon.marchi@polymtl.ca>
Cc: gdb-patches@sourceware.org, Simon Marchi <simon.marchi@efficios.com>
Subject: Re: [PATCH 5/5] gdbsupport: add path_join function
Date: Fri, 15 Apr 2022 14:38:27 +0000	[thread overview]
Message-ID: <20220415143827.t2nlcfhmh2pondev@ubuntu.lan> (raw)
In-Reply-To: <20220414200137.3479373-5-simon.marchi@polymtl.ca>

Hi,

> +/* Join COMPONENT and all subsequent null-terminated string arguments into a
> +   single path.
> +
> +   The last argument must be nullptr to denote the end of the argument list
> +
> +   Empty arguments or arguments consisting only of directory separators are
> +   ignored.  */
> +
> +extern std::string path_join (const char *component, ...)
> +  ATTRIBUTE_SENTINEL;

The last part of the interface seems odd to me.  Mostly because it is
quite different to the os.path.join API from python.

Most notable, in python `os.path.join (a, b)` will yield b if b is an
absolute path:

    >>> os.path.join("/home/foo", "/")
    '/'
    >>> os.path.join("/foo", "/bar")
    '/bar'

One rational for this behavior would be to try to mimic using cd on each
argument in order:

    def strange_join(*args):
        cwd = os.getcwd()
	try:
	    for comp in args:
	        os.chdir(comp)
	    return os.getcwd()
        finally:
	    os.chdir(cwd)

Of course this implementation would be wrong for multiple reasons:
- it must work for dirs which do not exist
- the ".." remain ".."
- many other...

This behaviour regarding abs path is also consistent what I have with
std::filesystem in c++17 (which we be nice if we could use, but
unfortunately for the moment we have to stick to c++11)

    auto p = std::filesystem::path {"/foo"} / "/bar";
    assert (p == "/bar");

If you compare to one of the testcase you add, the behavior is
different:

    s = ::path_join ("/foo", "/bar", nullptr);
    SELF_CHECK (s == "/foo/bar");

Is this different semantics made on purpose?

Note that there is another part of the behavior which is different in
your proposal v.s.  python and std::filesystem.  Appending an empty
component at the end ensures that the path terminates with a path
separator:

    >>> os.path.join("foo", "")
    "foo/"

If I use your patch, I see:

    s = ::path_join ("foo", "", nullptr);
    SELF_CHECK (s == "foo");

Using similar semantics to std::filesystem might allow us to replace our
implementation with the c++ standard library when we switch to c++17.

On a different note, I am not sure I am a huge fan of the last nullptr
argument which is required.

You can achieve a similar interface except for this nullptr using light
variadic templates (not too much, should be easy enough to maintain).
Is this something you considered?

One implementation could be:

    /* The real worker can be compiled in a .cc file if you wish.  */
    extern std::string path_join_1 (std::string a, std::string b);

    template <typename ...Args)
    std::string
    path_join (std::string a, std::string b, Args... comps)
    {
      return path_join (path_join (a, b), comps...);
    }

    template <>
    std::string
    path_join (std::string a, std::string b)
    {
      return path_join_1 (a, b);
    }

I do not pretend this is optimal (it is not).  In this POC I pass all
arguments as std::string and by value, this is probably not what you
want to do in a production grade implementation, but this just gives the
overall idea.

What do you think?

Best,
Lancelot.

  parent reply	other threads:[~2022-04-15 14:38 UTC|newest]

Thread overview: 28+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-04-14 20:01 [PATCH 1/5] gdb: call gdb_tilde_expand instead of gdb_tilde_expand_up in source_script_with_search Simon Marchi
2022-04-14 20:01 ` [PATCH 2/5] gdbsupport: make gdb_abspath return an std::string Simon Marchi
2022-04-18 19:41   ` Tom Tromey
2022-04-18 20:09     ` Pedro Alves
2022-04-18 20:11     ` Simon Marchi
2022-04-14 20:01 ` [PATCH 3/5] gdbsupport: make gdb_realpath_keepfile " Simon Marchi
2022-04-18 19:44   ` Tom Tromey
2022-04-14 20:01 ` [PATCH 4/5] gdb: use gdb_tilde_expand instead of gdb_tilde_expand_up in source_script_with_search Simon Marchi
2022-04-18 19:44   ` Tom Tromey
2022-04-18 20:12     ` Simon Marchi
2022-04-14 20:01 ` [PATCH 5/5] gdbsupport: add path_join function Simon Marchi
2022-04-15  5:59   ` Eli Zaretskii
2022-04-18 18:11     ` Simon Marchi
2022-04-18 18:30       ` Eli Zaretskii
2022-04-18 19:24       ` Pedro Alves
2022-04-15 14:38   ` Lancelot SIX [this message]
2022-04-15 16:55     ` Lancelot SIX
2022-04-18 18:43     ` Simon Marchi
2022-04-18 19:09       ` Pedro Alves
2022-04-18 19:12         ` Simon Marchi
2022-04-18 20:55           ` Simon Marchi
2022-04-18 21:07             ` Pedro Alves
2022-04-19  0:19               ` Simon Marchi
2022-04-18 19:22       ` Pedro Alves
2022-04-18 20:01       ` Tom Tromey
2022-04-18 23:11       ` Lancelot SIX
2022-04-20  0:22         ` Simon Marchi
2022-04-18 19:36 ` [PATCH 1/5] gdb: call gdb_tilde_expand instead of gdb_tilde_expand_up in source_script_with_search Tom Tromey

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=20220415143827.t2nlcfhmh2pondev@ubuntu.lan \
    --to=lsix@lancelotsix.com \
    --cc=gdb-patches@sourceware.org \
    --cc=simon.marchi@efficios.com \
    --cc=simon.marchi@polymtl.ca \
    /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).