# HG changeset patch
# User Kim Alvefur <zash@zash.se>
# Date 1643314462 -3600
# Node ID 25b853e64d833637e138e364fc55f8fa663001b9
# Parent  0a44def211fa725742f889d43cb3cd7c5b614073
util.format: Skip control code escaping when doing full serialization

Fixes that a multi-line string ended up "like\
\9this" instead of "like\nthis" as can be demonstrated by somehow
initiating a connection to a HTTP server.

diff -r 0a44def211fa -r 25b853e64d83 spec/util_format_spec.lua
--- a/spec/util_format_spec.lua	Thu Jan 27 16:23:26 2022 +0100
+++ b/spec/util_format_spec.lua	Thu Jan 27 21:14:22 2022 +0100
@@ -37,6 +37,11 @@
 			end)
 		end
 
+		it("escapes multi-line strings", function ()
+			assert.equal("Hello\n\tWorld", format("%s", "Hello\nWorld"))
+			assert.equal("\"Hello\\nWorld\"", format("%q", "Hello\nWorld"))
+		end)
+
 		-- Tests generated with loops!
 		describe("nil", function ()
 			describe("to %c", function ()
@@ -561,8 +566,8 @@
 			describe("to %q", function ()
 				it("works", function ()
 					assert.equal("\"hello\"", format("%q", "hello"))
-					assert.equal("\"foo \226\144\129\226\144\130\226\144\131 bar\"", format("%q", "foo \001\002\003 bar"))
-					assert.equal("\"nödåtgärd\"", format("%q", "n\195\182d\195\165tg\195\164rd"))
+					assert.equal("\"foo \\001\\002\\003 bar\"", format("%q", "foo \001\002\003 bar"))
+					assert.equal("\"n\\195\\182d\\195\\165tg\\195\\164rd\"", format("%q", "n\195\182d\195\165tg\195\164rd"))
 					assert.equal("\"n\\195\\182d\\195\\165tg\\195\"", format("%q", "n\195\182d\195\165tg\195"))
 				end);
 			end);
diff -r 0a44def211fa -r 25b853e64d83 util/format.lua
--- a/util/format.lua	Thu Jan 27 16:23:26 2022 +0100
+++ b/util/format.lua	Thu Jan 27 21:14:22 2022 +0100
@@ -108,7 +108,7 @@
 		if t == "string" and option ~= "p" then
 			if not valid_utf8(arg) then
 				option = "q";
-			else
+			elseif option ~= "q" then -- gets fully escaped in the next block
 				args[i] = arg:gsub("[%z\1-\8\11-\31\127]", control_symbols):gsub("\n\t?", "\n\t");
 				return spec;
 			end