On 03/05/2024 19:06, Ihor Radchenko wrote:
Max Nikulin writes:

What I do not like in `org-babel-read' is false positive for escaped
quote when actually backslash is escaped:

(org-babel-read "\"1\\\\\" 2 \\\\\"3\"" t)
"1\\"

Fixed, on main.
https://git.savannah.gnu.org/cgit/emacs/org-mode.git/commit/?id=2028bb15c

I have no idea if "other\n\"string\"\nlines" may be passed `org-babel-read', but it is not discarded by the current regexp:

"^[[:space:]]*\"\\(.*\\)\"[[:space:]]*$"

Is there a reason why it is necessary to call `read' twice on the same content? From my point of view, result of first call may be returned.

Does `read' have other role than unescaping backslash-protected characters? Likely it can be done by `replace-regexp-in-string', see the attachment. I have tried regexp on the following string:

   (let ((cases '(("" . nil)
               ("\"" . nil)
               ("\"\"" . t)
               ("\"\\\"" . nil)
               ("\"\\\\\"" . t)
               ("a" . nil)
               ("\"a\"" . t)
               ("\\\"a\\\"" . nil)
               ("\\\"a\\\\\"" . nil)
               ("\"a\\\"\"" . t)
               ("\"aa\\\"bb\"" . t)
               (" \"aa( bb\"" . t))))
diff --git a/lisp/ob-core.el b/lisp/ob-core.el
index 470db9fe6..a44d27cba 100644
--- a/lisp/ob-core.el
+++ b/lisp/ob-core.el
@@ -3346,6 +3346,26 @@ (defun org-babel-script-escape (str &optional force)
 	  (t str))))
     (condition-case nil (org-babel-read escaped) (error escaped))))
 
+(defconst org-babel--quoted-string-regexp
+  (rx string-start
+      (zero-or-more (or space ?\n))
+      ?\"
+      (group
+       (zero-or-more
+        ;; Anything besides double quotes ended not with backslash.
+        (zero-or-one (zero-or-more (not ?\"))
+		     (not (or ?\" ?\\)))
+        ;; Skip backslashes escaping themselves.
+	(zero-or-more "\\\\")
+        ;; Escaped quotes are allowed.
+	(zero-or-one "\\\"")))
+      ?\"
+      (zero-or-more (or space ?\n))
+      string-end)
+  "Regexp matching single string in double quotes.
+Group 1 is text inside quotes. String may be optionally padded with
+spaces. Backslashes quote other characters.")
+
 (defun org-babel-read (cell &optional inhibit-lisp-eval)
   "Convert the string value of CELL to a number if appropriate.
 Otherwise if CELL looks like Lisp (meaning it starts with a
@@ -3361,15 +3381,11 @@ (defun org-babel-read (cell &optional inhibit-lisp-eval)
          ;; FIXME: Arbitrary code evaluation.
 	 (eval (read cell) t))
 	((save-match-data
-           (and (string-match "^[[:space:]]*\"\\(.*\\)\"[[:space:]]*$" cell)
-                ;; CELL is a single string
-                (with-temp-buffer
-                  (insert cell)
-                  (goto-char 1)
-                  (read (current-buffer))
-                  (skip-chars-forward "[:space:]")
-                  (eobp))))
-         (read cell))
+           (and (string-match org-babel--quoted-string-regexp cell)
+                ;; Unquote characters escaped by backslashes similar to `read'.
+                (replace-regexp-in-string
+                 "\\\\\\(?:\\(.\\)\\|\n\\)" "\\1"
+		 (match-string 1 cell) 'fixedcase nil))))
 	(t (org-no-properties cell))))
 
 (defun org-babel--string-to-number (string)

Reply via email to