Files
text_editor/src/transcript_browser/searching_thread.cpp
Krzosa Karol 37982e0448 Add pdfio
2024-07-08 10:38:35 +02:00

43 lines
1.1 KiB
C++

Array<String> Matches = {};
char Prompt[256];
std::mutex SearchThreadArrayMutex;
int64_t SearchThreadStopSearching;
WORK_FUNCTION(SearchForMatchesWork) {
if (Prompt[0] == 0) return;
int64_t search_thread_stop_searching = SearchThreadStopSearching;
SearchThreadArrayMutex.lock();
Matches.clear();
SearchThreadArrayMutex.unlock();
String buffer = XGetArenaString();
String find = Prompt;
int64_t index = 0;
while (Seek(buffer, find, &index, SeekFlag_IgnoreCase)) {
String found = {buffer.data + index, find.len};
if (search_thread_stop_searching != SearchThreadStopSearching) break;
Matches.bounded_add(found);
buffer = buffer.skip(index + find.len);
}
}
void StartSearchingForMatches() {
SearchThreadStopSearching += 1;
PushWork(&MainWorkQueue, NULL, SearchForMatchesWork);
}
Array<String> LockSearchResults() {
SearchThreadArrayMutex.lock();
Array<String> copy = Matches;
return copy;
}
void UnlockSearchResults() {
SearchThreadArrayMutex.unlock();
}
void InitSearch() {
Matches.reserve(100000);
}