private static void TestRange(RegexContext context, SequenceBuffer <char> buffer) { var backtrackState = new BacktrackState(true, context.CurrentState); int j = 0; while (true) { if (buffer.IsPastEnd(context.Index)) { backtrackState.AddZeroConsumed(); context.Push(backtrackState); context.MoveToNextState(); break; } var(matches, consumed) = MatchStateHere(context.CurrentState, buffer, context.Index); if (!matches || consumed == 0) { backtrackState.AddZeroConsumed(); context.Push(backtrackState); context.MoveToNextState(); break; } backtrackState.AddConsumption(consumed); context.AdvanceIndex(consumed); j++; if (j >= context.CurrentState.Maximum) { context.MoveToNextState(); break; } } }
static void test_generate_ack_bits() { Log("test_generate_ack_bits"); const int Size = 256; var receivedPackets = new SequenceBuffer <TestPacketData>(Size); ushort ack = 0xFFFF; uint ack_bits = 0xFFFFFFFF; GenerateAckBits(receivedPackets, out ack, out ack_bits); IsTrue(ack == 0xFFFF); IsTrue(ack_bits == 0); for (int i = 0; i <= Size; ++i) { receivedPackets.Insert((ushort)i); } GenerateAckBits(receivedPackets, out ack, out ack_bits); IsTrue(ack == Size); IsTrue(ack_bits == 0xFFFFFFFF); receivedPackets.Reset(); ushort[] input_acks = { 1, 5, 9, 11 }; for (int i = 0; i < input_acks.Length; ++i) { receivedPackets.Insert(input_acks[i]); } GenerateAckBits(receivedPackets, out ack, out ack_bits); IsTrue(ack == 11); IsTrue(ack_bits == (1 | (1 << (11 - 9)) | (1 << (11 - 5)) | (1 << (11 - 1)))); }
private static void TestZeroOrOne(RegexContext context, SequenceBuffer <char> buffer) { if (buffer.IsPastEnd(context.Index)) { context.Push(new BacktrackState(false, context.CurrentState, 0)); context.MoveToNextState(); return; } var(matches, consumed) = MatchStateHere(context.CurrentState, buffer, context.Index); context.Push(new BacktrackState(matches && consumed > 0, context.CurrentState, consumed)); context.AdvanceIndex(consumed); context.MoveToNextState(); }
public DeltaBuffer(int size) { buffer = new SequenceBuffer <PriorityCubes>(size); for (int i = 0; i < buffer.size; ++i) { buffer.entries[i].resetId = 0; buffer.entries[i].count = 0; buffer.entries[i].priorityIds = new int[MaxCubes]; buffer.entries[i].cubeIds = new int[MaxCubes]; buffer.entries[i].states = new CubeState[MaxCubes]; } Reset(); }
static void test_sequence_buffer() { Log("test_sequence_buffer"); const int Size = 256; var buffer = new SequenceBuffer <TestPacketData>(Size); for (int i = 0; i < Size; ++i) { TestPacketData entry; entry.sequence = 0; IsTrue(buffer.Exists((ushort)i) == false); IsTrue(buffer.Available((ushort)i) == true); IsTrue(buffer.Get((ushort)i) == -1); } for (int i = 0; i <= Size * 4; ++i) { int index = buffer.Insert((ushort)i); IsTrue(index != -1); IsTrue(buffer.id == i + 1); buffer.entries[index].sequence = (ushort)i; } for (int i = 0; i <= Size; ++i) { int index = buffer.Insert((ushort)i); //note: outside bounds! IsTrue(index == -1); } ushort sequence = Size * 4; for (int i = 0; i < Size; ++i) { int index = buffer.Get(sequence); IsTrue(index >= 0); IsTrue(index < Size); IsTrue(buffer.entries[index].sequence == sequence); sequence--; } buffer.Reset(); IsTrue(buffer.id == 0); for (int i = 0; i < Size; ++i) { IsTrue(buffer.Exists((ushort)i) == false); IsTrue(buffer.Available((ushort)i) == true); IsTrue(buffer.Get((ushort)i) == -1); } }
/// <summary> /// Gets all descendant values below the current root, as well as the current root /// </summary> /// <param name="source"></param> /// <returns></returns> public static IEnumerable <TokenSequence> DescendantsAndSelf(this TokenSequence source) { if (source == null) { throw new ArgumentNullException("source"); } if (!(source is IList <ModelToken>)) { // ensure buffered source = new SequenceBuffer <ModelToken>(source); } return(ModelSubsequencer.DescendantsAndSelfIterator(source)); }
/// <summary> /// Covers the sitation where a stream of sequences may be back to back /// </summary> /// <param name="source"></param> /// <returns></returns> internal static IEnumerable <TokenSequence> SplitValues(this TokenSequence source) { if (source == null) { return(new TokenSequence[0]); } if (!(source is IList <ModelToken>)) { // ensure buffered source = new SequenceBuffer <ModelToken>(source); } return(ModelSubsequencer.SplitValuesIterator(source)); }
/// <summary> /// Gets the items of the root array with indexes satisfying the <paramref name="predicate"/> /// </summary> /// <param name="source"></param> /// <param name="predicate"></param> /// <returns>items of the root array which statisfy the predicate</returns> public static IEnumerable <TokenSequence> ArrayItems(this TokenSequence source, Func <int, bool> predicate) { if (source == null) { throw new ArgumentNullException("source"); } if (!(source is IList <ModelToken>)) { // ensure buffered source = new SequenceBuffer <ModelToken>(source); } return(ModelSubsequencer.ArrayItemsIterator(source, predicate)); }
/// <summary> /// Gets the properties of the root object which satisfies the <paramref name="predicate"/> /// </summary> /// <param name="source"></param> /// <param name="predicate"></param> /// <returns>matching properties for the root object</returns> public static IEnumerable <KeyValuePair <DataName, TokenSequence> > Properties(this TokenSequence source, Func <DataName, bool> predicate) { if (source == null) { throw new ArgumentNullException("source"); } if (!(source is IList <ModelToken>)) { // ensure buffered source = new SequenceBuffer <ModelToken>(source); } return(ModelSubsequencer.PropertiesIterator(source, predicate)); }
/// <summary> /// Attempts to match the given regex pattern on the given input starting at it's current /// location. /// </summary> /// <param name="input"></param> /// <param name="regex"></param> /// <returns></returns> public static PartialResult <string> GetMatch(ISequence <char> input, Regex regex) { Assert.ArgumentNotNull(input, nameof(input)); Assert.ArgumentNotNull(regex, nameof(regex)); var startLocation = input.CurrentLocation; var buffer = new SequenceBuffer <char>(input); var(matches, consumed) = Test(regex.States, buffer); if (matches) { var charArray = buffer.Capture(consumed); return(new PartialResult <string>(new string(charArray), consumed, startLocation)); } return(new PartialResult <string>($"Match failed at position {consumed}", startLocation)); }
private static (bool matches, int length) Test(IReadOnlyList <State> states, SequenceBuffer <char> buffer) { var context = new RegexContext(states); while (context.CurrentState.Type != StateType.EndSentinel) { switch (context.CurrentState.Quantifier) { case Quantifier.ExactlyOne: { var indexBeforeBacktracking = context.Index; var ok = TestExactlyOne(context, buffer); if (ok) { continue; } return(false, indexBeforeBacktracking); } case Quantifier.ZeroOrOne: { TestZeroOrOne(context, buffer); continue; } case Quantifier.ZeroOrMore: { TestZeroOrMore(context, buffer); continue; } case Quantifier.Range: { TestRange(context, buffer); continue; } default: throw new RegexException("Unrecognized quantifier"); } } return(true, context.Index); }
private static bool TestExactlyOne(RegexContext context, SequenceBuffer <char> buffer) { var(matches, consumed) = MatchStateHere(context.CurrentState, buffer, context.Index); if (matches) { context.Push(new BacktrackState(false, context.CurrentState, consumed)); context.AdvanceIndex(consumed); context.MoveToNextState(); return(true); } var couldBacktrack = context.Backtrack(); if (couldBacktrack) { return(true); } return(false); }
private static (bool matches, int length) MatchStateHere(State state, SequenceBuffer <char> context, int i) { if (context.IsPastEnd(i)) { if (state.Type == StateType.EndOfInput) { return(true, 0); } return(false, 0); } if (state.Type == StateType.EndOfInput) { return(false, 0); } if (state.Type == StateType.MatchValue) { var match = state.ValuePredicate?.Invoke(context[i]) ?? false; return(match, match ? 1 : 0); } if (state.Type == StateType.Group) { return(Test(state.Group !, context.CopyFrom(i))); } if (state.Type == StateType.Alternation) { foreach (var substate in state.Alternations !) { var(matches, consumed) = Test(substate, context.CopyFrom(i)); if (matches) { return(true, consumed); } } return(false, 0); } throw new RegexException("Unsupported state type during match"); }
public EntityInfo() { snapshots = new SequenceBuffer <EntitySnapshotInfo>(NetworkConfig.snapshotDeltaCacheSize, () => new EntitySnapshotInfo()); }
/// <summary> /// Covers the sitation where a stream of sequences may be back to back /// </summary> /// <param name="source"></param> /// <returns></returns> internal static IEnumerable<TokenSequence> SplitValues(this TokenSequence source) { if (source == null) { return new TokenSequence[0]; } if (!(source is IList<ModelToken>)) { // ensure buffered source = new SequenceBuffer<ModelToken>(source); } return ModelSubsequencer.SplitValuesIterator(source); }
/// <summary> /// Gets all descendant values below the current root, as well as the current root /// </summary> /// <param name="source"></param> /// <returns></returns> public static IEnumerable<TokenSequence> DescendantsAndSelf(this TokenSequence source) { if (source == null) { throw new ArgumentNullException("source"); } if (!(source is IList<ModelToken>)) { // ensure buffered source = new SequenceBuffer<ModelToken>(source); } return ModelSubsequencer.DescendantsAndSelfIterator(source); }
/// <summary> /// Gets the items of the root array with indexes satisfying the <paramref name="predicate"/> /// </summary> /// <param name="source"></param> /// <param name="predicate"></param> /// <returns>items of the root array which statisfy the predicate</returns> public static IEnumerable<TokenSequence> ArrayItems(this TokenSequence source, Func<int, bool> predicate) { if (source == null) { throw new ArgumentNullException("source"); } if (!(source is IList<ModelToken>)) { // ensure buffered source = new SequenceBuffer<ModelToken>(source); } return ModelSubsequencer.ArrayItemsIterator(source, predicate); }
/// <summary> /// Gets the properties of the root object which satisfies the <paramref name="predicate"/> /// </summary> /// <param name="source"></param> /// <param name="predicate"></param> /// <returns>matching properties for the root object</returns> public static IEnumerable<KeyValuePair<DataName, TokenSequence>> Properties(this TokenSequence source, Func<DataName, bool> predicate) { if (source == null) { throw new ArgumentNullException("source"); } if (!(source is IList<ModelToken>)) { // ensure buffered source = new SequenceBuffer<ModelToken>(source); } return ModelSubsequencer.PropertiesIterator(source, predicate); }