-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Can now use Category for specifying what most of the lambda checkers …
…were used for.
- Loading branch information
Showing
9 changed files
with
370 additions
and
339 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
using System; | ||
using System.Diagnostics.CodeAnalysis; | ||
using System.Traits; | ||
|
||
namespace Stringier.Patterns { | ||
internal sealed class CategoryChecker : Checker { | ||
/// <summary> | ||
/// The category to match. | ||
/// </summary> | ||
[DisallowNull, NotNull] | ||
private readonly Category Category; | ||
|
||
/// <summary> | ||
/// Initializes a new <see cref="CategoryChecker"/>. | ||
/// </summary> | ||
/// <param name="category">The category to match.</param> | ||
public CategoryChecker([DisallowNull] Category category) => Category = category; | ||
|
||
/// <inheritdoc/> | ||
protected internal override void Consume(ReadOnlyMemory<Char> source, ref Int32 location, [AllowNull, MaybeNull] out Exception exception, [AllowNull] IAdd<Capture> trace) => Consume(source.Span, ref location, out exception, trace); | ||
|
||
/// <inheritdoc/> | ||
protected internal override unsafe void Consume([DisallowNull] Char* source, Int32 length, ref Int32 location, [AllowNull, MaybeNull] out Exception exception, [AllowNull] IAdd<Capture> trace) => Consume(new ReadOnlySpan<Char>(source, length), ref location, out exception, trace); | ||
|
||
/// <inheritdoc/> | ||
protected internal override Boolean IsConsumeHeader(ReadOnlySpan<Char> source, Int32 location) => Category.Contains(source[location]); | ||
|
||
/// <inheritdoc/> | ||
protected internal override Boolean IsNeglectHeader(ReadOnlySpan<Char> source, Int32 location) => !Category.Contains(source[location]); | ||
|
||
/// <inheritdoc/> | ||
protected internal override void Neglect(ReadOnlyMemory<Char> source, ref Int32 location, [AllowNull, MaybeNull] out Exception exception, [AllowNull] IAdd<Capture> trace) => Neglect(source.Span, ref location, out exception, trace); | ||
|
||
/// <inheritdoc/> | ||
protected internal override unsafe void Neglect([DisallowNull] Char* source, Int32 length, ref Int32 location, [AllowNull, MaybeNull] out Exception exception, [AllowNull] IAdd<Capture> trace) => Neglect(new ReadOnlySpan<Char>(source, length), ref location, out exception, trace); | ||
|
||
private void Consume(ReadOnlySpan<Char> source, ref Int32 location, [AllowNull, MaybeNull] out Exception exception, [AllowNull] IAdd<Capture> trace) { | ||
if (location == source.Length) { | ||
exception = AtEnd; | ||
trace?.Add(exception, location); | ||
} else if (Category.Contains(source[location])) { | ||
trace?.Add(source[location], location); | ||
location++; | ||
exception = null; | ||
} else { | ||
exception = NoMatch; | ||
trace?.Add(exception, location); | ||
} | ||
} | ||
|
||
private void Neglect(ReadOnlySpan<Char> source, ref Int32 location, [AllowNull, MaybeNull] out Exception exception, [AllowNull] IAdd<Capture> trace) { | ||
if (location == source.Length) { | ||
exception = AtEnd; | ||
trace?.Add(exception, location); | ||
} else if (!Category.Contains(source[location])) { | ||
trace?.Add(source[location], location); | ||
location++; | ||
exception = null; | ||
} else { | ||
exception = NoMatch; | ||
trace?.Add(exception, location); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.