Objective-C??Boyer-Moore???????
Boyer-Moore????????????????????????????????????????????????????????????????????????????
#include <foundation/foundation.h>
#define ALPHABET_SIZE 256
#endif
????Objective-C???Boyer-Moore?????????????
#import <foundation/foundation.h>
#define ALPHABET_SIZE 256
#if DEBUG#define LOG(x) NSLog(x)#else#define LOG(x) ((void)0)#endif
typedef struct {unsigned char *pattern;unsigned char *text;size_t patternLength;size_t textLength;size_t remaining;unsigned char badChar;unsigned char goodSuffix;unsigned char lastMatch;unsigned char currentMatch;unsigned char maxMatch;unsigned char maxPatternLength;unsigned char maxTextLength;unsigned char maxBadChar;unsigned char maxGoodSuffix;unsigned char maxMaxMatch;} BoyerMooreContext;
static BoyerMooreContext *alcBoyerMoore(void *text, const unsigned char *pattern, size_t patternLength, size_t textLength) {BoyerMooreContext *context = malloc(sizeof(BoyerMooreContext));context->pattern = pattern;context->text = text;context->patternLength = patternLength;context->textLength = textLength;context->remaining = patternLength;context->badChar = 0;context->goodSuffix = 0;context->lastMatch = 0;context->currentMatch = 0;context->maxMatch = 0;context->maxPatternLength = patternLength;context->maxTextLength = textLength;context->maxBadChar = 0;context->maxGoodSuffix = 0;context->maxMaxMatch = 0;return context;}
static void deallocBoyerMoore(BoyerMooreContext *context) {free(context->pattern);free(context->text);free(context);}
static size_t boyermooreSearch(BoyerMooreContext *context) {unsigned char *text = context->text;size_t textLength = context->textLength;size_t patternLength = context->patternLength;size_t remaining = context->remaining;
size_t i = textLength;unsigned char *p = context->pattern;unsigned char *t = text;unsigned char badChar = context->badChar;unsigned char goodSuffix = context->goodSuffix;unsigned char lastMatch = context->lastMatch;unsigned char currentMatch = context->currentMatch;unsigned char maxMatch = context->maxMatch;unsigned char maxPatternLength = context->maxPatternLength;unsigned char maxTextLength = context->maxTextLength;unsigned char maxBadChar = context->maxBadChar;unsigned char maxGoodSuffix = context->maxGoodSuffix;unsigned char maxMaxMatch = context->maxMaxMatch;while (remaining > 0) { if (t[i] == p[0]) { size_t matched = 0; do { matched++; if (matched > maxMatch) break; if (t[i + matched] != p[matched]) break; i += matched; remaining -= matched; if (matched > maxMaxMatch) maxMaxMatch = matched; } while (matched <= patternLength && i + matched < textLength); if (matched > 0) { lastMatch = currentMatch; currentMatch = matched; if (currentMatch > maxMatch) maxMatch = currentMatch; if (currentMatch == patternLength) { LOG("Pattern found at position %ld", i - patternLength); return i - patternLength; } if (currentMatch > maxMaxMatch) maxMaxMatch = currentMatch; } else { if (i > 0) { if (t[i] == badChar || (goodSuffix != 0 && t[i] == goodSuffix)) { i--; remaining = patternLength; lastMatch = 0; currentMatch = 0; maxMatch = 0; maxMaxMatch = 0; } else { i--; remaining = patternLength; lastMatch = 0; currentMatch = 0; maxMatch = 0; maxMaxMatch = 0; } } else { i = textLength; remaining = patternLength; lastMatch = 0; currentMatch = 0; maxMatch = 0; maxMaxMatch = 0; } } } else { if (i > 0) { if (t[i] == badChar || (goodSuffix != 0 && t[i] == goodSuffix)) { i--; remaining = patternLength; lastMatch = 0; currentMatch = 0; maxMatch = 0; maxMaxMatch = 0; } else { i--; remaining = patternLength; lastMatch = 0; currentMatch = 0; maxMatch = 0; maxMaxMatch = 0; } } else { i = textLength; remaining = patternLength; lastMatch = 0; currentMatch = 0; maxMatch = 0; maxMaxMatch = 0; } }}return -1; }
#endif