Skip to content

Commit

Permalink
Merge pull request #61554 from dotnet/jamesnk/aspnetcorevirtualcharse…
Browse files Browse the repository at this point in the history
…quence

Add some APIs on AspNetCoreVirtualCharSequence
  • Loading branch information
CyrusNajmabadi authored May 29, 2022
2 parents a92c94f + c09e0a9 commit e62c649
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using Microsoft.CodeAnalysis.EmbeddedLanguages.VirtualChars;
using Microsoft.CodeAnalysis.Text;

namespace Microsoft.CodeAnalysis.ExternalAccess.AspNetCore.EmbeddedLanguages
{
Expand All @@ -16,10 +18,27 @@ internal AspNetCoreVirtualCharSequence(VirtualCharSequence virtualCharSequence)
_virtualCharSequence = virtualCharSequence;
}

/// <inheritdoc cref="VirtualCharSequence.Empty"/>
public static readonly AspNetCoreVirtualCharSequence Empty = new(VirtualCharSequence.Empty);

/// <inheritdoc cref="VirtualCharSequence.Length"/>
public int Length => _virtualCharSequence.Length;

/// <inheritdoc cref="VirtualCharSequence.this"/>
public AspNetCoreVirtualChar this[int index] => new(_virtualCharSequence[index]);

/// <inheritdoc cref="VirtualCharSequence.GetSubSequence"/>
public AspNetCoreVirtualCharSequence GetSubSequence(TextSpan span) => new(_virtualCharSequence.GetSubSequence(span));

/// <inheritdoc cref="VirtualCharSequence.Find"/>
public AspNetCoreVirtualChar? Find(int position) => (_virtualCharSequence.Find(position) is VirtualChar c) ? new(c) : null;

/// <inheritdoc cref="VirtualCharSequence.CreateString"/>
public string CreateString() => _virtualCharSequence.CreateString();

/// <inheritdoc cref="VirtualCharSequence.FromBounds"/>
public static AspNetCoreVirtualCharSequence FromBounds(
AspNetCoreVirtualCharSequence chars1, AspNetCoreVirtualCharSequence chars2) =>
new(VirtualCharSequence.FromBounds(chars1._virtualCharSequence, chars2._virtualCharSequence));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,16 @@ private VirtualCharSequence(Chunk sequence, TextSpan span)
/// </summary>
public VirtualChar this[int index] => _leafCharacters[_span.Start + index];

/// <summary>
/// Gets a value indicating whether the <see cref="VirtualCharSequence"/> was declared but not initialized.
/// </summary>
public bool IsDefault => _leafCharacters == null;
public bool IsEmpty => Length == 0;
public bool IsDefaultOrEmpty => IsDefault || IsEmpty;

/// <summary>
/// Retreives a sub-sequence from this <see cref="VirtualCharSequence"/>.
/// </summary>
public VirtualCharSequence GetSubSequence(TextSpan span)
=> new(_leafCharacters, new TextSpan(_span.Start + span.Start, span.Length));

Expand Down Expand Up @@ -172,6 +178,9 @@ public VirtualCharSequence SkipWhile(Func<VirtualChar, bool> predicate)
return this.GetSubSequence(TextSpan.FromBounds(start, this.Length));
}

/// <summary>
/// Create a <see cref="string"/> from the <see cref="VirtualCharSequence"/>.
/// </summary>
public string CreateString()
{
using var _ = PooledStringBuilder.GetInstance(out var builder);
Expand Down

0 comments on commit e62c649

Please sign in to comment.