Splitting complex paths using get_path_info

26 views
Skip to first unread message

John Stiles

unread,
Apr 13, 2021, 12:11:52 PM4/13/21
to gn-dev

I am using this GN code to split apart the filename and extension from a path:

    ext = get_path_info(dst, "name")

    ext = get_path_info(dst, "extension")

get_path_info considers extensions as "the substring following the last period following the last slash." Is there any way to retrieve "the substring following the first period following the last slash"?

As an example, assume this is your input:

    file.cpp.zip

The current GN implementation of get_path_info will split these as "file.cpp" and ".zip". This is totally reasonable for some use cases; unfortunately, for my use case, I need "file" and ".cpp.zip" instead. Is there an API I can use to split the path this way?





Brett Wilson

unread,
Apr 13, 2021, 12:32:06 PM4/13/21
to John Stiles, gn-dev
This doesn't exist, sorry.

Brett

John Stiles

unread,
Apr 13, 2021, 1:03:47 PM4/13/21
to gn-dev, Brett Wilson, gn-dev, John Stiles
Would a FR bug be useful? Or I can probably do it manually via string-splitting instead.

Brett Wilson

unread,
Apr 13, 2021, 1:12:17 PM4/13/21
to John Stiles, gn-dev
On Tue, Apr 13, 2021 at 10:03 AM John Stiles <johns...@google.com> wrote:
Would a FR bug be useful? Or I can probably do it manually via string-splitting instead.

The behavior you're asking for doesn't seem to be a normal definition of "extension" as far as a typical OS path class goes. And it seems like a dangerous assumption that file names don't contain dots, as many files do contain dots that don't count as an extension. So I wouldn't want to encourage this type of pattern with a real feature.

It sounds like you're specifically interested in certain two-level extensions, in which case I would recommend extracting the "name" and then getting the second-level extension from that.

Brett

John Stiles

unread,
Apr 13, 2021, 1:37:04 PM4/13/21
to gn-dev, Brett Wilson, gn-dev, John Stiles

Using get_path_info twice in a row seemed to do the trick. If this is the suggested approach, SGTM.

        # For input path "file.aa.bb", name will contain "file" and ext will contain ".aa.bb".

        # For input path "file.cc", name will contain "file" and ext will contain ".cc".

        nameTmp = get_path_info(dst, "name")

        name = get_path_info(nameTmp, "name")

        ext = get_path_info(nameTmp, "extension")

        ext += get_path_info(dst, "extension")

Reply all
Reply to author
Forward
0 new messages