Subject: Re: bug#54846: [PATCH] gnu: linux: Escape the values of string-type kconfig options

From: Ludovic Courtès <[email-masked]>
Date: Tue, 12 Apr 2022 23:39:07 +0200

Hi,

antlers <[email-masked]> skribis:

>  * gnu/packages/linux.scm (config->string): add escape-string
>
> Handles characters within the set
> (char-set-intersection char-set:ascii char-set:printing), removing
> those which are known to be unsupported.

[...]

>  (define (config->string options)
> +  (define (escape-string str)
> +    "Returns STR with the escapes necessary to be read as a string-type
> +    option's value. Handles characters within the set (char-set-intersection
> +    char-set:ascii char-set:printing), removing those which are known to be
> +    unsupported."

Nitpick: You can turn the docstring into a comment since the docstring
wouldn’t be accessible anyway.

> +    (fold (match-lambda* (((match? fmt) str)
> +			  (transform-string str match?
> +					    (cut format #f fmt <>))))

Please avoid tabs.

‘transform-string’ is from (texinfo string-utils), which is not imported
here.  IMO, we’d rather avoid depending on this module since it’s really
designed for the Texinfo machinery.

> +	  str
> +	  `((#\# "") ; No known way to escape # characters.
> +	    (#\$ "$~a")
> +	    ("\"\\'`" "\\~a")
> +	    (";:()#" "\\\\~a")
> +	    ("|" "\\\\\\~a")
> +	    ;; No support for tabs, newlines, etc.
> +	    (,(char-set->string (ucs-range->char-set 9 14)) ""))))

I wonder if this should be implemented in terms of ‘string-fold’
instead:

  (string-concatenate-reverse
    (string-fold (lambda (chr result)
                   (match chr
                     (#\# (cons "" result))
                     ;; …
                     (_ (cons (string chr) result))))
                 '()
                 str))

Thoughts?

Thanks,
Ludo’.