Feature #5422

File.fnmatch != Dir.glob # {no,sets}

Added by Suraj Kurapati over 2 years ago. Updated over 1 year ago.

[ruby-core:40037]
Status:Closed
Priority:Normal
Assignee:-
Category:core
Target version:Next Major

Description

Hello,

The File.fnmatch methods do not support Dir.glob's set notation:

Dir.glob '{.g,t}'
=> [".gem", "test"]
File.fnmatch? '{.g,t}
', 'test'
=> false
File.fnmatch? '{.g,t}*', '.gem'
=> false

Please add set notation to fnmatch() and make it equal to glob().

Thanks for your consideration.

Associated revisions

Revision 37463
Added by Nobuyoshi Nakada over 1 year ago

dir.c: FNM_EXTGLOB

  • dir.c (filesfnmatch): match with expanding braces if FNM_EXTGLOB is set. [Feature #5422]

History

#1 Updated by Suraj Kurapati over 2 years ago

I suppose this issue could be filed as a feature request since nobody else has complained about it yet. Sorry for the wrong categorization. Thanks.

#2 Updated by Thomas Sawyer over 2 years ago

I have. Obviously it's not earth shattering, but it really SHOULD be the same, so that glob functionality can be exactly emulated within code on arrays of strings. I've had need of this a couple of times.

#3 Updated by Eric Hodel over 2 years ago

  • Tracker changed from Bug to Feature

I have moved it to the Feature tracker for you.

#4 Updated by Nobuyoshi Nakada over 2 years ago

=begin
What about a new method to expand braces?

File.expandbrace("{.g,t}*").any? {|pat| File.fnmatch?(pat, name)}
or a shorthand
File.expand
brace("{.g,t}*") {|pat| File.fnmatch?(pat, name)}
=end

#5 Updated by Nobuyoshi Nakada over 2 years ago

  • Target version changed from 2.0.0 to Next Major

#6 Updated by Thomas Sawyer over 2 years ago

Why more complexity? Is there reason that fnmatch? can't do it?

#7 Updated by Nobuyoshi Nakada over 2 years ago

Because it comes from POSIX fnmatch() function.

http://pubs.opengroup.org/onlinepubs/9699919799/functions/fnmatch.html

#8 Updated by Thomas Sawyer over 2 years ago

I see. In that case, adding a new File.glob_match? (or maybe just File.match?) would be better.

File.expand_brace won't really help here b/c it would be tied to the actual file system. Part of the usefulness of fnmatch? is that it is not.

#9 Updated by Nobuyoshi Nakada over 2 years ago

=begin
Thomas Sawyer wrote:

I see. In that case, adding a new File.glob_match? (or maybe just File.match?) would be better.

The former doesn't sound appropriate, because "glob" does not imply brace
expansion originally.

$ case foo in f{oo,ar}) echo ok;; *) echo ng;; esac
ng

$ case "f{oo,ar}" in f{oo,ar}) echo ok;; *) echo ng;; esac
ok

File.expand_brace won't really help here b/c it would be tied to the actual file system. Part of the usefulness of fnmatch? is that it is not.

Brace expansion is a string to array conversion operation, and not used only for file names in shell scripts.
=end

#10 Updated by Thomas Sawyer over 2 years ago

"The former doesn't sound appropriate, because "glob" does not imply brace expansion originally."

I'm only going by the functionality of Dir.<b>glob</b>. But, whatever name seems best.

"Brace expansion is a string to array conversion operation, and not used only for file names in shell scripts."

Okay, I mistook your example's use of * and similarity of name to File.expand_path to think that it was. File.expand_brace is fine if you think it would be useful in and of itself. But the real need is for a Dir.glob compatible match method.

#11 Updated by Suraj Kurapati about 2 years ago

What if we pass a File::FNM_EXTGLOB flag to the File.fnmatch methods?

File.fnmatch? '{.g,t}*', '.gem', File::FNM_EXTGLOB

The name "extglob" comes from the Bash/Zsh option for extended globbing.

#12 Updated by Thomas Sawyer about 2 years ago

Works for me if it works for the API.

#13 Updated by Suraj Kurapati over 1 year ago

Thanks for your agreement Thomas.

Hey Ruby-core developers, we have agreed on the following API, where passing a File::FNM_EXTGLOB flag (the name "extglob" comes from the Bash/Zsh option for extended globbing) to the File.fnmatch methods enables support for Dir.glob's set notation.

File.fnmatch? '{.g,t}*', '.gem', File::FNM_EXTGLOB

Do you also agree on this API? Could you please implement this for us? If not, I'll try submitting a patch.

Thanks for your consideration.

#14 Updated by Nobuyoshi Nakada over 1 year ago

  • Status changed from Open to Closed
  • % Done changed from 0 to 100

This issue was solved with changeset r37463.
Suraj, thank you for reporting this issue.
Your contribution to Ruby is greatly appreciated.
May Ruby be with you.


dir.c: FNM_EXTGLOB

  • dir.c (filesfnmatch): match with expanding braces if FNM_EXTGLOB is set. [Feature #5422]

Also available in: Atom PDF