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

need a way to encode strings to valid var names or URLs #4150

Closed
krader1961 opened this Issue Jun 20, 2017 · 0 comments

Comments

Projects
None yet
1 participant
@krader1961
Contributor

krader1961 commented Jun 20, 2017

One way to reimplement how abbreviations are stored requires a method to convert arbitrary strings to/from valid var names. See issue #4048. This need also recently arose in the context of git completions. See issue #4147. It turns out that converting strings to valid var names is only moderately difficult using fish script:

function encode_to_var
    set -l orig_str "$argv"
    set -l new_str ''
    for letter in (string split '' $orig_str)
        if string match -qr '\w' -- $letter
            if test $letter = '_'
                set new_str {$new_str}__
            else
                set new_str "$new_str$letter"
            end
        else
            echo -n $letter | begin
                set -lx LC_ALL C
                read -lz utf8
                # string split '' -- $utf8 | od -tx1x >&2
                printf '_%02X' \'(string split '' -- $utf8)
            end | read letter
            set new_str {$new_str}$letter
        end
    end
    echo $new_str
end

Unfortunately that is ugly and slow. Doing the inverse operation in fish script will also be extremely difficult. So this issue is to track augmenting the string escape builtin by adding a --style=xxx flag. Where xxx would be var or url. The latter would allow us to eliminate the __fish_urlencode function. For completeness we would also allow the style to be script to explicitly request the current default behavior.

Note that we'll also need a string unescape that also supports the --style=xxx flag. But we'll do that work under issue #3543 since we don't currently have that string subcommand.

@krader1961 krader1961 added this to the fish 2.7.0 milestone Jun 20, 2017

@krader1961 krader1961 self-assigned this Jun 20, 2017

krader1961 added a commit to krader1961/fish-shell that referenced this issue Jun 22, 2017

implement `string escape --style=xxx`
We need a way to encode arbitrary strings into valid fish variable
names. It would also be nice if we could convert strings to valid URLs
without using the slow and hard to understand `__fish_urlencode` function.
In particular, eliminating the need to manipulate the locale.

Fixes fish-shell#4150

krader1961 added a commit to krader1961/fish-shell that referenced this issue Jun 23, 2017

implement `string escape --style=xxx`
We need a way to encode arbitrary strings into valid fish variable
names. It would also be nice if we could convert strings to valid URLs
without using the slow and hard to understand `__fish_urlencode` function.
In particular, eliminating the need to manipulate the locale.

Fixes fish-shell#4150

krader1961 added a commit to krader1961/fish-shell that referenced this issue Jun 23, 2017

implement `string escape --style=xxx`
We need a way to encode arbitrary strings into valid fish variable
names. It would also be nice if we could convert strings to valid URLs
without using the slow and hard to understand `__fish_urlencode` function.
In particular, eliminating the need to manipulate the locale.

Fixes fish-shell#4150

krader1961 added a commit to krader1961/fish-shell that referenced this issue Jun 23, 2017

implement `string escape --style=xxx`
We need a way to encode arbitrary strings into valid fish variable
names. It would also be nice if we could convert strings to valid URLs
without using the slow and hard to understand `__fish_urlencode` function.
In particular, eliminating the need to manipulate the locale.

Fixes fish-shell#4150

krader1961 added a commit to krader1961/fish-shell that referenced this issue Jun 23, 2017

implement `string escape --style=xxx`
We need a way to encode arbitrary strings into valid fish variable
names. It would also be nice if we could convert strings to valid URLs
without using the slow and hard to understand `__fish_urlencode` function.
In particular, eliminating the need to manipulate the locale.

Fixes fish-shell#4150
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment