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

44
src/Generator/Utils/BlockGenerator.cs

@ -110,16 +110,15 @@ namespace CppSharp @@ -110,16 +110,15 @@ namespace CppSharp
}
}
public virtual string Generate()
public virtual StringBuilder Generate()
{
if (CheckGenerate != null && !CheckGenerate())
return "";
return new StringBuilder();
if (Blocks.Count == 0)
return Text.ToString();
return Text.StringBuilder;
var builder = new StringBuilder();
uint totalIndent = 0;
Block previousBlock = null;
var blockIndex = 0;
@ -135,7 +134,7 @@ namespace CppSharp @@ -135,7 +134,7 @@ namespace CppSharp
if (nextBlock != null)
{
var nextText = nextBlock.Generate();
if (string.IsNullOrEmpty(nextText) &&
if (nextText.Length == 0 &&
childBlock.NewLineKind == NewLineKind.IfNotEmpty)
skipBlock = true;
}
@ -143,44 +142,25 @@ namespace CppSharp @@ -143,44 +142,25 @@ namespace CppSharp
if (skipBlock)
continue;
if (string.IsNullOrEmpty(childText))
if (childText.Length == 0)
continue;
var lines = childText.SplitAndKeep(Environment.NewLine).ToList();
if (previousBlock != null &&
previousBlock.NewLineKind == NewLineKind.BeforeNextBlock)
builder.AppendLine();
if (childBlock.isSubBlock)
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();
}
builder.Append(childText);
if (childBlock.NewLineKind == NewLineKind.Always)
builder.AppendLine();
totalIndent += childBlock.Text.Indent;
previousBlock = childBlock;
}
if (Text.StringBuilder.Length != 0)
builder.Append(Text.StringBuilder);
return builder.ToString();
return builder;
}
public bool IsEmpty
@ -271,7 +251,7 @@ namespace CppSharp @@ -271,7 +251,7 @@ namespace CppSharp
public virtual string Generate()
{
return RootBlock.Generate();
return RootBlock.Generate().ToString();
}
#region Block helpers
@ -284,6 +264,14 @@ namespace CppSharp @@ -284,6 +264,14 @@ namespace CppSharp
public void PushBlock(BlockKind kind = BlockKind.Unknown, object obj = null)
{
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);
}

11
src/Generator/Utils/TextGenerator.cs

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

Loading…
Cancel
Save