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

More ways to anchor patterns #1476

Open
g2p opened this issue Jan 12, 2024 · 5 comments · May be fixed by #1480
Open

More ways to anchor patterns #1476

g2p opened this issue Jan 12, 2024 · 5 comments · May be fixed by #1480

Comments

@g2p
Copy link

g2p commented Jan 12, 2024

I would like to use fixed-string patterns to match against a whole basename (something like find -name while disabling fnmatch or quoting metacharacters).

Right now, glob patterns are anchored to match the entire input (as determined by --full-path), and regex patterns aren't, and by adding * or ^$ it's easy to tweak them to get the desired anchoring. But with fixed-string patterns, there's no easy way to turn them into regexes or globs from a shell pipeline (one needs to call regex::escape with a matching crate version) to have patterns match start/end/whole input.

@tavianator
Copy link
Collaborator

For a workaround on the current version of fd, I believe sed 's/\([][\\*?]\)/\\\1/g' is enough to escape a string for globs. So something like

tavianator@graphene $ touch '\*?[]'
tavianator@graphene $ fd -g "$(echo '\*?[]' | sed 's/\([][\\*?]\)/\\\1/g')"
\*?[]
tavianator@graphene $ find -name "$(echo '\*?[]' | sed 's/\([][\\*?]\)/\\\1/g')"
./\*?[]

You could make a shell function for it:

globesc() {
    printf '%s' "$1" | sed 's/\([][\\*?]\)/\\\1/g'
}

fd -g "$(globesc '\*?[]')"

@tavianator
Copy link
Collaborator

I would not be terribly opposed to a new flag like --anchored, e.g. fd --anchored --fixed-strings '\*?[]'. I'm not sure what @sharkdp thinks. There sure are a lot of flags already.

@sharkdp
Copy link
Owner

sharkdp commented Jan 17, 2024

So --anchored would have an effect on regex patterns and on fixed-string patterns, but not on glob patterns (because they are anchored already)?

Or would we introduce this as an option instead of a flag (--anchored=true/false), and make it work for all three pattern options?

@g2p
Copy link
Author

g2p commented Jan 18, 2024

I've started prototyping, here's the help output:

-F, --fixed-strings
    Treat the pattern as a literal string instead of a regular expression. Note that the
    pattern would still match on a substring of the input. If you want to match on an exact
    filename, consider adding '--anchor=input' as well.

… after the --full-path flag

--no-anchor
    By default, with --glob and no --anchor arguments, the pattern must match the whole
    input (--anchor=input). This flag allows a glob pattern to match any part of the input.

--anchor <ANCHOR>
    By default, the search pattern must match the whole input for --glob (--anchor=input),
    while it can match any part of the input for --regex and --fixed-strings (--no-anchor).
    (See the --full-path option for what constitutes input)
    
    This flag allows other ways to anchor the pattern.
    
    [possible values: input-start, input-end, input, word]

(I refrained from making it as general as possible: no word-start / word-end / additive ArgAction::Append flags that are unlikely to be used. But because I had it in mind, I prefer --no-anchor to an --anchor=none that would not be additive)

@g2p
Copy link
Author

g2p commented Jan 18, 2024

globset would need to be patched, however, as anchoring is always done inside Token::to_regex_with with no builder customisation. But since globs are written by hand and still easy to script if needed, having --anchor conflict with --glob works for me.

g2p added a commit to g2p/fd that referenced this issue Jan 18, 2024
g2p added a commit to g2p/fd that referenced this issue Jan 18, 2024
g2p added a commit to g2p/fd that referenced this issue Jan 18, 2024
@g2p g2p linked a pull request Jan 18, 2024 that will close this issue
g2p added a commit to g2p/fd that referenced this issue Jan 18, 2024
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.

3 participants