bool String::match(char *string, char *mask) { /* Loop until the end of the mask, */ while (*mask) { /* See if the current character is a wildcard or not. */ if (!WILDCARD(*mask)) { /* * If it's not a wildcard, the string and mask * must match exactly to be a match. */ if (!*string || *mask != *string) { return false; } mask++, string++; } else { /* If we have a wildcard, look for the next character. */ while (WILDCARD(*mask)) { mask++; } /* * There is more coming after the wildcard, to which the * string must match. */ if (*mask) { /* * Loop until the char in string matches the char * after the wildcard. */ while (true) { if (!*string) return false; if (*mask == *string) break; string++; } } /* Mask ends with a wildcard, which means the string matches. */ else return true; } } /* If there remains more data in the string, it's not a match. */ return *string ? false : true; }
int memcmp2 (const void *buffer, const void *search, size_t searchlen, unsigned int flags) { #define WILDCARD(f) (f & 0xff) size_t i = 0, j = 0; const unsigned char *b = (const unsigned char *) buffer, *s = (const unsigned char *) search; #ifdef DEBUG if (flags & MEMMEM2_WCARD (0)) printf ("wildcard: %c\n", WILDCARD (flags)); #endif if (!flags) return memcmp (buffer, search, searchlen); if (flags & MEMMEM2_REL) { searchlen--; if (searchlen < 1) return -1; } for (i = j = 0; i < searchlen; i++, j++) { if (flags & MEMMEM2_WCARD (0)) { if (*(s + i) == WILDCARD (flags)) continue; } if (flags & MEMMEM2_REL) { if ((*(b + j) - *(b + j + 1)) != (*(s + i) - *(s + i + 1))) break; } else { if (flags & MEMMEM2_CASE && isalpha (*(s + i))) { if (tolower (*(b + j)) != tolower (*(s + i))) break; } else if (*(b + j) != *(s + i)) break; } } return i == searchlen ? 0 : -1; }
static const std::string& WILDCARD_STRING() { static std::string s(1, WILDCARD()); return s; }