Hi,

On Sun, Nov 28, 2021 at 7:35 AM lacygoill <[email protected]> wrote:

> do you have an idea for a better conversion ("eee" -> "\144\144\144)
>
> I would try something like this:
>
> :echo 'eee'->substitute('.', {-> '\' .. printf('%o', 
> submatch(0)->char2nr())}, 'g')
>
> \145\145\145
>
>
> ------------------------------
>
> and maybe even one that only converts the non-ascii parts?
>
> I think you can match non-ascii characters with this regex:
>
> [^\x00-\x7f]
>
>
> But I'm not sure the previous command would work as expected on non-ascii
> characters. Consider this simple test on the string containing the text
> résumé:
>
> :echo 'résumé'->substitute('[^\x00-\x7f]', {-> '\' .. printf('%o', 
> submatch(0)->char2nr())}, 'g')
>
> r\351sum\351
>
>
> Now, if you evaluate this in a double-quoted string, you don't get back
> résumé. Instead you get r<e9>sum<e9>:
>
> :echo "r\351sum\351"
>
> r<e9>sum<e9>
>
>
> AFAIK, there is no builtin function to encode a character into its actual
> byte sequence, as reported by the normal g8
> <https://vimhelp.org/various.txt.html#g8> command. I guess you would have
> to defer the task to xxd(1) which should ship with Vim:
>

Can you use the str2list() function for this?

- Yegappan


> :echo 'résumé'->substitute('[^\x00-\x7f]', {-> systemlist('xxd -plain', 
> submatch(0))->get(0, submatch(0))->split('..\zs')->map({_, v -> ('0x' .. 
> v)->eval()->printf('\%o')})->join('')}, 'g')
>
> r\303\251sum\303\251
>
>
> The latter sequence is correctly evaluated back into résumé:
>
> :echo "r\303\251sum\303\251"
>
> résumé
>
>
> Unfortunately, calling an external process might cause the plugin to be
> too slow. If that's the case, we would need a new Vim script function
> providing the equivalent of the normal g8 command.
>
>
>

-- 
-- 
You received this message from the "vim_dev" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

--- 
You received this message because you are subscribed to the Google Groups 
"vim_dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/vim_dev/CAAW7x7%3DHc9G1v9zPjiYmyFdBycm%3Dq%3D6weQKvxgDPv%2BeSzwGTmg%40mail.gmail.com.

Raspunde prin e-mail lui