The average length is 4 characters for the strings. I was thinking a binary search might be the fastest starting at position 4. Also I think an inlined templatized function might perform well. This is done in a very tight loop so performance is critical.
The data looks like:
"1234 "
"ABC "
"A1235 "
"A1235kgo"
Note that ‘no match’ is signaled wuth the end iterator:
Note, that
-O3 -march=nativefor g++)Edit since you have shown more code, I now realize you actually want to detect (sub)string length. You could use
etc.
Of course, that assumes you’d want to convert the char[] to std::string for the purpose. In practice, that might be a perfectly valid idea, because of SSO (Small String Optimization) found in nearly all implementations of the C++ standard library.
(see Items 13-16 in Herb Sutter’s More Exceptional C++, or Scott Meyers’ discussion of commercial std::string implementations in Effective STL).