lib: escape \ symbol

This commit is contained in:
Dmitriy Pleshevskiy 2024-05-06 02:16:02 +03:00
parent dc43d7ceda
commit 65d4a061a4
Signed by: pleshevskiy
GPG key ID: 17041163DA10A9A2
2 changed files with 6 additions and 2 deletions

View file

@ -16,7 +16,7 @@
*/ */
let let
inherit (builtins) isString isFloat isInt isBool isList isAttrs isNull isPath; 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); isNotNull = v: !(isNull v);
excludeNull = expr: filter isNotNull expr; excludeNull = expr: filter isNotNull expr;
@ -176,7 +176,7 @@ let
toLuaBool = expr: if expr then "true" else "false"; toLuaBool = expr: if expr then "true" else "false";
toLuaNumber = toString; toLuaNumber = toString;
toLuaString = expr: toLuaString = expr:
let str = validString expr; in let str = replaceStrings [ "\\" ] [ "\\\\" ] (validString expr); in
let isLines = match ".*\n.*" str; in let isLines = match ".*\n.*" str; in
if isLines != null then "[[\n${str}]]" else "\"${str}\""; if isLines != null then "[[\n${str}]]" else "\"${str}\"";
toLuaList = onValue: expr: toLuaList = onValue: expr:

View file

@ -35,6 +35,10 @@ with nix2lua; pkgs.lib.runTests {
expr = toLua "hello world"; expr = toLua "hello world";
expected = ''"hello world"''; expected = ''"hello world"'';
}; };
"test returns an escaped lua string" = {
expr = toLua ''\hello world'';
expected = ''"\\hello world"'';
};
"test returns a multiline lua string" = { "test returns a multiline lua string" = {
expr = toLua '' expr = toLua ''
hello hello