private int MatchGreedy(Matcher m, ReaderBuffer buffer, int start, int skip) { // Check for simple case if (skip == 0) { return(MatchPossessive(m, buffer, start, 0)); } // Find all matches if (_matchStart != start) { _matchStart = start; _matches = new BitArray(10); FindMatches(m, buffer, start, 0, 0, 0); } // Find first non-skipped match for (int i = _matches.Count - 1; i >= 0; i--) { if (_matches[i]) { if (skip == 0) { return(i); } skip--; } } return(-1); }
private int MatchReluctant(Matcher m, ReaderBuffer buffer, int start, int skip) { if (_matchStart != start) { _matchStart = start; _matches = new BitArray(10); FindMatches(m, buffer, start, 0, 0, 0); } // Find first non-skipped match for (int i = 0; i < _matches.Count; i++) { if (_matches[i]) { if (skip == 0) { return(i); } skip--; } } return(-1); }
public override int Match(Matcher m, ReaderBuffer buffer, int start, int skip) { if (skip == 0) { _matchStart = -1; _matches = null; } switch (_type) { case RepeatType.GREEDY: return(MatchGreedy(m, buffer, start, skip)); case RepeatType.RELUCTANT: return(MatchReluctant(m, buffer, start, skip)); case RepeatType.POSSESSIVE: if (skip == 0) { return(MatchPossessive(m, buffer, start, 0)); } break; } return(-1); }
private int MatchPossessive(Matcher m, ReaderBuffer buffer, int start, int count) { int length = 0; int subLength = 1; // Match as many elements as possible while (subLength > 0 && count < _max) { subLength = _elem.Match(m, buffer, start + length, 0); if (subLength >= 0) { count++; length += subLength; } } // Return result if (_min <= count && count <= _max) { return(length); } else { return(-1); } }
public override int Match(Matcher m, ReaderBuffer buffer, int start, int skip) { if (skip != 0) { return(-1); } for (int i = 0; i < _value.Length; i++) { var c = buffer.Peek(start + i); if (c < 0) { m.SetReadEndOfString(); return(-1); } if (m.IsCaseInsensitive()) { c = (int)Char.ToLower((char)c); } if (c != (int)_value[i]) { return(-1); } } return(_value.Length); }
public override int Match(Matcher m, ReaderBuffer buffer, int start, int skip) { int c; if (skip != 0) { return(-1); } c = buffer.Peek(start); if (c < 0) { m.SetReadEndOfString(); return(-1); } if (m.IsCaseInsensitive()) { c = (int)Char.ToLower((char)c); } return(InSet((char)c) ? 1 : -1); }
public override int Match(Matcher m, ReaderBuffer buffer, int start, int skip) { int length = 0; int length1 = -1; int length2 = -1; int skip1 = 0; int skip2 = 0; while (length >= 0 && skip1 + skip2 <= skip) { length1 = _elem1.Match(m, buffer, start, skip1); length2 = _elem2.Match(m, buffer, start, skip2); if (length1 >= length2) { length = length1; skip1++; } else { length = length2; skip2++; } } return(length); }
/// <summary> /// Returns the length of a matching string starting at the /// specified position. The number of matches to skip can also /// be specified, but numbers higher than zero (0) cause a /// failed match for any element that doesn't attempt to /// combine other elements. /// </summary> /// <param name="m">The matcher being used</param> /// <param name="buffer">The input character buffer to match</param> /// <param name="start">The starting position</param> /// <param name="skip">The number of matches to skip</param> /// <returns> /// The length of the longest matching string, or -1 if no match is found /// </returns> /// <exception cref="IOException">If an I/O error occurred</exception> public override int Match( Matcher m, ReaderBuffer buffer, int start, int skip) { int c; if (skip != 0) { return -1; } for (int i = 0; i < this.value.Length; i++) { c = buffer.Peek(start + i); if (c < 0) { m.SetReadEndOfString(); return -1; } if (m.IsCaseInsensitive) { c = (int)char.ToLower((char)c); } if (c != (int)this.value[i]) { return -1; } } return this.value.Length; }
internal Matcher(Element e, ReaderBuffer buffer, bool ignoreCase) { this._element = e; this._buffer = buffer; this._ignoreCase = ignoreCase; this._start = 0; Reset(); }
/// <summary> /// Initializes a new instance of the <see cref="Matcher"/> class, with /// the specified element. /// </summary> /// <param name="e">The base regular expression element</param> /// <param name="buffer">The input character buffer to work with</param> /// <param name="ignoreCase">The character case ignore flag</param> internal Matcher(Element e, ReaderBuffer buffer, bool ignoreCase) { this.element = e; this.buffer = buffer; this.ignoreCase = ignoreCase; this.start = 0; this.Reset(); }
/** * Creates a new matcher with the specified element. * * @param e the base regular expression element * @param buffer the input character buffer to work with * @param ignoreCase the character case ignore flag */ internal Matcher(Element e, ReaderBuffer buffer, bool ignoreCase) { element = e; buffer = buffer; ignoreCase = ignoreCase; start = 0; Reset(); }
private void FindMatches(Matcher m, ReaderBuffer buffer, int start, int length, int count, int attempt) { int subLength; // Check match ending here if (count > _max) { return; } if (_min <= count && attempt == 0) { if (_matches.Length <= length) { _matches.Length = length + 10; } _matches[length] = true; } // Check element match subLength = _elem.Match(m, buffer, start, attempt); if (subLength < 0) { return; } else if (subLength == 0) { if (_min == count + 1) { if (_matches.Length <= length) { _matches.Length = length + 10; } _matches[length] = true; } return; } // Find alternative and subsequent matches FindMatches(m, buffer, start, length, count, attempt + 1); FindMatches(m, buffer, start + subLength, length + subLength, count + 1, 0); }
/// <summary> /// Returns the length of a matching string starting at the /// specified position.The number of matches to skip can also /// be specified, but numbers higher than zero (0) cause a /// failed match for any element that doesn't attempt to /// combine other elements. /// </summary> /// <param name="m">the matcher being used</param> /// <param name="buffer">the input character buffer to match</param> /// <param name="start">The starting position</param> /// <param name="skip">the number of matches to skip</param> /// <returns> /// the length of the matching string, or -1 if no match was found /// </returns> public abstract int Match( Matcher m, ReaderBuffer buffer, int start, int skip);
/** * Returns the length of a matching string starting at the * specified position. The number of matches to skip can also * be specified, but numbers higher than zero (0) cause a * failed match for any element that doesn't attempt to * combine other elements. * * @param m the matcher being used * @param buffer the input character buffer to match * @param start the starting position * @param skip the number of matches to skip * * @return the length of the longest matching string, or * -1 if no match was found * * @throws IOException if an I/O error occurred */ public override int Match(Matcher m, ReaderBuffer buffer, int start, int skip) { int length = 0; int length1 = -1; int length2 = -1; int skip1 = 0; int skip2 = 0; while (length >= 0 && skip1 + skip2 <= skip) { length1 = elem1.Match(m, buffer, start, skip1); length2 = elem2.Match(m, buffer, start, skip2); if (length1 >= length2) { length = length1; skip1++; } else { length = length2; skip2++; } } return length; }
/// <summary> /// Resets the information about the last match. This will /// clear all flags and set the match length to a negative /// value. /// </summary> /// <param name="buffer">The character buffer to work with</param> public void Reset(ReaderBuffer buffer) { this.buffer = buffer; this.Reset(); }