Last active
September 1, 2021 11:34
-
-
Save Quackward/49b0037ddbc6a23f6b91daff95a62227 to your computer and use it in GitHub Desktop.
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
// [email protected] \(v ` >`)_v | |
// returns index of first `searchStrs` we found, or `searchCount` if none was found | |
// str string we are searching through | |
// searchStrs array of sub-strings we are looking for | |
// searchCount the count of sub-strings we are looking for | |
// outStartOfArg if not NULL, fills this with pointer to first char in found search, or NULL if none found | |
// outEndOfArg if not NULL, fills this with pointer to first char AFTER found search, or NULL if none found | |
uint32 findFirstSubStr(const char * str, const char ** searchStrs, uint32 searchCount, const char ** outStartOfArg, const char ** outEndOfArg) { | |
if (str && searchStrs && searchCount) { | |
while (*str) { | |
for (uint32 n = 0; n < searchCount; ++n) { | |
if (searchStrs[n] && *str == searchStrs[n][0]) { | |
const char * token = searchStrs[n]+1; | |
const char * strSub = str+1; | |
while (*token && *token == *strSub) { | |
++token; | |
++strSub; | |
} | |
if (*token == 0) { | |
if(outStartOfArg) *outStartOfArg = str; | |
if(outEndOfArg) *outEndOfArg = strSub; | |
return n; | |
} | |
} | |
} | |
++str; | |
} | |
} | |
if(outStartOfArg) *outStartOfArg = NULL; | |
if(outEndOfArg) *outEndOfArg = NULL; | |
return searchCount; | |
} | |
// same as `findFirstSubStr()` but case insensitive | |
uint32 findFirstSubStr_caseless(const char * str, const char ** searchStrs, uint32 searchCount, const char ** outStartOfArg, const char ** outEndOfArg) { | |
if (str && searchStrs && searchCount) { | |
while (*str) { | |
for (uint32 n = 0; n < searchCount; ++n) { | |
if (searchStrs[n] && tolower(*str) == tolower(searchStrs[n][0])) { | |
const char * token = searchStrs[n]+1; | |
const char * strSub = str+1; | |
while (*token && tolower(*token) == tolower(*strSub)) { | |
++token; | |
++strSub; | |
} | |
if (*token == 0) { | |
if(outStartOfArg) *outStartOfArg = str; | |
if(outEndOfArg) *outEndOfArg = strSub; | |
return n; | |
} | |
} | |
} | |
++str; | |
} | |
} | |
if(outStartOfArg) *outStartOfArg = NULL; | |
if(outEndOfArg) *outEndOfArg = NULL; | |
return searchCount; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment