Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit be2c17d2 authored by Isaac_Chen's avatar Isaac_Chen
Browse files

Bug fix: 769. The main reason is that the complexity of countOccurrences is...

Bug fix: 769. The main reason is that the complexity of countOccurrences is O(mn^2), which is too large. So that the main thread may be frozen for a long time. We use a O(m+n) implementation
parent 8cdf4263
Loading
Loading
Loading
Loading
+8 −11
Original line number Diff line number Diff line
@@ -17,6 +17,9 @@ import androidx.appcompat.widget.SearchView;

import com.google.android.material.floatingactionbutton.FloatingActionButton;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import it.niedermann.owncloud.notes.R;

public abstract class SearchableBaseNoteFragment extends BaseNoteFragment {
@@ -213,18 +216,12 @@ public abstract class SearchableBaseNoteFragment extends BaseNoteFragment {
    }

    private static int countOccurrences(String haystack, String needle) {
        if (haystack == null || haystack.isEmpty() || needle == null || needle.isEmpty()) {
            return 0;
        }
        int lastIndex = 0;
        int count = 0;
        Matcher m = Pattern.compile(needle, Pattern.CASE_INSENSITIVE | Pattern.LITERAL)
                .matcher(haystack);

        while (lastIndex != -1) {
            lastIndex = haystack.toLowerCase().indexOf(needle.toLowerCase(), lastIndex);
            if (lastIndex != -1) {
        int count = 0;
        while (m.find()) {
            count++;
                lastIndex += needle.length();
            }
        }
        return count;
    }