string search across multiple buffers algorithm
I used a simple implementation (online).
/** match granularity is bytes, i.e. compare byte by byte. * @param mem the text to be searched * @param mem_len the text length * @param pattern the word sought * @param pattern_length the pattern length * @param pattern_index the continuing index from the last partial/whole match or 0 * @return the past-the-end index after the text is looked through or the stop index if partial match occurs * @example size_t mem_idx, index = 0; // if matched, current matching range in mem is [mem_idx-(index-old_index), mem_idx-1]. * mem_idx = memmatch("ABC", 3, "FGH", 3, &index); // NotFound: mem_idx = 3; index = 0; * mem_idx = memmatch("EFG", 3, "FGH", 3, &index); // Continue: mem_idx = 3; index = 2; mem[1,2]=pat[0,1] * mem_idx = memmatch("HIJ", 3, "FGH", 3, &index); // Complete: mem_idx = 1; index = 3; mem[0,0]=pat[2,2] */size_t memmatch(const void* mem, size_t mem_len, const void* pattern, size_t pattern_length, size_t* pattern_index) { assert(*pattern_index < pattern_length); size_t idx = 0; // do for loop on `mem` register size_t index = *pattern_index; for (; idx < mem_len;) { if (*((const char*)mem + idx++) == *(const char*)pattern + index) { ++index; if (pattern_length == index) { break; // ++idx; } } else if (index) { index = 0; // reset } } *pattern_index = index; return idx;}#ifdef MEMMATCH_EXAMPLEvoid memmatch_example0() { size_t mem_idx, idx, index = 0; mem_idx = memmatch("ABC", 3, "FGH", 3, (idx = index, &index)); // NotFound std::cout << "mem_idx stops at " << mem_idx << ", pat_idx stops at " << index << " since " << idx << '\n'; mem_idx = memmatch("EFG", 3, "FGH", 3, (idx = index, &index)); // Continue std::cout << "mem_idx stops at " << mem_idx << ", pat_idx stops at " << index << " since " << idx << '\n'; mem_idx = memmatch("HIJ", 3, "FGH", 3, (idx = index, &index)); // Complete std::cout << "mem_idx stops at " << mem_idx << ", pat_idx stops at " << index << " since " << idx << '\n';}#endif