using CanonSharp.Combinator.Abstractions;
using CanonSharp.Combinator.Extensions;
using CanonSharp.Combinator.Parsers;
namespace CanonSharp.Combinator.Text;
///
/// 字符串解析器
///
/// 期望的字符串
/// 字符串比较模式
public class StringParser(string except, StringComparison comparison) : PrimitiveParser
{
protected override IParseResult Run(TState state)
{
TState[] states = state.AsEnumerable().Take(except.Length).ToArray();
string actual = new(states.Select(x => x.Current).ToArray());
return string.Equals(except, actual, comparison)
? ParseResultBuilder.Succeed(actual, states.Length == 0 ? state : states.Last().Next)
: ParseResultBuilder.Fail($"Except '{except}' but found '{actual}.", state);
}
}