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

Add string-replace-fewer-backslashes feature #5556

Merged
merged 2 commits into from Mar 15, 2019

Conversation

faho
Copy link
Member

@faho faho commented Jan 19, 2019

This disables an extra round of escaping in the string replace -r
replacement string.

Currently, to add a backslash to an a or b (to "escape" it):

string replace -ra '([ab])' '\\\\\\\$1' a

7 backslashes!

This removes one of the layers, so now 3 or 4 works (each one escaped
for the single-quotes, so pcre receives two, which it reads as one literal):

string replace -ra '([ab])' '\\\\$1' a

This is backwards-incompatible as replacement strings will change
meaning, so we put it behind a feature flag.

The name is kinda crappy, though.

Fixes #5474.

TODOs:

  • Changes to fish usage are reflected in user documentation/manpages.
  • Tests have been added for regressions fixed
  • User-visible changes noted in CHANGELOG.md

We really should think about documenting feature flags better!

This disables an extra round of escaping in the `string replace -r`
replacement string.

Currently, to add a backslash to an a or b (to "escape" it):

    string replace -ra '([ab])' '\\\\\\\$1' a

7 backslashes!

This removes one of the layers, so now 3 or 4 works (each one escaped
for the single-quotes, so pcre receives two, which it reads as one literal):

    string replace -ra '([ab])' '\\\\$1' a

This is backwards-incompatible as replacement strings will change
meaning, so we put it behind a feature flag.

The name is kinda crappy, though.

Fixes fish-shell#5474.
@faho faho added this to the fish 3.1.0 milestone Jan 19, 2019
@floam
Copy link
Member

floam commented Jan 19, 2019

I'm kind of opposed to adding more feature flags. I don't think a meaningful number of people are going to opt-in to test features against their scripts; I think we should just break fix it.

@faho
Copy link
Member Author

faho commented Jan 19, 2019

I think we should just break fix it.

The main problem is that there is no way to write cross-compatible expressions. If you need a backslash in the replacement, you'll need to write two different ones.

So without a feature flag, which allows testing via status test-feature, you'll have to read version numbers, or try and retry. Which isn't great!

But I admit there is this question how far we want to take this system - do we want to put everything even slightly backwards-incompatible through it? Or do we only do it for huge stuff (like ?)? Or do we freeze the flags as they are now and never add anything, and remove the system itself eventually?

Personally, I'd like to see a better deprecation cycle!

@floam
Copy link
Member

floam commented Jan 19, 2019

Could the feature flags be used to test for the new behavior without actually requiring users to configure something to get the saner string replace? Making it possible to change the behavior isn't actually even required - you just could always have status test-feature string-replace-fewer-backslashes exit 0 on fish 3.1.0.

@faho
Copy link
Member Author

faho commented Jan 19, 2019

Could the feature flags be used to test for the new behavior without actually requiring users to configure something to get the saner string replace?

We could default it to on.

Also, as a note to myself:

I'm pretty sure we'd have to fix the paste escaping in fish_clipboard_paste

Do that!

This is the one place in fish where we use a `\` in the replacement of
a `string replace -r`, so we'll have to check the feature.
@mqudsi
Copy link
Contributor

mqudsi commented Jan 22, 2019

@floam yes, imho the feature flags should all default to on in fish 3.0 and users should be able to temporarily opt in to the legacy behavior while updating their scripts by disabling the features in question. The discoverability otherwise approaches zero.

@faho faho added the RFC label Jan 22, 2019
@faho faho merged commit 8393244 into fish-shell:master Mar 15, 2019
@faho
Copy link
Member Author

faho commented Mar 15, 2019

I've just merged this as-is. It's not a huge deal if you do use backslashes in the replacement expression (and none if you don't), but this way people at least get a fighting chance to handle it, and defaulting it to on seems like a bigger precedent that I don't want to set on something this small.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 16, 2020
@faho faho deleted the string-backslashes-feature branch March 10, 2022 08:20
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

string replace in regex-mode requires waaaay too many backslashes for the replacement
3 participants