PageRenderTime 73ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/jEdit/tags/jedit-4-0-pre5/gnu/regexp/RETokenPOSIX.java

#
Java | 125 lines | 92 code | 11 blank | 22 comment | 23 complexity | 80ac7b5f177815440b2671d8fd2986d8 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/RETokenPOSIX.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. final class RETokenPOSIX extends REToken {
  21. int type;
  22. boolean insens;
  23. boolean negated;
  24. static final int ALNUM = 0;
  25. static final int ALPHA = 1;
  26. static final int BLANK = 2;
  27. static final int CNTRL = 3;
  28. static final int DIGIT = 4;
  29. static final int GRAPH = 5;
  30. static final int LOWER = 6;
  31. static final int PRINT = 7;
  32. static final int PUNCT = 8;
  33. static final int SPACE = 9;
  34. static final int UPPER = 10;
  35. static final int XDIGIT = 11;
  36. // Array indices correspond to constants defined above.
  37. static final String[] s_nameTable = {
  38. "alnum", "alpha", "blank", "cntrl", "digit", "graph", "lower",
  39. "print", "punct", "space", "upper", "xdigit"
  40. };
  41. // The RE constructor uses this to look up the constant for a string
  42. static int intValue(String key) {
  43. for (int i = 0; i < s_nameTable.length; i++) {
  44. if (s_nameTable[i].equals(key)) return i;
  45. }
  46. return -1;
  47. }
  48. RETokenPOSIX(int subIndex, int type, boolean insens, boolean negated) {
  49. super(subIndex);
  50. this.type = type;
  51. this.insens = insens;
  52. this.negated = negated;
  53. }
  54. int getMinimumLength() {
  55. return 1;
  56. }
  57. boolean match(CharIndexed input, REMatch mymatch) {
  58. char ch = input.charAt(mymatch.index);
  59. if (ch == CharIndexed.OUT_OF_BOUNDS)
  60. return false;
  61. boolean retval = false;
  62. switch (type) {
  63. case ALNUM:
  64. // Note that there is some debate over whether '_' should be included
  65. retval = Character.isLetterOrDigit(ch) || (ch == '_');
  66. break;
  67. case ALPHA:
  68. retval = Character.isLetter(ch);
  69. break;
  70. case BLANK:
  71. retval = ((ch == ' ') || (ch == '\t'));
  72. break;
  73. case CNTRL:
  74. retval = Character.isISOControl(ch);
  75. break;
  76. case DIGIT:
  77. retval = Character.isDigit(ch);
  78. break;
  79. case GRAPH:
  80. retval = (!(Character.isWhitespace(ch) || Character.isISOControl(ch)));
  81. break;
  82. case LOWER:
  83. retval = ((insens && Character.isLetter(ch)) || Character.isLowerCase(ch));
  84. break;
  85. case PRINT:
  86. retval = (!(Character.isWhitespace(ch) || Character.isISOControl(ch)))
  87. || (ch == ' ');
  88. break;
  89. case PUNCT:
  90. // This feels sloppy, especially for non-U.S. locales.
  91. retval = ("`~!@#$%^&*()-_=+[]{}\\|;:'\"/?,.<>".indexOf(ch)!=-1);
  92. break;
  93. case SPACE:
  94. retval = Character.isWhitespace(ch);
  95. break;
  96. case UPPER:
  97. retval = ((insens && Character.isLetter(ch)) || Character.isUpperCase(ch));
  98. break;
  99. case XDIGIT:
  100. retval = (Character.isDigit(ch) || ("abcdefABCDEF".indexOf(ch)!=-1));
  101. break;
  102. }
  103. if (negated) retval = !retval;
  104. if (retval) {
  105. ++mymatch.index;
  106. return next(input, mymatch);
  107. }
  108. else return false;
  109. }
  110. void dump(StringBuffer os) {
  111. if (negated) os.append('^');
  112. os.append("[:" + s_nameTable[type] + ":]");
  113. }
  114. }