Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: Slurping a file (was: more spllitting travails)



On 2/10/24, Stephane Chazelas <stephane@xxxxxxxxxxxx> wrote:
> 2024-01-14 11:34:00 +0100, Roman Perepelitsa:
> [...]
>>     function slurp() {
>>       emulate -L zsh -o no_multibyte
>>       zmodload zsh/system || return
>>       local -a content
>>       local -i i
>>       while true; do
>>         sysread 'content[++i]' && continue
>>         (( $? == 5 )) || return
>>         break
>>       done
>>       typeset -g REPLY=${(j::)content}
>>     }
> [...]
>
> IMO, it would be more useful if the result was returned in the
> variable whose name was given as argument (defaulting to REPLY
> if none was given like for read or sysread).
>
> And would be better if upon error the returned variable
> contained either what was successfully read or nothing (like
> read but unlikely sysread).
>
> Maybe something like:
>
> zslurp() {
>   emulate -L zsh -o no_multibyte
>   typeset -n _zslurp_var=${1-REPLY}
>   _zslurp_var=
>   zmodload zsh/system || return
>   local -a _zslurp_content
>   local -i _zslurp_i _zslurp_ret
>   while true; do
>     sysread '_zslurp_content[++_zslurp_i]' && continue
>     _zslurp_ret=$?
>     break
>   done
>   _zslurp_var=${(j::)_zslurp_content}
>   (( _zslurp_ret == 5 ))
> }

I believe one of the motivating factors for this function was speed,
and copying all the data an extra time probably doesn't help with
that.

-- 
Mikael Magnusson




Messages sorted by: Reverse Date, Date, Thread, Author