作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
另请参阅:Is this a good substr() for C?
<小时/>strtok()
和 friend 跳过空字段,我不知道如何告诉它在这种情况下不要跳过而是返回空。
我能看到的大多数分词器都有类似的行为,甚至没有让我开始使用 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;
}
是的,你没看错;它现在仅适用于单个分隔符,但是当然,我们需要解决一个错误。
Outputs:
[*]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,因为我在网上看到了许多类似的请求。
最佳答案
在某些系统上,有一个名为 strsep() 的函数。你可以通过谷歌找到它的源代码。例如,http://www.google.com/codesearch/p?hl=zh-TW#XAzRy8oK4zA/libc/string/strsep.c&q=strsep
关于C Tokenizer(当字段丢失时它也会返回空。耶!),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/874161/
另请参阅:Is this a good substr() for C? strtok() 和 friend 跳过空字段,我不知道如何告诉它在这种情况下不要跳过而是返回空。 我能看到的大多数分词器都有
这个问题在这里已经有了答案: What is a NullPointerException, and how do I fix it? (12 个答案) 关闭 6 年前。 编辑:排序。解决方案见底部
我是一名优秀的程序员,十分优秀!