-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[RFC] extractFilename("usr/lib/")
should return "lib" (not "") and be called baseName
(since works with dirs)
#8341
Comments
Meh, "basename" is a terrible name, might as well call it |
baseName would be a familiar name as it's used by many shells too (bash, tcsh). Another data point: https://www.gnu.org/software/emacs/manual/html_node/elisp/File-Name-Components.html UPDATE: This means that it would be nice if extractFilename were renamed to basename, but really that's not a big deal. I'd prefer the current behavior of extractFilename to remain. |
not married to |
Continuing my previous comment, the Emacs basename would return "" too for "/foo/lib/". |
The Python result makes more sense to me.
I disagree with this too. |
@timotheecour Emacs has a strong requirement (seen in many functions, API) that directory names always end in "/". In Unix systems, a directory is a "file" too! Semantically everything is a "file". So sticking to a "/"-ending is the best way to distinguish directories. So in response to your rationale section:
That's a bad idea. Then we cannot distinguish whether an input arg is a file or a directory. How about having something as
If the extractFilename is returning "", it means that you are looking at a directory name, not a "Filename". So it makes perfect sense to me. It can be used to check of the input argument is a directory (if that code is using a strong convention of always ending directory names in "/"). |
@kaushalmodi unfortunately there are tons of code that doesn't follow this convention, so we can't use that logic @dom96 I just fixed a typo in top post, I meant Anyway looks like these 2 statements are contradicting:
If we're not introducing basename/pathTail, other possibilities are:
|
import os
let (head, tail) = splitPath("/foo/bar")
echo head, " ", tail |
the main issue is trailing import os
let (head, tail) = splitPath("/foo/bar/")
echo head, ":", tail,":"
/foo/bar::
|
At least to me it seems to be a special case that can be dealt with: import os, strformat
proc myBaseName(path: string): string =
var (head, tail) = splitPath(path)
if tail == "":
(head, tail) = splitPath(head)
return tail
let paths = ["/foo/bar/",
"/foo/bar"]
for p in paths:
echo fmt"path = `{p}', basename = `{p.myBaseName}'" Outputs:
|
Well we can fix/change |
/cc @dom96 this is relevant for #8315 (comment)
Most languages have a
baseName
function instead ofextractFilename
; and I seeextractFilename
comes fromDelphi
,FreePascal
, which are more "fringe" for Nim userbase (IMO) compared to the other ones.More importantly,
extractFilename
is not a good name because it works with directories, eg in "/home/bob", "bob" is not really a filename (it's a directory); that's why every other language uses basename.path semantics can be ambiguous due to things like trailing
/
etc; it's annoying but we should find the most sensible semantics.Currently
extractFilename("usr/lib/")
returns""
; let's see how other languages handle this corner case:these return "lib":
these return ".":
these return "":
proposal: deprecate
extractFilename
(keeping its behavior so code won't break), and introducebaseName
which would work as unix, C, D, node.js.rationale:
/
or not (eg, in environment variables, and shouldn't affect their basename. eg:DESTDIR=/tmp/foo run_prog
orDESTDIR=/tmp/foo/ run_prog
should have same semanticsbasename
convention,EDITisHidden("./git/")
isHidden(".git/")
would be true which makes more sense; using python's convention, it would be false (with fix #8225 os.isHidden was buggy on posix #8315 implementation)would a PR be accepted for that?
The text was updated successfully, but these errors were encountered: