From 34ad79529dcda3f953180af3abbe1b01dd58ce18 Mon Sep 17 00:00:00 2001 From: Dimitar Dobrev Date: Fri, 9 Nov 2018 12:34:49 +0200 Subject: [PATCH] Optimized the generation of C# by avoiding splitting. Signed-off-by: Dimitar Dobrev --- .../Generators/CSharp/CSharpSources.cs | 10 ++--- src/Generator/Utils/BlockGenerator.cs | 44 +++++++------------ src/Generator/Utils/TextGenerator.cs | 11 ++--- 3 files changed, 25 insertions(+), 40 deletions(-) diff --git a/src/Generator/Generators/CSharp/CSharpSources.cs b/src/Generator/Generators/CSharp/CSharpSources.cs index a9212352..49046628 100644 --- a/src/Generator/Generators/CSharp/CSharpSources.cs +++ b/src/Generator/Generators/CSharp/CSharpSources.cs @@ -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 { if (isAbstract) { - Write(";"); + WriteLine(";"); PopBlock(NewLineKind.BeforeNextBlock); return; } @@ -1044,7 +1044,7 @@ namespace CppSharp.Generators.CSharp { if (isAbstract) { - Write(";"); + WriteLine(";"); PopBlock(NewLineKind.BeforeNextBlock); return; } @@ -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 if (method.IsPure) { - Write(";"); + WriteLine(";"); PopBlock(NewLineKind.BeforeNextBlock); return; } diff --git a/src/Generator/Utils/BlockGenerator.cs b/src/Generator/Utils/BlockGenerator.cs index a10be6f5..041c7499 100644 --- a/src/Generator/Utils/BlockGenerator.cs +++ b/src/Generator/Utils/BlockGenerator.cs @@ -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 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 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 public virtual string Generate() { - return RootBlock.Generate(); + return RootBlock.Generate().ToString(); } #region Block helpers @@ -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); } diff --git a/src/Generator/Utils/TextGenerator.cs b/src/Generator/Utils/TextGenerator.cs index fb5c712d..308849a7 100644 --- a/src/Generator/Utils/TextGenerator.cs +++ b/src/Generator/Utils/TextGenerator.cs @@ -25,10 +25,10 @@ namespace CppSharp { public const uint DefaultIndent = 4; - public StringBuilder StringBuilder; - protected bool IsStartOfLine; - protected bool NeedsNewLine; - protected readonly Stack CurrentIndent; + public StringBuilder StringBuilder = new StringBuilder(); + public bool IsStartOfLine { get; set; } + public bool NeedsNewLine { get; set; } + public Stack CurrentIndent { get; } = new Stack(); public uint Indent { @@ -37,9 +37,6 @@ namespace CppSharp public TextGenerator() { - StringBuilder = new StringBuilder(); - IsStartOfLine = false; - CurrentIndent = new Stack(); } public TextGenerator(TextGenerator generator)