Advertisement
Guest User

Untitled

a guest
Mar 20th, 2019
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.94 KB | None | 0 0
  1. import com.sun.org.apache.bcel.internal.generic.IF_ACMPEQ;
  2.  
  3. import java.util.ArrayList;
  4.  
  5.  
  6. public class Find {
  7. private String text;
  8.  
  9. public Find(String text) {
  10. this.text = text;
  11. }
  12.  
  13. public boolean match(String stringPat) {
  14. ArrayList<Atom> atoms = Atom.getAtoms(stringPat);
  15. int cntPlus;
  16. if (stringPat.contains("+")){
  17. int cnt = 0;
  18. for (int i = 0; i < stringPat.length(); i++) {
  19. char curentChar = stringPat.charAt(i);
  20. if (curentChar=='+')cnt++;
  21. }
  22. cntPlus = atoms.size()-cnt;
  23. }
  24. else cntPlus = atoms.size();
  25. if (atoms.isEmpty()) return false;
  26.  
  27. boolean matches = false;
  28. int finalPos = (atoms.get(0).getAtomType().equals(Atom.AtomType.AT_START)? 1 : text.length());
  29. int initialPos = 0;
  30. Character currentTextChar = null;
  31. while (initialPos < finalPos && cntPlus <= text.length() - initialPos) {
  32. matches = true;
  33. int currentPos = initialPos;
  34. for (Atom atom : atoms) {
  35. currentTextChar = text.charAt(currentPos);
  36. if (atom.getAtomType().equals(Atom.AtomType.AT_START) || (atom.getAtomType().equals(Atom.AtomType.LIST)) || (atom.getAtomType().equals(Atom.AtomType.CHARACTER))){
  37. if (atom.matches(currentTextChar)) {
  38. currentPos++;
  39. continue;
  40. }
  41. matches = false;
  42. break;
  43. } else if (atom.getAtomType().equals(Atom.AtomType.AT_END)) {
  44. if (atom.matches(currentTextChar) && currentPos == text.length()-1) {
  45. break;
  46. }
  47. matches = false;
  48. break;
  49. } else if (atom.getAtomType().equals(Atom.AtomType.ALL)) {
  50. currentPos++;
  51. continue;
  52. }
  53. else if (atom.getAtomType().equals(Atom.AtomType.CLOSURE_AST)){
  54. currentPos++;
  55. continue;
  56. }
  57.  
  58. }
  59. if (matches)break;
  60. else initialPos++;
  61. }
  62. return matches;
  63. }
  64. }
  65.  
  66. /* else if (atom.getAtomType().equals(Atom.AtomType.CLOSURE_PLUS)){
  67. ArrayList<Atom> closureAtoms = (ArrayList<Atom>) atoms.clone();
  68. ArrayList<Atom> = new ArrayList<>();
  69. for (int i = 0; i < ; i++) {
  70.  
  71. }
  72. }
  73. */
  74.  
  75.  
  76.  
  77. /*
  78. Atom currentAtom = null;
  79. Character currentTextChar = null;
  80. int cnt = 0;
  81. int numRepeat = (expr.get(0).getAtomType().equals(Atom.AtomType.AT_START)) ? 1 : text.length();
  82.  
  83. for (int i = 0; i < numRepeat; i++) {
  84. if (cnt == expr.size()) {
  85. break;
  86. }
  87. currentAtom = expr.get(cnt);
  88. currentTextChar = text.charAt(i);
  89. if (currentAtom.getAtomType().equals(Atom.AtomType.AT_START)) {
  90. currentAtom.getMatchingChars().get(0).equals(currentTextChar);
  91. }
  92.  
  93. if (currentAtom.getAtomType().equals(Atom.AtomType.AT_END)) {
  94. return checkFirstMetaCharacter(expr, text,currentAtom);
  95. }
  96. if (currentAtom.getAtomType().equals(Atom.AtomType.LIST)) {
  97. currentAtom = searchCurrentCharText(currentAtom, currentTextChar);
  98. }
  99. if (currentAtom.getAtomType().equals(Atom.AtomType.ALL)) {
  100. cnt++;
  101. } else if (currentTextChar == currentAtom.getMatchingChars().get(0)) {
  102. cnt++;
  103. } else {
  104. cnt = 0;
  105. if (checkAll(expr, currentAtom)) {
  106. i--;
  107. }
  108. }
  109. }
  110. if (cnt == expr.size() || expr.get(expr.size() - 1).getAtomType().equals(Atom.AtomType.AT_END)) {
  111. if (currentAtom.getAtomType().equals(Atom.AtomType.ALL)) {
  112. return true;
  113. }
  114. return currentTextChar == currentAtom.getMatchingChars().get(0);
  115. }
  116. return false;
  117. }
  118.  
  119. private Atom searchCurrentCharText(Atom currentAtom, Character currentCharText) {
  120. for (int i = 0; i < currentAtom.getMatchingChars().size(); i++) {
  121. char currentChar = currentAtom.getMatchingChars().get(i);
  122. if (currentChar == currentCharText) {
  123. currentAtom = new Atom(Atom.AtomType.CHARACTER, Atom.newArrayListWithChars(currentChar));
  124. return currentAtom;
  125. }
  126. }
  127. return currentAtom;
  128. }
  129.  
  130. private boolean checkAll(ArrayList<Atom> expr, Atom atom) {
  131. for (int i = 0; i < expr.size(); i++) {
  132. Atom currentAtom = expr.get(0);
  133. if (atom == currentAtom) {
  134. break;
  135. }
  136. if (currentAtom.getAtomType().equals(Atom.AtomType.ALL)) {
  137. return true;
  138. }
  139. }
  140. return false;
  141. }
  142.  
  143. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement