Skip to content

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

@krader1961

Description

@krader1961

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.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions