Skip to content

Commit

Permalink
Merge pull request #165 from Philipp-Binder/feature/reintroduce-TextC…
Browse files Browse the repository at this point in the history
…ombinator

Feature/reintroduce text combinator
  • Loading branch information
nblumhardt authored Sep 30, 2024
2 parents 0d3af97 + ea59437 commit 92a1909
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
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");
}
}

0 comments on commit 92a1909

Please sign in to comment.