patch 9.1.1669: Vim script: no support for URI de-/encoding

Problem:  Vim script: no support for URI de-/encoding
          (ubaldot)
Solution: Add the uri_encode() and uri_decode() functions
          (Yegappan Lakshmanan)

fixes: #17861
closes: #18034

Signed-off-by: Yegappan Lakshmanan <yegappan@yahoo.com>
Signed-off-by: Christian Brabandt <cb@256bit.org>
This commit is contained in:
Yegappan Lakshmanan
2025-08-23 06:26:16 -04:00
committed by Christian Brabandt
parent da34f84847
commit 454c7ea484
9 changed files with 271 additions and 0 deletions

View File

@ -747,6 +747,8 @@ undofile({name}) String undo file name for {name}
undotree([{buf}]) List undo file tree for buffer {buf}
uniq({list} [, {func} [, {dict}]])
List remove adjacent duplicates from a list
uri_decode({string}) String URI-decode a string
uri_encode({string}) String URI-encode a string
utf16idx({string}, {idx} [, {countcc} [, {charidx}]])
Number UTF-16 index of byte {idx} in {string}
values({dict}) List values in {dict}
@ -12187,6 +12189,59 @@ uniq({list} [, {func} [, {dict}]]) *uniq()* *E882*
Return type: list<{type}>
uri_decode({string}) *uri_decode()*
Returns the URI-decoded form of {string}, reversing
percent-encoding (converting sequences like "%3D" back to
the corresponding character).
The decoding follows standard percent-decoding rules:
- "%HH" is replaced with the character for the hex value
HH.
- If the decoded bytes form valid UTF-8, they are combined
into the corresponding character(s). Otherwise, the
bytes are kept as-is.
- Invalid or incomplete encodings (e.g. "%GZ", "%3", or a
trailing "%") are left unchanged.
Returns an empty String if {string} is empty.
Example: >
:echo uri_decode('c%3A%5Cmy%5Cdir%5Cfoo%20bar')
c:\my\dir\foo bar
:echo uri_decode('%CE%B1%CE%B2%CE%B3')
αβγ
<
Can also be used as a |method|: >
mystr->uri_decode()
<
Return type: |String|
uri_encode({string}) *uri_encode()*
Returns the URI-encoded form of {string}. URI encoding
replaces unsafe or reserved characters with percent-encoded
sequences.
The encoding follows standard percent-encoding rules:
- Alphanumeric characters [0-9A-Za-z] remain unchanged.
- The characters "-", "_", ".", and "~" also remain
unchanged.
- All other characters are replaced with "%HH", where HH
is the two-digit uppercase hexadecimal value.
- Existing percent-encoded sequences are not modified.
Returns an empty String if {string} is empty.
Example: >
:echo uri_encode('c:\my\dir\foo bar')
c%3A%5Cmy%5Cdir%5Cfoo%20bar
:echo uri_encode('key=value&name=αβγ')
key%3Dvalue%26name%3D%CE%B1%CE%B2%CE%B3
<
Can also be used as a |method|: >
mystr->uri_encode()
<
Return type: |String|
*utf16idx()*
utf16idx({string}, {idx} [, {countcc} [, {charidx}]])
Same as |charidx()| but returns the UTF-16 code unit index of

View File

@ -11101,6 +11101,8 @@ unix os_unix.txt /*unix*
unlisted-buffer windows.txt /*unlisted-buffer*
up-down-motions motion.txt /*up-down-motions*
uppercase change.txt /*uppercase*
uri_decode() builtin.txt /*uri_decode()*
uri_encode() builtin.txt /*uri_encode()*
urxvt-mouse options.txt /*urxvt-mouse*
use-visual-cmds version4.txt /*use-visual-cmds*
useful-mappings tips.txt /*useful-mappings*

View File

@ -807,6 +807,8 @@ String manipulation: *string-functions*
str2blob() convert a list of strings into a blob
blob2str() convert a blob into a list of strings
items() get List of String index-character pairs
uri_encode() URI-encode a string
uri_decode() URI-decode a string
List manipulation: *list-functions*
get() get an item without error for wrong index

View File

@ -41788,6 +41788,8 @@ Functions: ~
|str2blob()| convert a List of strings into a blob
|test_null_tuple()| return a null tuple
|tuple2list()| turn a Tuple of items into a List
|uri_decode()| URI-decode a string
|uri_encode()| URI-encode a string
|wildtrigger()| trigger wildcard expansion