A Boyer-Moore-Horspool search algorithm. */ If it finds the needle, it returns an offset to haystack from which * the needle was found. Otherwise, it returns haystack_length. */
| 438 | * the needle was found. Otherwise, it returns haystack_length. |
| 439 | */ |
| 440 | size_t String::BMH::search( const unsigned char* haystack, size_t haystackLength, |
| 441 | const unsigned char* needle, const size_t needleLength, |
| 442 | const OccTable& occ ) { |
| 443 | if ( needleLength > haystackLength ) |
| 444 | return haystackLength; |
| 445 | if ( needleLength == 1 ) { |
| 446 | const unsigned char* result = |
| 447 | (const unsigned char*)std::memchr( haystack, *needle, haystackLength ); |
| 448 | return result ? size_t( result - haystack ) : haystackLength; |
| 449 | } |
| 450 | |
| 451 | const size_t needleLengthMinus1 = needleLength - 1; |
| 452 | |
| 453 | const unsigned char lastNeedleChar = needle[needleLengthMinus1]; |
| 454 | |
| 455 | size_t haystackPosition = 0; |
| 456 | while ( haystackPosition <= haystackLength - needleLength ) { |
| 457 | const unsigned char occChar = haystack[haystackPosition + needleLengthMinus1]; |
| 458 | |
| 459 | // The author modified this part. Original algorithm matches needle right-to-left. |
| 460 | // This code calls memcmp() (usually matches left-to-right) after matching the last |
| 461 | // character, thereby incorporating some ideas from |
| 462 | // "Tuning the Boyer-Moore-Horspool String Searching Algorithm" |
| 463 | // by Timo Raita, 1992. |
| 464 | if ( lastNeedleChar == occChar && |
| 465 | std::memcmp( needle, haystack + haystackPosition, needleLengthMinus1 ) == 0 ) { |
| 466 | return haystackPosition; |
| 467 | } |
| 468 | |
| 469 | haystackPosition += occ[occChar]; |
| 470 | } |
| 471 | return haystackLength; |
| 472 | } |
| 473 | |
| 474 | Int64 String::BMH::find( const std::string& haystack, const std::string& needle, |
| 475 | const size_t& haystackOffset, const OccTable& occ ) { |
no outgoing calls
no test coverage detected