Skip to content

Instantly share code, notes, and snippets.

@Quackward
Last active September 1, 2021 11:34
Show Gist options
  • Select an option

  • Save Quackward/49b0037ddbc6a23f6b91daff95a62227 to your computer and use it in GitHub Desktop.

Select an option

Save Quackward/49b0037ddbc6a23f6b91daff95a62227 to your computer and use it in GitHub Desktop.

Revisions

  1. Quackward revised this gist Sep 1, 2021. 1 changed file with 30 additions and 3 deletions.
    33 changes: 30 additions & 3 deletions findFirstSubStr.cpp
    Original file line number Diff line number Diff line change
    @@ -1,8 +1,8 @@
    // [email protected] \(v ` >`)_v
    // returns index of first `searchStr` we found, or `searchCount` if none was found
    // returns index of first `searchStrs` we found, or `searchCount` if none was found
    // str string we are searching through
    // searchStrs array of strings we are looking for
    // searchCount the number of strings we are looking for
    // 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) {
    @@ -29,4 +29,31 @@ uint32 findFirstSubStr(const char * str, const char ** searchStrs, uint32 search
    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;
    }
  2. Quackward created this gist Sep 1, 2021.
    32 changes: 32 additions & 0 deletions findFirstSubStr.cpp
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,32 @@
    // [email protected] \(v ` >`)_v
    // returns index of first `searchStr` we found, or `searchCount` if none was found
    // str string we are searching through
    // searchStrs array of strings we are looking for
    // searchCount the number of 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;
    }