diff --git a/src/Superpower/Combinators.cs b/src/Superpower/Combinators.cs index 7683a84..7d06abf 100644 --- a/src/Superpower/Combinators.cs +++ b/src/Superpower/Combinators.cs @@ -531,6 +531,22 @@ public static TextParser ManyDelimitedBy(this TextParser parser, T .OptionalOrDefault(Array.Empty()); } + /// + /// Constructs a parser that converts a char[]-parser to a string-parser. + /// + /// The parser. + /// The resulting parser. + public static TextParser Text(this TextParser parser) + => parser.Select(chars => new string(chars)); + + /// + /// Constructs a parser that converts a TextSpan-parser to a string-parser. + /// + /// The parser. + /// The resulting parser. + public static TextParser Text(this TextParser parser) + => parser.Select(textSpan => textSpan.ToStringValue()); + /// /// Construct a parser that fails with error message when fails. /// diff --git a/test/Superpower.Tests/Combinators/TextCombinatorTests.cs b/test/Superpower.Tests/Combinators/TextCombinatorTests.cs new file mode 100644 index 0000000..5568ef0 --- /dev/null +++ b/test/Superpower.Tests/Combinators/TextCombinatorTests.cs @@ -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"); + } +}