准备一个C Style的Split函数

      准备一个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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值