Skip to content
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

Dir.glob doesn't support recursive matching using double star ("**") wildcard #1433

Closed
5t111111 opened this issue Sep 10, 2015 · 6 comments · Fixed by #1487
Closed

Dir.glob doesn't support recursive matching using double star ("**") wildcard #1433

5t111111 opened this issue Sep 10, 2015 · 6 comments · Fixed by #1487

Comments

@5t111111
Copy link
Contributor

Although I don't know whether this is intended or not, Dir.glob doesn't support recursive matching using double star ("**") wildcard.

when the below directory tree and the program are given:

.
├── dir_b
│   ├── dir_c
│   │   ├── file_c_1
│   │   └── file_c_2
│   ├── file_b_1
│   └── file_b_2
├── file_a_1
├── file_a_2
└── glob_check.cr
Dir.glob("**/*") do |file|
  puts file
end

while when I run this code as Ruby I get:

$ ruby glob_check.cr 
dir_b
dir_b/dir_c
dir_b/dir_c/file_c_1
dir_b/dir_c/file_c_2
dir_b/file_b_1
dir_b/file_b_2
file_a_1
file_a_2
glob_check.cr

as Crystal I get:

$ crystal glob_check.cr 
dir_b/dir_c
dir_b/file_b_1
dir_b/file_b_2

Crystal 0.7.7 on OS X.

@jhass
Copy link
Member

jhass commented Sep 10, 2015

Yes, unfortunately the backing libc call glob(3) doesn't support it and everybody seems to reimplement it all the time.

@5t111111
Copy link
Contributor Author

thanks, i see the point.

@jhass
Copy link
Member

jhass commented Sep 10, 2015

To clarify, we totally want this, it's just that nobody had time or motivation yet to do a full Crystal glob implementation.

@asterite
Copy link
Member

Yes! We know this now for a long time but it's not a simple thing to do. As @jhass says, we would really love having this functionality, though. If somebody want's to tackle this, please let us know! It should be a fun little project, plus it's very easy to test.

@pgkos
Copy link
Contributor

pgkos commented Sep 10, 2015

I suppose that any glob pattern can be translated into a regex? If yes, then the implementation should be quite easy. We will just match the paths using the regex. Later, we might implement a hand-written glob matcher for efficiency.

If there is a ** in the pattern, we will recurse infinitely into the subdirectories. If there is no **, we can optimize the traversal by counting the depth of the pattern and not recursing too deeply.

@ysbaddaden
Copy link
Contributor

Or maybe keep using LibC.glob to match individual files/folders but manually traverse directories on **.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants