branch: externals/yaml commit 64c117d0847c57b27133ffeefa4d77cfcec288e9 Author: Zachary Romero <zacrom...@posteo.net> Commit: Zachary Romero <zacrom...@posteo.net>
Fix encoding issues for nil, false, and arrays --- yaml-tests.el | 22 +++++++++++++++++++++- yaml.el | 18 ++++++++++++++++-- 2 files changed, 37 insertions(+), 3 deletions(-) diff --git a/yaml-tests.el b/yaml-tests.el index 67e0805b90..aee8f78246 100644 --- a/yaml-tests.el +++ b/yaml-tests.el @@ -512,11 +512,31 @@ keep: |+ ("nested-map" . ((1 . 2) (3 . 4) (5 . 6))) ("nested-list" . (1 2 3 4 5))) ("nested" "list" 1 2 3)))) + (should (yaml-test-round-trip + '("one" t nil :false))) (should (yaml-test-round-trip '(t nil))) + (should (yaml-test-round-trip + [1 2 3 4])) (should (yaml-encode '((("aaaa" "bbbb" "cccc") ("dddd" "eeee" "ffff") ("gggg" "hhhh" "iiii")) - ("jjjj" "kkkk" "llll") ("mmmm" "nnnn" "oooo") ("pppp" "qqqq" "rrrr"))))) + ("jjjj" "kkkk" "llll") ("mmmm" "nnnn" "oooo") ("pppp" "qqqq" "rrrr")))) + (should (yaml-encode + '(("aaaa" "bbbb" "cccc" ("dddd" "eeee" "ffff") ("gggg" "hhhh" "iiii")) + ("jjjj" "kkkk" "llll") ("mmmm" "nnnn" "oooo") ("pppp" "qqqq" "rrrr")))) + + (should (equal + (yaml-encode :null) + "null")) + (should (equal + (yaml-encode :false) + "false")) + (should (equal + (yaml-encode nil) + "null")) + (should (equal + (yaml-encode [1 2 3]) + "[1, 2, 3]"))) (provide 'yaml-tests) diff --git a/yaml.el b/yaml.el index aa9c812f75..a858a82d76 100644 --- a/yaml.el +++ b/yaml.el @@ -2371,6 +2371,7 @@ without first inserting a newline." ((yaml--scalarp object) (yaml--encode-scalar object)) ((hash-table-p object) (yaml--encode-hash-table object indent auto-indent)) ((listp object) (yaml--encode-list object indent auto-indent)) + ((arrayp object) (yaml--encode-array object indent auto-indent)) (t (error "Unknown object %s" object)))) (defun yaml--scalarp (object) @@ -2389,14 +2390,27 @@ without first inserting a newline." (s (replace-regexp-in-string "\"" "\\\\\"" s))) s)) +(defun yaml--encode-array (a indent &optional auto-indent) + "Encode array A to a string in the context of being INDENT deep. + +If AUTO-INDENT is non-nil, start the list on the current line, +auto-detecting the indentation. Functionality defers to +`yaml--encode-list'." + (yaml--encode-list (seq-map #'identity a) + indent + auto-indent)) (defun yaml--encode-scalar (s) "Encode scalar S to buffer." (cond - ((not s) (insert "nil")) + ((not s) (insert "null")) ((eql t s) (insert "true")) - ((symbolp s) (insert (symbol-name s))) + ((symbolp s) + (cond + ((eql s :null) (insert "null")) + ((eql s :false) (insert "false")) + (t (insert t)))) ((numberp s) (insert (number-to-string s))) ((stringp s) (if (string-match "\\`[-_a-zA-Z0-9]+\\'" s)