Skip to content

Instantly share code, notes, and snippets.

@fcaylus
Last active July 14, 2017 16:39
Show Gist options
  • Save fcaylus/68157c7eb49d6c188d46e8048abbb987 to your computer and use it in GitHub Desktop.
Save fcaylus/68157c7eb49d6c188d46e8048abbb987 to your computer and use it in GitHub Desktop.
CStringUtil
// Returns true if str contains c
constexpr bool contains(const char* str, const char *c)
{
return (*str == 0) ? false : (str[0] == *c ? true: contains(++str, c));
}
constexpr int countOccurences(char* str, char* c)
{
return (*str == 0) ? 0 : (str[0] == *c ? 1 + countOccurences(++str, c) : countOccurences(++str, c));
}
char** splitString(char* str, char* separator)
{
const int varCount = countOccurences(str, separator)+1;
char** list = (char**) calloc(varCount, sizeof(char*));
char* split = std::strtok(strdup(str), separator);
int i = 0;
while(split != NULL)
{
// Remove the first space generated by the preprocessor after every ","
if(strncmp(split, " ", 1) == 0)
list[i] = ++split;
else
list[i] = split;
i += 1;
split = std::strtok(NULL, separator);
}
return list;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment