Commit 0b661ce3 authored by Naïm Favier's avatar Naïm Favier Committed by pennae
Browse files

lib/generators.toPretty: escape strings properly

parent 0fa7b1b0
Loading
Loading
Loading
Loading
+13 −11
Original line number Diff line number Diff line
@@ -297,17 +297,19 @@ rec {
    else if isFloat    v then "~${toString v}"
    else if isString   v then
      let
        # Separate a string into its lines
        newlineSplits = filter (v: ! isList v) (builtins.split "\n" v);
        # For a '' string terminated by a \n, which happens when the closing '' is on a new line
        multilineResult = "''" + introSpace + concatStringsSep introSpace (lib.init newlineSplits) + outroSpace + "''";
        # For a '' string not terminated by a \n, which happens when the closing '' is not on a new line
        multilineResult' = "''" + introSpace + concatStringsSep introSpace newlineSplits + "''";
        # For single lines, replace all newlines with their escaped representation
        singlelineResult = "\"" + libStr.escape [ "\"" ] (concatStringsSep "\\n" newlineSplits) + "\"";
      in if multiline && length newlineSplits > 1 then
        if lib.last newlineSplits == "" then multilineResult else multilineResult'
      else singlelineResult
        lines = filter (v: ! isList v) (builtins.split "\n" v);
        escapeSingleline = libStr.escape [ "\\" "\"" "\${" ];
        escapeMultiline = libStr.replaceStrings [ "\${" "''" ] [ "''\${" "'''" ];
        singlelineResult = "\"" + concatStringsSep "\\n" (map escapeSingleline lines) + "\"";
        multilineResult = let
          escapedLines = map escapeMultiline lines;
          # The last line gets a special treatment: if it's empty, '' is on its own line at the "outer"
          # indentation level. Otherwise, '' is appended to the last line.
          lastLine = lib.last escapedLines;
        in "''" + introSpace + concatStringsSep introSpace (lib.init escapedLines)
                + (if lastLine == "" then outroSpace else introSpace + lastLine) + "''";
      in
        if multiline && length lines > 1 then multilineResult else singlelineResult
    else if true  ==   v then "true"
    else if false ==   v then "false"
    else if null  ==   v then "null"
+8 −8
Original line number Diff line number Diff line
@@ -727,7 +727,7 @@ runTests {
      float = 0.1337;
      bool = true;
      emptystring = "";
      string = ''fno"rd'';
      string = "fn\${o}\"r\\d";
      newlinestring = "\n";
      path = /. + "/foo";
      null_ = null;
@@ -735,7 +735,7 @@ runTests {
      functionArgs = { arg ? 4, foo }: arg;
      list = [ 3 4 function [ false ] ];
      emptylist = [];
      attrs = { foo = null; "foo bar" = "baz"; };
      attrs = { foo = null; "foo b/ar" = "baz"; };
      emptyattrs = {};
      drv = deriv;
    };
@@ -744,7 +744,7 @@ runTests {
      float = "~0.133700";
      bool = "true";
      emptystring = ''""'';
      string = ''"fno\"rd"'';
      string = ''"fn\''${o}\"r\\d"'';
      newlinestring = "\"\\n\"";
      path = "/foo";
      null_ = "null";
@@ -752,7 +752,7 @@ runTests {
      functionArgs = "<function, args: {arg?, foo}>";
      list = "[ 3 4 ${function} [ false ] ]";
      emptylist = "[ ]";
      attrs = "{ foo = null; \"foo bar\" = \"baz\"; }";
      attrs = "{ foo = null; \"foo b/ar\" = \"baz\"; }";
      emptyattrs = "{ }";
      drv = "<derivation ${deriv.name}>";
    };
@@ -799,8 +799,8 @@ runTests {
      newlinestring = "\n";
      multilinestring = ''
        hello
        there
        test
        ''${there}
        te'''st
      '';
      multilinestring' = ''
        hello
@@ -827,8 +827,8 @@ runTests {
      multilinestring = ''
        '''
          hello
          there
          test
          '''''${there}
          te''''st
        ''''';
      multilinestring' = ''
        '''