Skip to content

Instantly share code, notes, and snippets.

@jweinst1
Created April 3, 2025 21:49
Show Gist options
  • Save jweinst1/95e5bd1c002251c1b1bbe130ffe0144a to your computer and use it in GitHub Desktop.
Save jweinst1/95e5bd1c002251c1b1bbe130ffe0144a to your computer and use it in GitHub Desktop.
events search in C++
#include <string>
#include <filesystem>
#include <vector>
#include <cstdio>
#include <cstdlib>
struct EventPos {
size_t ind = 0;
size_t size = 0;
void clear() {
ind = 0;
size = 0;
}
};
static void generateEvents(const std::string& data, char lineSep, std::vector<EventPos>& events) {
bool currentMatch = false;
EventPos cur;
for (size_t i = 0; i < data.size(); ++i)
{
if (currentMatch) {
if (data[i] == lineSep) {
events.push_back(cur);
cur.clear();
currentMatch = false;
} else {
cur.size++;
}
} else {
if (data[i] != lineSep) {
cur.ind = i;
currentMatch = true;
}
}
}
}
int main(int argc, char const *argv[])
{
const std::string foo = "hello \n sir \n";
std::vector<EventPos> pairs;
generateEvents(foo, '\n', pairs);
std::printf("%zu\n", pairs.size());
const auto found = foo.find("hello");
const auto found2 = foo.find("hello", found + 5);
std::printf("%zu %zu\n", found, found2);
std::printf("%s\n", found2 == foo.npos ? "not found" : "found");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment