public void TestExceptLeftClip() { var r = new CharSet(); r.AddRange('a', 'f'); var r2 = new CharSet(); r2.AddRange('c', 'f'); r = r.Except(r2); Assert.IsTrue(r.ContainsChar('b')); Assert.IsFalse(r.ContainsChar('c')); }
public void UnionWith(CharSet charSet) { foreach (var charRange in charSet.ranges) { if (!ranges.Contains(charRange)) { // Sanity check #if DEBUG if (ranges.Any( f => f.From == charRange.From || f.To == charRange.To)) throw new Exception("Do not want"); #endif ranges.Add(charRange); } } }
private static NFA Accept(CharSet acceptCharacters) { // Generate a NFA with a simple path with one state transitioning into an accept state. var nfa = new NFA(); var state = new NFA.State(); nfa.States.Add(state); var acceptState = new NFA.State { AcceptState = true }; nfa.States.Add(acceptState); nfa.Transitions.Add(new Transition<NFA.State>(state, acceptState, acceptCharacters)); nfa.StartState = state; return nfa; }
public void TestDistinguishRangesWithSingleChar() { var r1 = new CharSet(); r1.AddRange('a', 'k'); var r2 = new CharSet(); r2.Add('a'); r2.Add('k'); r1.DistinguishRanges(r2); r2.DistinguishRanges(r1); r1.DistinguishRanges(r2); r2.DistinguishRanges(r1); Assert.IsFalse(r1.DistinguishRanges(r2)); Assert.IsFalse(r2.DistinguishRanges(r1)); }
public bool DistinguishRanges(CharSet other) { bool changes = false; ISet<CharRange> newRanges = new HashSet<CharRange>(); // For each of the ranges that this set contains, if there is another range that intersects // create new ranges so that there is no intersecting ranges for (int i = 0; i < ranges.Count; ++i ) { var r = ranges[i]; foreach (var r2 in other.ranges) { // If from is inside the range (not on borders), split the range if (r2.From > r.From && r2.From <= r.To) { var oldTo = r.To; r.To = (char) (r2.From - 1); var newRange = new CharRange { From = r2.From, To = oldTo }; newRanges.Add(newRange); changes = true; } // Same thing goes for to if (r2.To >= r.From && r2.To < r.To) { var oldTo = r.To; r.To = r2.To; var newRange = new CharRange { From = (char) (r2.To + 1), To = oldTo }; newRanges.Add(newRange); changes = true; } } newRanges.Add(r); } ranges = newRanges.ToList(); return changes; }
public void TestDistinguishRanges() { var r1 = new CharSet(); r1.AddRange('a', 'k'); var r2 = new CharSet(); r2.AddRange('g', 'z'); r1.DistinguishRanges(r2); r2.DistinguishRanges(r1); Assert.AreEqual(2, r1.Ranges.Count()); Assert.AreEqual(2, r2.Ranges.Count()); r1.DistinguishRanges(r2); r2.DistinguishRanges(r1); Assert.AreEqual(2, r1.Ranges.Count()); Assert.AreEqual(2, r2.Ranges.Count()); }
public CharacterClassState() { CharsSet = new CharSet(); }
private CharSet SingleChar(char c) { var cs = new CharSet(); cs.Add(c); return cs; }
private static CharSet CharRange(char start, char end) { var charRange = new CharSet(); charRange.AddRange(start, end); return charRange; }
public CharSet Union(CharSet charRange) { var c = new CharSet(); foreach (var range in ranges) { c.AddRange(range.From, range.To); } foreach (var range in charRange.ranges) { c.AddRange(range.From, range.To); } return c; }
public CharSet Except(CharSet except) { var cs = new CharSet(); foreach (var range in ranges) { foreach (var clippedRange in ClipRange(range, except.ranges)) { cs.AddRange(clippedRange.From, clippedRange.To); } } return cs; }