PageRenderTime 41ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/jEdit/tags/jedit-4-2-pre14/gnu/regexp/REMatchEnumeration.java

#
Java | 116 lines | 49 code | 10 blank | 57 comment | 7 complexity | 3510891e5c05079f03bf1efcc0f863d0 MD5 | raw file
Possible License(s): BSD-3-Clause, AGPL-1.0, Apache-2.0, LGPL-2.0, LGPL-3.0, GPL-2.0, CC-BY-SA-3.0, LGPL-2.1, GPL-3.0, MPL-2.0-no-copyleft-exception, IPL-1.0
  1. /*
  2. * gnu/regexp/REMatchEnumeration.java
  3. * Copyright (C) 1998-2001 Wes Biggs
  4. *
  5. * This library is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU Lesser General Public License as published
  7. * by the Free Software Foundation; either version 2.1 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18. */
  19. package gnu.regexp;
  20. import java.io.Serializable;
  21. import java.util.Enumeration;
  22. import java.util.NoSuchElementException;
  23. /**
  24. * An REMatchEnumeration enumerates regular expression matches over a
  25. * given input text. You obtain a reference to an enumeration using
  26. * the <code>getMatchEnumeration()</code> methods on an instance of
  27. * RE.
  28. *
  29. * <P>
  30. *
  31. * REMatchEnumeration does lazy computation; that is, it will not
  32. * search for a match until it needs to. If you'd rather just get all
  33. * the matches at once in a big array, use the
  34. * <code>getAllMatches()</code> methods on RE. However, using an
  35. * enumeration can help speed performance when the entire text does
  36. * not need to be searched immediately.
  37. *
  38. * <P>
  39. *
  40. * The enumerated type is especially useful when searching on a Reader
  41. * or InputStream, because the InputStream read position cannot be
  42. * guaranteed after calling <code>getMatch()</code> (see the
  43. * description of that method for an explanation of why). Enumeration
  44. * also saves a lot of overhead required when calling
  45. * <code>getMatch()</code> multiple times.
  46. *
  47. * @author <A HREF="mailto:wes@cacas.org">Wes Biggs</A>
  48. */
  49. public class REMatchEnumeration implements Enumeration, Serializable {
  50. private static final int YES = 1;
  51. private static final int MAYBE = 0;
  52. private static final int NO = -1;
  53. private int more;
  54. private REMatch match;
  55. private RE expr;
  56. private CharIndexed input;
  57. private int eflags;
  58. private int index;
  59. // Package scope constructor is used by RE.getMatchEnumeration()
  60. REMatchEnumeration(RE expr, CharIndexed input, int index, int eflags) {
  61. more = MAYBE;
  62. this.expr = expr;
  63. this.input = input;
  64. this.index = index;
  65. this.eflags = eflags;
  66. }
  67. /** Returns true if there are more matches in the input text. */
  68. public boolean hasMoreElements() {
  69. return hasMoreMatches(null);
  70. }
  71. /** Returns true if there are more matches in the input text. */
  72. public boolean hasMoreMatches() {
  73. return hasMoreMatches(null);
  74. }
  75. /** Returns true if there are more matches in the input text.
  76. * Saves the text leading up to the match (or to the end of the input)
  77. * in the specified buffer.
  78. */
  79. public boolean hasMoreMatches(StringBuffer buffer) {
  80. if (more == MAYBE) {
  81. match = expr.getMatchImpl(input,index,eflags,buffer);
  82. if (match != null) {
  83. input.move((match.end[0] > 0) ? match.end[0] : 1);
  84. index = (match.end[0] > 0) ? match.end[0] + match.offset : index + 1;
  85. more = YES;
  86. } else more = NO;
  87. }
  88. return (more == YES);
  89. }
  90. /** Returns the next match in the input text. */
  91. public Object nextElement() throws NoSuchElementException {
  92. return nextMatch();
  93. }
  94. /**
  95. * Returns the next match in the input text. This method is provided
  96. * for convenience to avoid having to explicitly cast the return value
  97. * to class REMatch.
  98. */
  99. public REMatch nextMatch() throws NoSuchElementException {
  100. if (hasMoreElements()) {
  101. more = (input.isValid()) ? MAYBE : NO;
  102. return match;
  103. }
  104. throw new NoSuchElementException();
  105. }
  106. }