int ZLSearchUtil::find(const char *text, size_t length, const ZLSearchPattern &pattern, int pos) { if (pattern.ignoreCase()) { if (pos < 0) { pos = 0; } const std::string &lower = pattern.lowerCasePattern(); const std::string &upper = pattern.upperCasePattern(); const char *last = text + length - pattern.lowerCasePattern().length(); const char *patternLast = lower.data() + lower.length() - 1; for (const char *i = text + pos; i <= last; ++i) { const char *j0 = lower.data(); const char *j1 = upper.data(); const char *k = i; for (; j0 <= patternLast; ++j0, ++j1, ++k) { if ((*j0 != *k) && (*j1 != *k)) { break; } } if (j0 > patternLast) { return i - text; } } return -1; } else { if (pos < 0) { pos = 0; } const std::string &lower = pattern.lowerCasePattern(); const char *last = text + length - pattern.lowerCasePattern().length(); const char *patternLast = lower.data() + lower.length() - 1; for (const char *i = text + pos; i <= last; ++i) { const char *j0 = lower.data(); const char *k = i; for (; j0 <= patternLast; ++j0, ++k) { if (*j0 != *k) { break; } } if (j0 > patternLast) { return i - text; } } return -1; } }
int ZLSearchUtil::find(const char *text, size_t length, const ZLSearchPattern &pattern, int pos) { if (pattern.ignoreCase()) { if (pos < 0) { pos = 0; } const std::string &lower = pattern.lowerCasePattern(); const std::string &upper = pattern.upperCasePattern(); const char *last = text + length - pattern.lowerCasePattern().length(); const char *patternLast = lower.data() + lower.length() - 1; for (const char *i = text + pos; i <= last; ++i) { const char *j0 = lower.data(); const char *j1 = upper.data(); const char *k = i; for (; j0 <= patternLast; ++j0, ++j1, ++k) { if ((*j0 != *k) && (*j1 != *k)) { break; } else { if ((unsigned char)(*k) > 127) { if (j0 == patternLast) break; //check lower case if (*j0 == *k) { if ((unsigned char)(*(j0 + 1)) != (unsigned char)(*(k + 1))) { goto check_upper_case; } ++j0, ++j1, ++k; continue; } //check upper case check_upper_case: if (*j1 == *k) { if ((unsigned char)(*(j1 + 1)) != (unsigned char)(*(k + 1))) { break; } ++j0, ++j1, ++k; continue; } } } } if (j0 > patternLast) { return i - text; } } return -1; } else { if (pos < 0) { pos = 0; } const std::string &lower = pattern.lowerCasePattern(); const char *last = text + length - pattern.lowerCasePattern().length(); const char *patternLast = lower.data() + lower.length() - 1; for (const char *i = text + pos; i <= last; ++i) { const char *j0 = lower.data(); const char *k = i; for (; j0 <= patternLast; ++j0, ++k) { if (*j0 != *k) { break; } } if (j0 > patternLast) { return i - text; } } return -1; } }