Skip to content

Commit

Permalink
Fix printing of strings inside functions
Browse files Browse the repository at this point in the history
Previously we normalised all whitespace in printed out functions,
which would effect whitespace inside strings.
  • Loading branch information
ChrisJefferson committed May 4, 2021
1 parent d8fdd19 commit 11df702
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
14 changes: 11 additions & 3 deletions lib/function.gi
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,21 @@ end);

InstallMethod(String, "for a function, with whitespace reduced", [IsFunction and IsInternalRep],
function(fun)
local s, stream;
local s, stream,lines, pos, i;
s := "";
stream := OutputTextString(s, true);
PrintTo(stream, fun);
CloseStream(stream);
NormalizeWhitespace(s);
return MakeImmutable(s);
lines := SplitString(s, "\n");
for i in [1..Length(lines)] do
Chomp(lines[i]);
pos := 1;
while pos <= Length(lines[i]) and lines[i][pos] = ' ' do
pos := pos + 1;
od;
lines[i] := lines[i]{[pos..Length(lines[i])]};
od;
return MakeImmutable(JoinStringsWithSeparator(lines, " "));
end);

BIND_GLOBAL( "VIEW_STRING_OPERATION",
Expand Down
25 changes: 25 additions & 0 deletions tst/testinstall/function.tst
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,31 @@ gap> Print({x,y} -> x + y, "\n");
function ( x, y )
return x + y;
end
gap> String({x,y} -> x + y);
"function ( x, y ) return x + y; end"

# Test nesting
gap> Print(function(x) if x then if x then while x do od; fi; fi; end, "\n");
function ( x )
if x then
if x then
while x do
;
od;
fi;
fi;
return;
end
gap> String(function(x) if x then if x then while x do od; fi; fi; end);
"function ( x ) if x then if x then while x do ; od; fi; fi; return; end"

# Check strings in functions
gap> Print({x} -> "a b","\n");
function ( x )
return "a b";
end
gap> String({x} -> "a b");
"function ( x ) return \"a b\"; end"
gap> f := ({x,y} -> x + y);
function( x, y ) ... end
gap> f(2,3);
Expand Down

0 comments on commit 11df702

Please sign in to comment.