PageRenderTime 38ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/jEdit/tags/jedit-4-0-pre3/gnu/regexp/RETokenWordBoundary.java

#
Java | 85 lines | 44 code | 11 blank | 30 comment | 24 complexity | 438cd8cb20820f549397f3d35ffa3d75 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/RETokenWordBoundary.java
  3. * Copyright (C) 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. /**
  21. * Represents a combination lookahead/lookbehind for POSIX [:alnum:].
  22. */
  23. final class RETokenWordBoundary extends REToken {
  24. private boolean negated;
  25. private int where;
  26. static final int BEGIN = 1;
  27. static final int END = 2;
  28. RETokenWordBoundary(int subIndex, int where, boolean negated) {
  29. super(subIndex);
  30. this.where = where;
  31. this.negated = negated;
  32. }
  33. boolean match(CharIndexed input, REMatch mymatch) {
  34. // Word boundary means input[index-1] was a word character
  35. // and input[index] is not, or input[index] is a word character
  36. // and input[index-1] was not
  37. // In the string "one two three", these positions match:
  38. // |o|n|e| |t|w|o| |t|h|r|e|e|
  39. // ^ ^ ^ ^ ^ ^
  40. boolean after = false; // is current character a letter or digit?
  41. boolean before = false; // is previous character a letter or digit?
  42. char ch;
  43. // TODO: Also check REG_ANCHORINDEX vs. anchor
  44. if (((mymatch.eflags & RE.REG_ANCHORINDEX) != RE.REG_ANCHORINDEX)
  45. || (mymatch.offset + mymatch.index > mymatch.anchor)) {
  46. if ((ch = input.charAt(mymatch.index - 1)) != CharIndexed.OUT_OF_BOUNDS) {
  47. before = Character.isLetterOrDigit(ch) || (ch == '_');
  48. }
  49. }
  50. if ((ch = input.charAt(mymatch.index)) != CharIndexed.OUT_OF_BOUNDS) {
  51. after = Character.isLetterOrDigit(ch) || (ch == '_');
  52. }
  53. // if (before) and (!after), we're at end (\>)
  54. // if (after) and (!before), we're at beginning (\<)
  55. boolean doNext = false;
  56. if ((where & BEGIN) == BEGIN) {
  57. doNext = after && !before;
  58. }
  59. if ((where & END) == END) {
  60. doNext ^= before && !after;
  61. }
  62. if (negated) doNext = !doNext;
  63. return (doNext ? next(input, mymatch) : false);
  64. }
  65. void dump(StringBuffer os) {
  66. if (where == (BEGIN | END)) {
  67. os.append( negated ? "\\B" : "\\b" );
  68. } else if (where == BEGIN) {
  69. os.append("\\<");
  70. } else {
  71. os.append("\\>");
  72. }
  73. }
  74. }