0

另请参阅:这是 C 的一个好的 substr() 吗?


strtok()和朋友跳过空字段,我不知道如何告诉它在这种情况下不要跳过而是返回空。

我能看到的大多数标记器的类似行为,甚至没有让我开始sscanf()(但它从未说过它会在空字段上工作)。

我一直在滚动并且也感到困倦,所以在这里进行审查:

char* substr(const char* text, int nStartingPos, int nRun)
{
    char* emptyString = strdup(""); /* C'mon! This cannot fail */

    if(text == NULL) return emptyString;

    int textLen = strlen(text);

    --nStartingPos;

    if((nStartingPos < 0) || (nRun <= 0) || (textLen == 0) || (textLen < nStartingPos)) return emptyString;

    char* returnString = (char *)calloc((1 + nRun), sizeof(char));

    if(returnString == NULL) return emptyString;

    strncat(returnString, (nStartingPos + text), nRun);

    /* We do not need emptyString anymore from this point onwards */
    free(emptyString);
    emptyString = NULL;

    return returnString;
}

int TokenizeC(const char* text, char delim, char ***output)
{
    if((*output) != NULL) return -1; /* I will allocate my own storage */

    int nCountOfDelimiters = 0;
    int dx = 0;
    int nCountOfElements = 0;
    int textLen = strlen(text);

    for(; dx < textLen; ++dx)
    {
        if((text[dx] == delim) && (dx != (textLen - 1))) ++nCountOfDelimiters;
        /* trailing delimiter need not be counted separately as we are */
        /* incrementing the count always by 1 */
    }

    /*
    We will have as many array elements as nCountOfDelimiters + 1
    Tokenizing an empty string should return a single token that would
    be empty (Is this not how most libraries behave? Or should it return NULL?)
    */

    (*output) = (char **)malloc((1 + nCountOfDelimiters) * sizeof(char *));

    for(dx = 0; dx < textLen; dx++)
    {
        int nStartOfString = (1 + dx);

        //printf("\n[! 1]dx = %d, nStartOfString = %d", dx, nStartOfString);

        /* Get the run between delimiters */
        while((dx < textLen) && (text[dx] != delim)) dx++;

        //printf("\n[! 2]dx = %d, nStartOfString = %d", dx, nStartOfString);

        (*output)[nCountOfElements] = (1 + dx - nStartOfString) ? substr(text, nStartOfString, (1 + dx - nStartOfString)) : strdup("");

        //printf("\n[!]substr(text, %d, %d) => '%s'", nStartOfString, (1 + dx - nStartOfString), (*output)[nCountOfElements]);

        if(NULL == (*output)[nCountOfElements])
        {
            // Woops! Undo all
            // TODO: How to test this scenario?!

            for(; nCountOfElements >= 0; --nCountOfElements)
            {
                if((*output)[nCountOfElements] != NULL) free((*output)[nCountOfElements]);
                (*output)[nCountOfElements] = NULL;
            }

            return -2; 
        }

        ++nCountOfElements;
    }

    return nCountOfElements; /* Return the number of tokens if sucessful */
}

void reclaim2D(char ***store, unsigned int itemCount)
{
    for (int x = 0; itemCount < itemCount; ++x)
    {
        if((*store)[x] != NULL) free((*store)[x]);
        (*store)[x] = NULL;
    }

    if((*store) != NULL) free((*store));
    (*store) = NULL;
}

这是驱动程序:

int main()
{
    // Trailing '-' scenarios not giving correct count of elements
    // (off by 1 for the last element that should come as empty)

    const char *text = "1-2-3-6-7-8-9-10-11-", delim = '-'; // 10 elements

    char **output = NULL;

    int c = TokenizeC(text, delim, &output);

    printf("\n\n[*]%d", c);

    for (int x = 0; x < c; ++x)
    {
        printf("\n[main]'%s'", output[x]); //Expected : 1-2-3-6-7-8-9-10-11-<empty>
    }

    reclaim2D(&output, c);

    text = "12-3-6-7-8-9-10-11";  // 8 elements

    c = TokenizeC(text, delim, &output);

    printf("\n\n[*]%d", c);

    for(int x = 0; x < c; ++x)
    {
        printf("\n[main]'%s'", output[x]); //Expected : 12-3-6-7-8-9-10-11
    }

    reclaim2D(&output, c);

    text = "-----2--4--6-7100000000-8-9-10-11-100000000-";  // 17 elements

    c = TokenizeC(text, delim, &output);

    printf("\n\n[*]%d", c);

    for(int x = 0; x < c; ++x)
    {
        printf("\n[main]'%s'", output[x]);
        //Expected <empty>-<empty>-<empty>-<empty>
        // -<empty>-2-<empty>-4-<empty>-6-7100000000
        // -8-9-10-11-100000000-<empty>
    }

    reclaim2D(&output, c);

    text = "-----2--4--6-7100000000-8-9-10-11-100000000";  // 16 elements

    c = TokenizeC(text, delim, &output);

    printf("\n\n[*]%d", c);

    for(int x = 0; x < c; ++x)
    {
        printf("\n[main]'%s'", output[x]);
        //Expected : <empty>-<empty>-<empty>-<empty>-<empty>
        //-2-<empty>-4-<empty>-6-7100000000-8-9-10-11-100000000
    }

    reclaim2D(&output, c);

    return 0;
}

是的,你没看错;它现在仅适用于单个分隔符,但当然,我们需要处理一个错误。

输出:

[*]9
[main]'1'
[main]'2'
[main]'3'
[main]'6'
[main]'7'
[main]'8'
[main]'9'
[main]'10'
[main]'11'

[*]8
[main]'12'
[main]'3'
[main]'6'
[main]'7'
[main]'8'
[main]'9'
[main]'10'
[main]'11'

[*]16
[main]''
[main]''
[main]''
[main]''
[main]''
[main]'2'
[main]''
[main]'4'
[main]''
[main]'6'
[main]'7100000000'
[main]'8'
[main]'9'
[main]'10'
[main]'11'
[main]'100000000'

[*]16
[main]''
[main]''
[main]''
[main]''
[main]''
[main]'2'
[main]''
[main]'4'
[main]''
[main]'6'
[main]'7100000000'
[main]'8'
[main]'9'
[main]'10'
[main]'11'
[main]'100000000'

我也将其设为 wiki,因为我在网上看到了许多类似的请求。

4

1 回答 1

1

在某些系统上,有一个名为 strsep() 的函数。你可以通过谷歌找到它的源代码。例如,http://www.google.com/codesearch/p?hl=zh-TW#XAzRy8oK4zA/libc/string/strsep.c&q=strsep

于 2009-05-17T08:46:51.263 回答