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

support $ interpolation in glob"..." macro? #27

Open
stevengj opened this issue Mar 26, 2021 · 8 comments
Open

support $ interpolation in glob"..." macro? #27

stevengj opened this issue Mar 26, 2021 · 8 comments

Comments

@stevengj
Copy link

stevengj commented Mar 26, 2021

Since $ isn't used for glob patterns, it might be nice to implement Julia interpolation when a non-backslashed $ is encountered in glob"..." rather than treating it as a literal dollar sign.

(See also discourse.)

@stevengj stevengj changed the title support support $ interpolation in glob"..." macro? Mar 26, 2021
@ryofurue
Copy link

ryofurue commented Jan 5, 2025

As discussed in #47 , you can use Glob.GlobMatch(...) to construct a Glob from a String.

This strategy is analogous to regex: Instead of allowing for string interpolation in r"...", it provides the Regex(...) constructor. See the official documentation of regex.

@stevengj
Copy link
Author

stevengj commented Jan 5, 2025

Using Glob.GlobMatch("...") has the disadvantage of requiring you to escape backslashes. You could do it that way, but why not support $ interpolation in the glob macro itself?

Regexes are different, because $ is already a metacharacter for regexes.

@ryofurue
Copy link

ryofurue commented Jan 6, 2025

Regexes are different, because $ is already a metacharacter for regexes.

I see your point and I agree with you! Thank you for your clarification.

While we are at it, I also wish the standard string concatenation worked on Globs. With Regex, you can do something like this:

a = r"hello"
b = r".*\.jpg"
m = match(a*b, "hello3.jpg") # matches

You sometimes prefer string interpolation and sometimes concatenation, depending on the complexity.

@stevengj
Copy link
Author

stevengj commented Jan 6, 2025

I would make concatenation a separate issue, because its implementation seems entirely separate from (and much simpler than) that of interpolation.

@stevengj
Copy link
Author

stevengj commented Jan 8, 2025

Note that @vtjnash commented on this issue on discourse and also #47 (comment)

He noted that if you are using this to interpolate path components then you have to be careful, because arbitrary paths might contain glob metacharacters that should be escaped to be treated as literals within a glob.

@vtjnash
Copy link
Owner

vtjnash commented Jan 8, 2025

Right, as I mentioned there, this is somewhat intentionally unimplemented, as putting a path into the fnmatch is rather risky (it doesn't work well on Windows with the path separator is different or on Unix where metacharacters are legal in the path itself), so doing exactly raw text seemed the least surprising, and the most similar to fnmatch in a shell

We also could do something slightly special (relative to most shells), where fnmatch treats the contents of the $ as something that must be matched explicitly (and ignores embedded metacharacters) by putting a \ before every (special) character in the value to splice in

@ryofurue
Copy link

ryofurue commented Jan 9, 2025

He noted that if you are using this to interpolate path components then you have to be careful, because arbitrary paths might contain glob metacharacters that should be escaped to be treated as literals within a glob.

As I said, I understand your point, but my argument is different . . . If we didn't have Glob in the first place, we would be doing this:

a = "hello"
for file in readdir()
   m = match(Regex("$(a).*\\.jpg")) # You have to be careful!
   isnothing(m) && continue # skip the file if it doesn't match the pattern
   # . . .
end

The same risk as you are talking about exists in the above code.

For that reason, I didn't understand why you needed to emphasize this risk. (It seemed to me that you were pointing to the risk as an argument not to implement these features.)

@vtjnash
Copy link
Owner

vtjnash commented Jan 9, 2025

I am not opposed, but just noting that since globs are not filepaths (e.g. glob"a//b" does not match the path "a//b" and starting with "/" isn't legal), there seems like a lot of hidden complexity in what happens if someone does try to include something there other than a simple file path ending in / or followed by /. That doesn't happen at all if you use that same string as the readdir argument for the starting directory, or happens more obviously if you construct the Glob object explicitly. The API here was intended to be loosely based around regex"" vs Regex(raw""), which is how regexes are relevant to understanding what is expected behavior here (otherwise fnmatch isn't especially necessary, since unlike with PCRE2's implementation of a regex, it is essentially free to construct an FnMatcher at runtime)

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

No branches or pull requests

3 participants