Browse Source

Optimized the generation of C# by avoiding splitting.

Signed-off-by: Dimitar Dobrev <dpldobrev@protonmail.com>
pull/1128/head
Dimitar Dobrev 7 years ago committed by João Matos
parent
commit
34ad79529d
  1. 10
      src/Generator/Generators/CSharp/CSharpSources.cs
  2. 44
      src/Generator/Utils/BlockGenerator.cs
  3. 11
      src/Generator/Utils/TextGenerator.cs

10
src/Generator/Generators/CSharp/CSharpSources.cs

@ -727,7 +727,7 @@ namespace CppSharp.Generators.CSharp
var fieldValuePrinted = field.Expression.CSharpValue(ExpressionPrinter); var fieldValuePrinted = field.Expression.CSharpValue(ExpressionPrinter);
Write($" = {fieldValuePrinted}"); Write($" = {fieldValuePrinted}");
} }
Write(";"); WriteLine(";");
PopBlock(NewLineKind.BeforeNextBlock); PopBlock(NewLineKind.BeforeNextBlock);
} }
@ -757,7 +757,7 @@ namespace CppSharp.Generators.CSharp
{ {
if (isAbstract) if (isAbstract)
{ {
Write(";"); WriteLine(";");
PopBlock(NewLineKind.BeforeNextBlock); PopBlock(NewLineKind.BeforeNextBlock);
return; return;
} }
@ -1044,7 +1044,7 @@ namespace CppSharp.Generators.CSharp
{ {
if (isAbstract) if (isAbstract)
{ {
Write(";"); WriteLine(";");
PopBlock(NewLineKind.BeforeNextBlock); PopBlock(NewLineKind.BeforeNextBlock);
return; return;
} }
@ -1707,7 +1707,7 @@ namespace CppSharp.Generators.CSharp
} }
else else
{ {
Write($"{property.Name}"); Write(property.Name);
if (isSetter) if (isSetter)
Write($" = {marshalsCode}"); Write($" = {marshalsCode}");
} }
@ -2329,7 +2329,7 @@ namespace CppSharp.Generators.CSharp
if (method.IsPure) if (method.IsPure)
{ {
Write(";"); WriteLine(";");
PopBlock(NewLineKind.BeforeNextBlock); PopBlock(NewLineKind.BeforeNextBlock);
return; return;
} }

44
src/Generator/Utils/BlockGenerator.cs

@ -110,16 +110,15 @@ namespace CppSharp
} }
} }
public virtual string Generate() public virtual StringBuilder Generate()
{ {
if (CheckGenerate != null && !CheckGenerate()) if (CheckGenerate != null && !CheckGenerate())
return ""; return new StringBuilder();
if (Blocks.Count == 0) if (Blocks.Count == 0)
return Text.ToString(); return Text.StringBuilder;
var builder = new StringBuilder(); var builder = new StringBuilder();
uint totalIndent = 0;
Block previousBlock = null; Block previousBlock = null;
var blockIndex = 0; var blockIndex = 0;
@ -135,7 +134,7 @@ namespace CppSharp
if (nextBlock != null) if (nextBlock != null)
{ {
var nextText = nextBlock.Generate(); var nextText = nextBlock.Generate();
if (string.IsNullOrEmpty(nextText) && if (nextText.Length == 0 &&
childBlock.NewLineKind == NewLineKind.IfNotEmpty) childBlock.NewLineKind == NewLineKind.IfNotEmpty)
skipBlock = true; skipBlock = true;
} }
@ -143,44 +142,25 @@ namespace CppSharp
if (skipBlock) if (skipBlock)
continue; continue;
if (string.IsNullOrEmpty(childText)) if (childText.Length == 0)
continue; continue;
var lines = childText.SplitAndKeep(Environment.NewLine).ToList();
if (previousBlock != null && if (previousBlock != null &&
previousBlock.NewLineKind == NewLineKind.BeforeNextBlock) previousBlock.NewLineKind == NewLineKind.BeforeNextBlock)
builder.AppendLine(); builder.AppendLine();
if (childBlock.isSubBlock) builder.Append(childText);
totalIndent = 0;
foreach (var line in lines)
{
if (string.IsNullOrEmpty(line))
continue;
if (!string.IsNullOrWhiteSpace(line))
builder.Append(new string(' ', (int)totalIndent));
builder.Append(line);
if (!line.EndsWith(Environment.NewLine, StringComparison.Ordinal))
builder.AppendLine();
}
if (childBlock.NewLineKind == NewLineKind.Always) if (childBlock.NewLineKind == NewLineKind.Always)
builder.AppendLine(); builder.AppendLine();
totalIndent += childBlock.Text.Indent;
previousBlock = childBlock; previousBlock = childBlock;
} }
if (Text.StringBuilder.Length != 0) if (Text.StringBuilder.Length != 0)
builder.Append(Text.StringBuilder); builder.Append(Text.StringBuilder);
return builder.ToString(); return builder;
} }
public bool IsEmpty public bool IsEmpty
@ -271,7 +251,7 @@ namespace CppSharp
public virtual string Generate() public virtual string Generate()
{ {
return RootBlock.Generate(); return RootBlock.Generate().ToString();
} }
#region Block helpers #region Block helpers
@ -284,6 +264,14 @@ namespace CppSharp
public void PushBlock(BlockKind kind = BlockKind.Unknown, object obj = null) public void PushBlock(BlockKind kind = BlockKind.Unknown, object obj = null)
{ {
var block = new Block { Kind = kind, Object = obj }; var block = new Block { Kind = kind, Object = obj };
var array = new uint[ActiveBlock.Text.CurrentIndent.Count];
ActiveBlock.Text.CurrentIndent.CopyTo(array, 0);
foreach (var indent in array.Reverse())
{
block.Text.CurrentIndent.Push(indent);
}
block.Text.IsStartOfLine = ActiveBlock.Text.IsStartOfLine;
block.Text.NeedsNewLine = ActiveBlock.Text.NeedsNewLine;
PushBlock(block); PushBlock(block);
} }

11
src/Generator/Utils/TextGenerator.cs

@ -25,10 +25,10 @@ namespace CppSharp
{ {
public const uint DefaultIndent = 4; public const uint DefaultIndent = 4;
public StringBuilder StringBuilder; public StringBuilder StringBuilder = new StringBuilder();
protected bool IsStartOfLine; public bool IsStartOfLine { get; set; }
protected bool NeedsNewLine; public bool NeedsNewLine { get; set; }
protected readonly Stack<uint> CurrentIndent; public Stack<uint> CurrentIndent { get; } = new Stack<uint>();
public uint Indent public uint Indent
{ {
@ -37,9 +37,6 @@ namespace CppSharp
public TextGenerator() public TextGenerator()
{ {
StringBuilder = new StringBuilder();
IsStartOfLine = false;
CurrentIndent = new Stack<uint>();
} }
public TextGenerator(TextGenerator generator) public TextGenerator(TextGenerator generator)

Loading…
Cancel
Save