Page 2 of 2

Re: Read the contents of folder

Posted: Tue Oct 02, 2012 10:46 am
by vladislavbyk
kikito wrote:Come on guys. Lua's patterns have always been difficult. Give vlad a break.

Besides, getting the pattern is actually tricker than it looks. BlackBulletV, who is very experienced with Lua, got it wrong :) (his pattern will fail with files with a double extension, such as myfile.tar.gz).

Here's what I'd use to get the file name only (this is what vlad needs - probably there's a simpler pattern, but this one works very well):

Code: Select all

function getBasename(filename)
  return filename:match("^([^%.]*)%.?") -- "myfile.lua" -> "myfile"
end
Here's an extended version (compatible with the previous one) which does a little more; it returns the corresponding extension:

Code: Select all

function getBasenameAndExtension(filename)
  return filename:match("^([^%.]*)%.?(.*)$") -- "myfile.lua" -> "myfile", "lua"
end
Tests:

Code: Select all

for _,filename in ipairs({"myfile.lua", "myfile", ".bashrc", "myfile.tar.gz", "myfile.yeah-baby.yeah"}) do
  print(("getBasename(%q) = %q"):format(filename, getBasename(filename)))
  print(("getBasenameAndExtension(%q) = %q, %q"):format(filename, getBasenameAndExtension(filename)))
end
Result:

Code: Select all

getBasename("myfile.lua") = "myfile"
getBasenameAndExtension("myfile.lua") = "myfile", "lua"
getBasename("myfile") = "myfile"
getBasenameAndExtension("myfile") = "myfile", ""
getBasename(".bashrc") = ""
getBasenameAndExtension(".bashrc") = "", "bashrc"
getBasename("myfile.tar.gz") = "myfile"
getBasenameAndExtension("myfile.tar.gz") = "myfile", "tar.gz"
getBasename("myfile.yeah-baby.yeah") = "myfile"
getBasenameAndExtension("myfile.yeah-baby.yeah") = "myfile", "yeah-baby.yeah"
Nice, thanks you very much for explanation!

Re: Read the contents of folder

Posted: Tue Oct 02, 2012 2:35 pm
by bartbes
kikito wrote:his pattern will fail with files with a double extension, such as myfile.tar.gz
This is unfortunate, we have wildly different views here, then. Tar.gz is perhaps the only double extension I do care about, otherwise it's generally just a dot in the filename. Also, I wouldn't say the extension of ".bashrc" is "bashrc".

Re: Read the contents of folder

Posted: Tue Oct 02, 2012 2:53 pm
by kikito
bartbes wrote:Tar.gz is perhaps the only double extension I do care about
To be completely honest it's the same case to me - in LÖVE I doubt I'll ever need any other "composed" file extension. But if I get a .tar.gz, I want the thing to work intuitively and remove the gz and the tar too, dammit! :)

In ruby on rails I there is a lot more of double/triple extension: index.html.erb and script.js.coffee.erb are not uncommon.
bartbes wrote:Also, I wouldn't say the extension of ".bashrc" is "bashrc".
I don't have a strong opinion about that; It was simpler to implement that way, so I did. I doubt many of LÖVERs will have to deal with .dotfiles anyway. The test is there to ensure that the functions work without error in all cases (i.e. not raising errors or returning nils) not to express a particular personal preference.

Re: Read the contents of folder

Posted: Tue Oct 02, 2012 10:35 pm
by BlackBulletIV
kikito wrote:Besides, getting the pattern is actually tricker than it looks. BlackBulletV, who is very experienced with Lua, got it wrong :) (his pattern will fail with files with a double extension, such as myfile.tar.gz).
Ah yes, you are correct. Never thought about that since I never deal with double extensions these days.

EDIT: Just for the hell of it, here's how to extract the filename from a path with folders:

Code: Select all

path:match("([^/]+)$")
("foo/bar/image.png"):match("([^/]+)$") -- image.png
You could also run kikito's getBasename on the string before or after that match to remove the extension.