Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
16 changes: 16 additions & 0 deletions src/Superpower/Combinators.cs
Original file line number Diff line number Diff line change
Expand Up @@ -531,6 +531,22 @@ public static TextParser<T[]> ManyDelimitedBy<T, U>(this TextParser<T> parser, T
.OptionalOrDefault(Array.Empty<T>());
}

/// <summary>
/// Constructs a parser that converts a char[]-parser to a string-parser.
/// </summary>
/// <param name="parser">The parser.</param>
/// <returns>The resulting parser.</returns>
public static TextParser<string> Text(this TextParser<char[]> parser)
=> parser.Select(chars => new string(chars));

/// <summary>
/// Constructs a parser that converts a TextSpan-parser to a string-parser.
/// </summary>
/// <param name="parser">The parser.</param>
/// <returns>The resulting parser.</returns>
public static TextParser<string> Text(this TextParser<TextSpan> parser)
=> parser.Select(textSpan => textSpan.ToStringValue());

/// <summary>
/// Construct a parser that fails with error message <paramref name="errorMessage"/> when <paramref name="parser"/> fails.
/// </summary>
Expand Down
20 changes: 20 additions & 0 deletions test/Superpower.Tests/Combinators/TextCombinatorTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using Superpower.Parsers;
using Superpower.Tests.Support;
using Xunit;

namespace Superpower.Tests.Combinators;

public class TextCombinatorTests
{
[Fact]
public void TextSucceedsWithAnyCharArrayInput()
{
AssertParser.SucceedsWith(Character.AnyChar.Many().Text(), "ab", "ab");
}

[Fact]
public void TextSucceedsWithTextSpanInput()
{
AssertParser.SucceedsWith(Span.Length(2).Text(), "ab", "ab");
}
}