/** * 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; }
/** * 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 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 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; }
/** * 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 c; if (skip != 0) { return -1; } for (int i = 0; i < 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) value[i]) { return -1; } } return value.Length; }
/** * 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 matching string, or * -1 if no match was found * * @throws IOException if an I/O error occurred */ public abstract int Match(Matcher m, ReaderBuffer buffer, int start, int skip);
/** * Returns the length of the shortest possible matching string * starting at the specified position. The number of matches to * skip can also be specified. * * @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 shortest matching string, or * -1 if no match was found * * @throws IOException if an I/O error occurred */ private int MatchReluctant(Matcher m, ReaderBuffer buffer, int start, int skip) { // 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 = 0; i < matches.Count; i++) { if (matches[i]) { if (skip == 0) { return i; } skip--; } } return -1; }
/** * Returns the length of the maximum number of elements matching * the string starting at the specified position. This method * allows no backtracking, i.e. no skips.. * * @param m the matcher being used * @param buffer the input character buffer to match * @param start the starting position * @param count the start count, normally zero (0) * * @return the length of the longest matching string, or * -1 if no match was found * * @throws IOException if an I/O error occurred */ 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; } }
/** * Returns the length of the longest possible matching string * starting at the specified position. The number of matches * to skip can also be specified. * * @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 */ 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; }
/** * Finds all matches and adds the lengths to the matches set. * * @param m the matcher being used * @param buffer the input character buffer to match * @param start the starting position * @param length the match length at the start position * @param count the number of sub-elements matched * @param attempt the number of match attempts here * * @throws IOException if an I/O error occurred */ 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); }
/** * Returns the length of a matching string starting at the * specified position. The number of matches to skip can also be * specified. * * @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 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) { 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; }
/** * 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 length1 = -1; int length2 = 0; int skip1 = 0; int skip2 = 0; while (skip >= 0) { length1 = elem1.Match(m, buffer, start, skip1); if (length1 < 0) { return -1; } length2 = elem2.Match(m, buffer, start + length1, skip2); if (length2 < 0) { skip1++; skip2 = 0; } else { skip2++; skip--; } } return length1 + length2; }
/** * Checks if the start of the input stream matches this * regular expression. * * @param buffer the input buffer to check * * @return the longest match found, or * zero (0) if no match was found * * @throws IOException if an I/O error occurred */ public override int Match(ReaderBuffer buffer) { if (matcher == null) { matcher = regExp.Matcher(buffer); } else { matcher.Reset(buffer); } return matcher.MatchFromBeginning() ? matcher.Length() : 0; }
/// <summary> /// Finds all matches and adds the lengths to the matches set. /// </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="length">The match length at the start position</param> /// <param name="count">The number of sub-elements matched</param> /// <param name="attempt">The number of match attempts here</param> /// <exception cref="IOException">If an I/O error occurred</exception> private void FindMatches( Matcher m, ReaderBuffer buffer, int start, int length, int count, int attempt) { int subLength; // Check match ending here if (count > this.max) { return; } if (this.min <= count && attempt == 0) { if (this.matches.Length <= length) { this.matches.Length = length + 10; } this.matches[length] = true; } // Check element match subLength = this.elem.Match(m, buffer, start, attempt); if (subLength < 0) { return; } else if (subLength == 0) { if (this.min == count + 1) { if (this.matches.Length <= length) { this.matches.Length = length + 10; } this.matches[length] = true; } return; } // Find alternative and subsequent matches this.FindMatches( m, buffer, start, length, count, attempt + 1); this.FindMatches( m, buffer, start + subLength, length + subLength, count + 1, 0); }