1
0
mirror of https://github.com/arduino/Arduino.git synced 2024-11-29 10:24:12 +01:00

Simplify FindReplace.find() logic (part 2)

The snippet:

    boolean wrapNeeded = false;
    if (wrap && nextIndex == -1) {
      // if wrapping, a second chance is ok, start from the end
      wrapNeeded = true;
    }

Can be moved inside the `if (nextIndex == -1)` that follows, this way:

    if (nextIndex == -1) {
      boolean wrapNeeded = false;
      if (wrap) {
        // if wrapping, a second chance is ok, start from the end
        wrapNeeded = true;
      }

      [...CUT...]

      if (wrapNeeded) {
        nextIndex = backwards ? text.lastIndexOf(search) : text.indexOf(search, 0);
      }
    }

but since `wrapNeeded` is used only at the very end of the `if` statement
we can move it forward:

    if (nextIndex == -1) {
      [...CUT...]

      boolean wrapNeeded = false;
      if (wrap) {
        // if wrapping, a second chance is ok, start from the end
        wrapNeeded = true;
      }
      if (wrapNeeded) {
        nextIndex = backwards ? text.lastIndexOf(search) : text.indexOf(search, 0);
      }
    }

and finally simplify it by removing `wrapNeeded` altogether:

    if (nextIndex == -1) {
      [...CUT...]

      if (wrap) {
        nextIndex = backwards ? text.lastIndexOf(search) : text.indexOf(search, 0);
      }
    }
This commit is contained in:
Cristian Maglie 2016-09-20 13:58:36 +02:00
parent 47fcff77d5
commit c5a6a44b55

View File

@ -314,12 +314,6 @@ public class FindReplace extends javax.swing.JFrame {
}
}
boolean wrapNeeded = false;
if (wrap && nextIndex == -1) {
// if wrapping, a second chance is ok, start from the end
wrapNeeded = true;
}
if (nextIndex == -1) {
// Nothing found on this tab: Search other tabs if required
if (searchTabs) {
@ -355,7 +349,7 @@ public class FindReplace extends javax.swing.JFrame {
}
}
if (wrapNeeded) {
if (wrap) {
nextIndex = backwards ? text.lastIndexOf(search) : text.indexOf(search, 0);
}
}