Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;

namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration;

public sealed partial class CodeWriter
{
private static class IndentCache
{
private static readonly ReadOnlyMemory<char> s_tabs = new string('\t', 64).AsMemory();
private static readonly ReadOnlyMemory<char> s_spaces = new string(' ', 128).AsMemory();

public static ReadOnlyMemory<char> GetIndentString(int size, bool useTabs, int tabSize)
{
if (!useTabs)
{
return SliceOrCreate(size, s_spaces);
}

var tabCount = size / tabSize;
var spaceCount = size % tabSize;

if (spaceCount == 0)
{
return SliceOrCreate(tabCount, s_tabs);
}

return string.Create(length: tabCount + spaceCount, (tabCount, spaceCount), static (destination, state) =>
{
var (tabCount, spaceCount) = state;

s_tabs.Span[..tabCount].CopyTo(destination);
s_spaces.Span[..spaceCount].CopyTo(destination[tabCount..]);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this will probably break on existing code somewhere. These are going to throw if tabCount if greater than 64 or spaceCount is greater than 128. That's what the while loops and Math.Min(...) calls handled in my suggested implementation.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice catch! I think commit 3 should address that. Tests added. If you find a bug in it, I'll concede.

}).AsMemory();
}

private static ReadOnlyMemory<char> SliceOrCreate(int length, ReadOnlyMemory<char> chars)
{
return (length <= chars.Length)
? chars[..length]
: new string(chars.Span[0], length).AsMemory();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
using System.IO;
using System.Runtime.CompilerServices;
using System.Text;
using Microsoft.AspNetCore.Razor.PooledObjects;
using Microsoft.CodeAnalysis.Text;

namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration;
Expand Down Expand Up @@ -118,7 +117,7 @@ public int CurrentIndent
if (_indentSize != value)
{
_indentSize = value;
_indentString = ComputeIndent(value, IndentWithTabs, TabSize);
_indentString = IndentCache.GetIndentString(value, IndentWithTabs, TabSize);
}
}
}
Expand Down Expand Up @@ -189,7 +188,7 @@ public CodeWriter Indent(int size)

var indentString = size == _indentSize
? _indentString
: ComputeIndent(size, IndentWithTabs, TabSize);
: IndentCache.GetIndentString(size, IndentWithTabs, TabSize);

AddTextChunk(indentString);

Expand All @@ -200,30 +199,6 @@ public CodeWriter Indent(int size)
return this;
}

private static ReadOnlyMemory<char> ComputeIndent(int size, bool useTabs, int tabSize)
{
if (size == 0)
{
return ReadOnlyMemory<char>.Empty;
}

if (useTabs)
{
var tabCount = size / tabSize;
var spaceCount = size % tabSize;

using var _ = StringBuilderPool.GetPooledObject(out var builder);
builder.SetCapacityIfLarger(tabCount + spaceCount);

builder.Append('\t', tabCount);
builder.Append(' ', spaceCount);

return builder.ToString().AsMemory();
}

return new string(' ', size).AsMemory();
}

public CodeWriter Write(string value)
{
ArgHelper.ThrowIfNull(value);
Expand Down