1
0
mirror of https://github.com/arduino/Arduino.git synced 2025-01-21 10:52:14 +01:00

Optimize applyFilter() to Avoid Redundant String Operations (#11284)

if 'showingHint' == true, then many potentially expensive String operations are being executed on an empty string. This change wraps these operations in an if block which will only run them when needed.
This commit is contained in:
Adrian Hughes 2021-04-09 13:36:32 +02:00 committed by GitHub
parent f4f98cf25e
commit 3f0699a949
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -101,13 +101,16 @@ public class FilterJTextField extends JTextField {
}
public void applyFilter() {
String filter = showingHint ? "" : getText();
filter = filter.toLowerCase();
// Replace anything but 0-9, a-z, or : with a space
filter = filter.replaceAll("[^\\x30-\\x39^\\x61-\\x7a^\\x3a]", " ");
onFilter(filter.split(" "));
String[] filteredText = new String[0];
if (!showingHint) {
String filter = getText().toLowerCase();
// Replace anything but 0-9, a-z, or : with a space
filter = filter.replaceAll("[^\\x30-\\x39^\\x61-\\x7a^\\x3a]", " ");
filteredText = filter.split(" ");
}
onFilter(filteredText);
}
protected void onFilter(String[] strings) {