From 65d4a061a42dbbb2cc326a4554dc8bac350999b0 Mon Sep 17 00:00:00 2001 From: Dmitriy Pleshevskiy Date: Mon, 6 May 2024 02:16:02 +0300 Subject: [PATCH] lib: escape \ symbol --- lib.nix | 4 ++-- lib.test.nix | 4 ++++ 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/lib.nix b/lib.nix index 14f25b5..9395260 100644 --- a/lib.nix +++ b/lib.nix @@ -16,7 +16,7 @@ */ let inherit (builtins) isString isFloat isInt isBool isList isAttrs isNull isPath; - inherit (builtins) concatStringsSep filter mapAttrs attrValues concatLists match; + inherit (builtins) concatStringsSep filter mapAttrs attrValues concatLists match replaceStrings; isNotNull = v: !(isNull v); excludeNull = expr: filter isNotNull expr; @@ -176,7 +176,7 @@ let toLuaBool = expr: if expr then "true" else "false"; toLuaNumber = toString; toLuaString = expr: - let str = validString expr; in + let str = replaceStrings [ "\\" ] [ "\\\\" ] (validString expr); in let isLines = match ".*\n.*" str; in if isLines != null then "[[\n${str}]]" else "\"${str}\""; toLuaList = onValue: expr: diff --git a/lib.test.nix b/lib.test.nix index 34d1822..48c3b66 100644 --- a/lib.test.nix +++ b/lib.test.nix @@ -35,6 +35,10 @@ with nix2lua; pkgs.lib.runTests { expr = toLua "hello world"; expected = ''"hello world"''; }; + "test returns an escaped lua string" = { + expr = toLua ''\hello world''; + expected = ''"\\hello world"''; + }; "test returns a multiline lua string" = { expr = toLua '' hello