Function: haskell-string-literal-encode

haskell-string-literal-encode is a natively compiled function defined in haskell-string.el.

Signature

(haskell-string-literal-encode STR &optional NO-QUOTES)

Documentation

Encode STR according Haskell escape rules using 7-bit ASCII representation.

The serialization has been implemented to closely match the behaviour of GHC's Show instance for Strings.

If NO-QUOTES is non-nil, omit wrapping result in quotes.

This is the dual operation to haskell-string-literal-decode.

Source Code

;; Defined in /nix/store/9lz2mhw95f9fqxr9mnq46k3f6qz3aq83-emacs-packages-deps/share/emacs/site-lisp/elpa/haskell-mode-20260904.1042/haskell-string.el
(defun haskell-string-literal-encode (str &optional no-quotes)
  "Encode STR according Haskell escape rules using 7-bit ASCII representation.

The serialization has been implemented to closely match the
behaviour of GHC's Show instance for Strings.

If NO-QUOTES is non-nil, omit wrapping result in quotes.

This is the dual operation to `haskell-string-literal-decode'."

  (let ((lastc -1))
    (let ((encode (lambda (c)
                    (let ((lc lastc))
                      (setq lastc c)
                      (if (>= c 128) ;; if non-ASCII code point
                          (format "\\%d" c)
                        ;; else, for ASCII code points
                        (if (or (and (= lc 14) (= c ?H)) ;; "\SO\&H"
                                (and (>= lc 128) (>= c ?0) (<= c ?9))) ;; "\123\&4"
                            (concat "\\&" (aref haskell-string-literal-encode-ascii-array c))
                          (aref haskell-string-literal-encode-ascii-array c)
                          ))))))

      (if no-quotes
          (mapconcat encode str "")
        (concat "\"" (mapconcat encode str "") "\"")))))