Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import com.sun.org.apache.bcel.internal.generic.IF_ACMPEQ;
- import java.util.ArrayList;
- public class Find {
- private String text;
- public Find(String text) {
- this.text = text;
- }
- public boolean match(String stringPat) {
- ArrayList<Atom> atoms = Atom.getAtoms(stringPat);
- int cntPlus;
- if (stringPat.contains("+")){
- int cnt = 0;
- for (int i = 0; i < stringPat.length(); i++) {
- char curentChar = stringPat.charAt(i);
- if (curentChar=='+')cnt++;
- }
- cntPlus = atoms.size()-cnt;
- }
- else cntPlus = atoms.size();
- if (atoms.isEmpty()) return false;
- boolean matches = false;
- int finalPos = (atoms.get(0).getAtomType().equals(Atom.AtomType.AT_START)? 1 : text.length());
- int initialPos = 0;
- Character currentTextChar = null;
- while (initialPos < finalPos && cntPlus <= text.length() - initialPos) {
- matches = true;
- int currentPos = initialPos;
- for (Atom atom : atoms) {
- currentTextChar = text.charAt(currentPos);
- if (atom.getAtomType().equals(Atom.AtomType.AT_START) || (atom.getAtomType().equals(Atom.AtomType.LIST)) || (atom.getAtomType().equals(Atom.AtomType.CHARACTER))){
- if (atom.matches(currentTextChar)) {
- currentPos++;
- continue;
- }
- matches = false;
- break;
- } else if (atom.getAtomType().equals(Atom.AtomType.AT_END)) {
- if (atom.matches(currentTextChar) && currentPos == text.length()-1) {
- break;
- }
- matches = false;
- break;
- } else if (atom.getAtomType().equals(Atom.AtomType.ALL)) {
- currentPos++;
- continue;
- }
- else if (atom.getAtomType().equals(Atom.AtomType.CLOSURE_AST)){
- currentPos++;
- continue;
- }
- }
- if (matches)break;
- else initialPos++;
- }
- return matches;
- }
- }
- /* else if (atom.getAtomType().equals(Atom.AtomType.CLOSURE_PLUS)){
- ArrayList<Atom> closureAtoms = (ArrayList<Atom>) atoms.clone();
- ArrayList<Atom> = new ArrayList<>();
- for (int i = 0; i < ; i++) {
- }
- }
- */
- /*
- Atom currentAtom = null;
- Character currentTextChar = null;
- int cnt = 0;
- int numRepeat = (expr.get(0).getAtomType().equals(Atom.AtomType.AT_START)) ? 1 : text.length();
- for (int i = 0; i < numRepeat; i++) {
- if (cnt == expr.size()) {
- break;
- }
- currentAtom = expr.get(cnt);
- currentTextChar = text.charAt(i);
- if (currentAtom.getAtomType().equals(Atom.AtomType.AT_START)) {
- currentAtom.getMatchingChars().get(0).equals(currentTextChar);
- }
- if (currentAtom.getAtomType().equals(Atom.AtomType.AT_END)) {
- return checkFirstMetaCharacter(expr, text,currentAtom);
- }
- if (currentAtom.getAtomType().equals(Atom.AtomType.LIST)) {
- currentAtom = searchCurrentCharText(currentAtom, currentTextChar);
- }
- if (currentAtom.getAtomType().equals(Atom.AtomType.ALL)) {
- cnt++;
- } else if (currentTextChar == currentAtom.getMatchingChars().get(0)) {
- cnt++;
- } else {
- cnt = 0;
- if (checkAll(expr, currentAtom)) {
- i--;
- }
- }
- }
- if (cnt == expr.size() || expr.get(expr.size() - 1).getAtomType().equals(Atom.AtomType.AT_END)) {
- if (currentAtom.getAtomType().equals(Atom.AtomType.ALL)) {
- return true;
- }
- return currentTextChar == currentAtom.getMatchingChars().get(0);
- }
- return false;
- }
- private Atom searchCurrentCharText(Atom currentAtom, Character currentCharText) {
- for (int i = 0; i < currentAtom.getMatchingChars().size(); i++) {
- char currentChar = currentAtom.getMatchingChars().get(i);
- if (currentChar == currentCharText) {
- currentAtom = new Atom(Atom.AtomType.CHARACTER, Atom.newArrayListWithChars(currentChar));
- return currentAtom;
- }
- }
- return currentAtom;
- }
- private boolean checkAll(ArrayList<Atom> expr, Atom atom) {
- for (int i = 0; i < expr.size(); i++) {
- Atom currentAtom = expr.get(0);
- if (atom == currentAtom) {
- break;
- }
- if (currentAtom.getAtomType().equals(Atom.AtomType.ALL)) {
- return true;
- }
- }
- return false;
- }
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement