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

Literal mode for string match #9048

Open
ajeetdsouza opened this issue Jul 1, 2022 · 7 comments
Open

Literal mode for string match #9048

ajeetdsouza opened this issue Jul 1, 2022 · 7 comments

Comments

@ajeetdsouza
Copy link

I think starts-with and ends-with should be a standard inclusion in a set of string processing functions. Currently, I can use matches but I have to do a regex escape on the start of the string, and it unnecessarily complicates what would otherwise be a very simple piece of code.

@floam
Copy link
Member

floam commented Jul 12, 2022

How is string match '*foo' or string match 'foo*' so complicated?

@ajeetdsouza
Copy link
Author

How would you find if $str1 starts-with $str2? You can't simply say string match "$str2*" $str1, because $str2 might have special characters like * which would be interpreted differently.

@faho
Copy link
Member

faho commented Jul 12, 2022

That sounds like a case for a "literal" mode for string match, and you'd then have it do substring matching, prefix matching or suffix matching.

@faho faho added this to the fish-future milestone Jul 12, 2022
@ajeetdsouza
Copy link
Author

Could you elaborate on what that might look like?

@faho
Copy link
Member

faho commented Jul 12, 2022

Something like

string match --literal foo foobar # checks if "foo" is anywhere in "foobar"
string match --literal --prefix foo foobar # checks if "foobar" starts with "foo"
string match --literal --suffix foo foobar # checks if it ends with "foo"

or similar. In essence just like you would do --regex mode, but now it's "literal mode".

That way the rest of the string match machinery is there - you can do --quiet to only return a status code, or --invert to return strings that don't fit, or --ignore-case to do a case-insensitive match.

@floam
Copy link
Member

floam commented Jul 12, 2022

How would you find if $str1 starts-with $str2?

string match 'foo*' foobar
or

set str1 foobar 
set str2 foo
string match "$str2*" $str1

Oh I see what you're saying, if str2 actually already had glob characters in it, there is a problem. I was slow on the uptake there.

@floam floam added the RFC label Jul 12, 2022
@faho faho changed the title [FR] string starts-with and ends-with Literal mode for string match Jul 18, 2022
@faho faho removed the RFC label Aug 28, 2022
@vergenzt
Copy link

vergenzt commented Dec 24, 2022

This is similar to how grep has glob mode (the default), --extended-regexp mode (similar to -r) here, and --fixed-strings mode (turn off globbing like in this request).

I wonder if -f/--fixed-string might be an appropriate option name?

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

No branches or pull requests

4 participants