Created
September 5, 2015 16:34
-
-
Save badcc/2c20a63067b97851537a to your computer and use it in GitHub Desktop.
Depends on my personal library (currently private).
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#define AB_WIN | |
#include "ab.h" | |
void MatchString(char *Match, const char *String, char *Result) { | |
int StringIndex = 0; | |
int MatchLength = 0; | |
int ResultLength = 0; | |
bool Class = false; | |
bool JustRestart = false; | |
while (StringIndex < ab_strlen(String) && String[StringIndex]) { | |
bool Replace = false; | |
// Literal | |
if (String[StringIndex] == *Match) Replace = true; | |
if (*Match == '.') Replace = true; | |
if (*Match == '\\') { | |
*Match++; | |
MatchLength++; | |
Class = true; | |
} | |
// Classes | |
if (Class) { | |
// Alpha | |
if (*Match == 'w' && ab_isalpha(String[StringIndex])) Replace = true; | |
// Numberic | |
if (*Match == 'd' && ab_isnum(String[StringIndex])) Replace = true; | |
// Alphanumeric | |
if (*Match == 'a' && ab_isalphanum(String[StringIndex])) Replace = true; | |
} | |
// Quantifiers | |
if (Match[1] == '?') { | |
if (Replace) { // Match, optional | |
*Match++; | |
MatchLength++; | |
} else { // No match, optional. | |
*Match++; | |
*Match++; | |
MatchLength++; | |
MatchLength++; | |
// StringIndex--; | |
continue; | |
} | |
} | |
if (Replace) { | |
*Result++ = String[StringIndex]; | |
ResultLength++; | |
// printf("%s ", Result); | |
*Match++; | |
MatchLength++; | |
Class = false; | |
JustRestart = false; | |
} else { | |
while (--MatchLength >= 0) *Match--; | |
while (--ResultLength >= 0) *Result--; | |
StringIndex = 0; | |
if (JustRestart) { | |
*Result--; | |
break; | |
}; | |
JustRestart = true; | |
} | |
StringIndex++; | |
} | |
*Result = '\0'; | |
} | |
int main(int argc, char *argv[]) { | |
// Do not buffer stdout. | |
setvbuf(stdout, NULL, _IONBF, 0); | |
char Match[] = "\\worl?d"; | |
// char Match[] = "\\w\\d"; | |
char Result[10] = { 0 }; | |
MatchString(Match, "World", Result); | |
printf("%s\n", Result); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment