准备一个C Style的Split函数,不用strtok,不用std::string,不用STL。据说范型编程因为占内存比较多,有些单位也要求禁用STL。
Split在很多语言中是经典函数,实现多次调用,不复开内存:
Ref1(char **):
#include <stdio.h> #include <stdlib.h> char **Split(const char *inputString, const char *delimiter, int *count) { char **splitRes = NULL; int bufferSize = 10; int splitCount = 0; int start = 0; splitRes = (char**)malloc(bufferSize * sizeof(char*)); if (!splitRes ) { *count = 0; return NULL; } char *buffer = (char*)malloc((strlen(inputString) + 1) * sizeof(char)); if (!buffer) { free(splitRes ); *count = 0; return NULL; } strcpy(buffer, inputString); char *token = NULL; while ((token = strstr(buffer + start, delimiter)) != NULL) { *token = '\0'; char *subStr = (char*)malloc((token - (buffer + start) + 1) * sizeof(char)); if (!subStr) { free(buffer); *count = splitCount; for (int i = 0; i < splitCount; i++) { free(splitRes [i]); } free(splitRes ); return NULL; } strcpy(subStr, buffer + start); splitRes [splitCount++] = subStr; if (splitCount >= bufferSize) { bufferSize *= 2; char **newSplitRes = (char**)realloc(splitRes , bufferSize * sizeof(char*)); if (!newSplitRes ) { free(buffer); *count = splitCount; for (int i = 0; i < splitCount; i++) { free(splitRes [i]); } free(splitRes ); return NULL; } else { splitRes = newSplitRes ; } } start = (token - buffer) + strlen(delimiter); } char *subStr = (char*)malloc((strlen(buffer + start) + 1) * sizeof(char)); if (!subStr) { free(buffer); *count = splitCount; for (int i = 0; i < splitCount; i++) { free(splitRes [i]); } free(splitRes ); return NULL; } strcpy(subStr, buffer + start); splitRes [splitCount++] = subStr; free(buffer); *count = splitCount; return splitRes ; }
Ref2(void && char *)://另参考一个:
void split(char *src, const char *separator, char **dest, int *num)
{
char *pSeparator, *pStart, *pEnd;
unsigned int sep_len;
int count = 0;
if (src == NULL || strlen(src) == 0)
return;
pSeparator = (char *)malloc(16);
if (pSeparator == NULL)
return;
if (separator == NULL || strlen(separator) == 0)
strcpy(pSeparator, " "); /* one blank by default */
else
strcpy(pSeparator, separator);
sep_len = strlen(pSeparator);
pStart = src;
while (1)
{
pEnd = strstr(pStart, pSeparator);
if (pEnd != NULL)
{
memset(pEnd, '/0', sep_len);
*dest++ = pStart;
pEnd = pEnd + sep_len;
pStart = pEnd;
++count;
}
else
{
*dest = pStart;
++count;
break;
}
}
*num = count;
if (pSeparator != NULL)
free(pSeparator);
}
Ref3(struct *):(安全推荐,抽空去看看AWK Src,Linux简直值得学习)
#include <stdio.h> #include <stdlib.h> #include <string.h> typedef struct { char** tokens; // 存储分割后的子字符串 int count; // 分割后的子字符串数量 } SplitStc; SplitStc split_string(char* str, char* delim) { SplitStc result = { NULL, 0 }; char* p = strtok(str, delim); while (p != NULL) { result.tokens = realloc(result.tokens, sizeof(char*) * (result.count+1)); result.tokens[result.count] = p; result.count++; p = strtok(NULL, delim); } return result; } void free_split_result(SplitStc * result) { for (int i = 0; i < result->count; i++) { free(result->tokens[i]); } free(result->tokens); result->count = 0; }