From dde3787397614ba926a915de6ee6fc8f86665b71 Mon Sep 17 00:00:00 2001 From: Gregory Hess Date: Thu, 16 May 2024 18:56:02 -0700 Subject: [PATCH] Add double quotes around strings with YAML special characters --- src/lib/yaml/writer.lua | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/lib/yaml/writer.lua b/src/lib/yaml/writer.lua index 50f6c100..25cbe2c5 100644 --- a/src/lib/yaml/writer.lua +++ b/src/lib/yaml/writer.lua @@ -29,6 +29,7 @@ function writer.write(filename, data, options) return false, serialized end +local indicators = "[-?:,%[%]{}#&*!|>'\"%%@`]" --A pattern that matches all yaml characters listed as indicators in the most recent standard, which could cause problems if simply blindly written in strings function writer.serialize(data, options, depth, firstDepth) options = options or {} depth = depth or 0 @@ -39,7 +40,12 @@ function writer.serialize(data, options, depth, firstDepth) local dataType = type(data) if dataType == "string" then - -- TODO - Use quotes when needed + --use quotes as needed + if string.match(data, indicators) then + local out = string.gsub(data, "\\", "\\\\") --escape backslashes + out = string.gsub(out, "\"", "\\\"") --escape quotes. Not sure if the reader can correctly deal with escape sequences + return "\"" .. out .. "\"" + end return data elseif dataType == "boolean" then