PageRenderTime 54ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/jEdit/tags/jedit-4-2-pre14/org/gjt/sp/jedit/search/RESearchMatcher.java

#
Java | 138 lines | 60 code | 14 blank | 64 comment | 14 complexity | 5cb8f21003318d0cd8b20e878bfd4d8f 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. * RESearchMatcher.java - Regular expression matcher
  3. * :tabSize=8:indentSize=8:noTabs=false:
  4. * :folding=explicit:collapseFolds=1:
  5. *
  6. * Copyright (C) 1999, 2003 Slava Pestov
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; either version 2
  11. * of the License, or any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  21. */
  22. package org.gjt.sp.jedit.search;
  23. //{{{ Imports
  24. import bsh.BshMethod;
  25. import bsh.NameSpace;
  26. import gnu.regexp.*;
  27. import org.gjt.sp.jedit.BeanShell;
  28. import org.gjt.sp.jedit.MiscUtilities;
  29. //}}}
  30. /**
  31. * A regular expression string matcher using {@link gnu.regexp}.
  32. * @author Slava Pestov
  33. * @version $Id: RESearchMatcher.java 4826 2003-07-14 23:00:54Z spestov $
  34. */
  35. public class RESearchMatcher extends SearchMatcher
  36. {
  37. /**
  38. * Perl5 syntax with character classes enabled.
  39. * @since jEdit 3.0pre5
  40. */
  41. public static final RESyntax RE_SYNTAX_JEDIT
  42. = new RESyntax(RESyntax.RE_SYNTAX_PERL5)
  43. .set(RESyntax.RE_CHAR_CLASSES)
  44. .setLineSeparator("\n");
  45. //{{{ RESearchMatcher constructor
  46. /**
  47. * Creates a new regular expression string matcher.
  48. * @since jEdit 4.2pre4
  49. */
  50. public RESearchMatcher(String search, boolean ignoreCase)
  51. throws REException
  52. {
  53. re = new RE(search,(ignoreCase ? RE.REG_ICASE : 0)
  54. | RE.REG_MULTILINE,RE_SYNTAX_JEDIT);
  55. returnValue = new Match();
  56. } //}}}
  57. //{{{ nextMatch() method
  58. /**
  59. * Returns the offset of the first match of the specified text
  60. * within this matcher.
  61. * @param text The text to search in
  62. * @param start True if the start of the segment is the beginning of the
  63. * buffer
  64. * @param end True if the end of the segment is the end of the buffer
  65. * @param firstTime If false and the search string matched at the start
  66. * offset with length zero, automatically find next match
  67. * @param reverse If true, searching will be performed in a backward
  68. * direction.
  69. * @return an array where the first element is the start offset
  70. * of the match, and the second element is the end offset of
  71. * the match
  72. * @since jEdit 4.2pre4
  73. */
  74. public SearchMatcher.Match nextMatch(CharIndexed text, boolean start,
  75. boolean end, boolean firstTime, boolean reverse)
  76. {
  77. int flags = 0;
  78. // unless we are matching from the start of the buffer,
  79. // ^ should not match on the beginning of the substring
  80. if(!start)
  81. flags |= RE.REG_NOTBOL;
  82. // unless we are matching to the end of the buffer,
  83. // $ should not match on the end of the substring
  84. if(!end)
  85. flags |= RE.REG_NOTEOL;
  86. REMatch match = re.getMatch(text,0,flags);
  87. if(match == null)
  88. return null;
  89. returnValue.substitutions = new String[
  90. re.getNumSubs() + 1];
  91. for(int i = 0; i < returnValue.substitutions.length; i++)
  92. {
  93. returnValue.substitutions[i] = match.toString(i);
  94. }
  95. int _start = match.getStartIndex();
  96. int _end = match.getEndIndex();
  97. // some regexps (eg ^ by itself) have a length == 0, so we
  98. // implement this hack. if you don't understand what's going on
  99. // here, then go back to watching MTV
  100. if(!firstTime && _start == 0 && _end == 0)
  101. {
  102. text.move(1);
  103. if(text.charAt(0) == CharIndexed.OUT_OF_BOUNDS)
  104. {
  105. // never mind
  106. return null;
  107. }
  108. match = re.getMatch(text,0,flags | RE.REG_NOTBOL);
  109. if(match == null)
  110. return null;
  111. else
  112. {
  113. _start = match.getStartIndex() + 1;
  114. _end = match.getEndIndex() + 1;
  115. }
  116. }
  117. returnValue.start = _start;
  118. returnValue.end = _end;
  119. return returnValue;
  120. } //}}}
  121. //{{{ Private members
  122. private RE re;
  123. //}}}
  124. }