C 中用另一个字符串替换一个子字符串
我正在编写一个代码来用它的值替换所有宏。 如果我的宏 MAX 值为 1000, 在代码中,它必须替换为 1000。(我假设一种情况,如果 MACROS 是一行中的第一个单词,那么在该行中 MACROS 将不会被我们替换,并且这种情况我们将得到不同的处理。
//Code to replace MACROS BY THEIR VALUES
//line contains the actual one line of the code.
//line is initialized to contain as maximum number of charectos(say 100).
//SrcStr is the macro and destStr is its value.
//This block will be looped for all lines.
char* p;
p = strstr(line,srcStr);
if(p != NULL) //if the srcString is found
{
if(strlen(p) != strlen(line)) //special case
{
if( isalnum(*(p-1)) == 0 && isalnum( *(p+strlen(srcStr)))==0 )
// if the next char and prev char to our macro is not a alphabets or digits
{
/*As answered by medo42 (below)*/
memmove(p+strlen(destStr), p+strlen(srcStr),strlen(p+strlen(srcStr)+1);
memcpy(p,destStr,strlen(destStr));
}
}
else
{/* handle differently*/}
}
因为我是第一次使用 memmove
和 memcopy
,我怀疑上面的代码是否稳定并正常工作
。 上面的代码对于所有输入情况都是稳定的吗?
I am writing a code to replace all MACROS with its value.
If my macro MAX has a value 1000,
And in the code, it must be replaced with 1000.(I am assuming a case that if the MACROS are the first word in a line, then in that line MACROS will not we replaced, and that case will we be handled differently.
//Code to replace MACROS BY THEIR VALUES
//line contains the actual one line of the code.
//line is initialized to contain as maximum number of charectos(say 100).
//SrcStr is the macro and destStr is its value.
//This block will be looped for all lines.
char* p;
p = strstr(line,srcStr);
if(p != NULL) //if the srcString is found
{
if(strlen(p) != strlen(line)) //special case
{
if( isalnum(*(p-1)) == 0 && isalnum( *(p+strlen(srcStr)))==0 )
// if the next char and prev char to our macro is not a alphabets or digits
{
/*As answered by medo42 (below)*/
memmove(p+strlen(destStr), p+strlen(srcStr),strlen(p+strlen(srcStr)+1);
memcpy(p,destStr,strlen(destStr));
}
}
else
{/* handle differently*/}
}
Since I'm using memmove
and memcopy
for the first time, I'm in doubt whether the above code is stable and works properly.
Is the above code correct?
And is the above code stable for all cases of inputs?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我至少看到三个问题:
I see at least three problems:
sizeof(p)
which is always going to be fixed (say 4), it should usestrlen(line) - (p + strlen(p) - line)
if(strlen(p) != strlen(line))
为什么不在这里简单地使用if(p != line)
呢?这应该是等效的,更容易理解并且更快(strlen 扫描整个字符串)。isalnum(...) == 0
也许是个人喜好,但我会将该表达式写为!isalnum(...)
因为这样更容易理解其含义方式。memmove(p+(strlen(destStr)-strlen(srcStr)),p,sizeof(p));
这对我来说看起来不对。它将根据指针大小移动许多字符,这是没有意义的,如果 srcStr 比 destStr 长,则移动的目的地可能是行缓冲区开始之前的位置。如果您想移动该行的其余部分以调整更改后的长度,请尝试以下操作:memmove(p+strlen(destStr), p+strlen(srcStr), strlen(p+strlen(srcStr)+1) ; +1 对于移动空终止符也很重要,当然,您需要确保行缓冲区实际上提供了足够的空间。if(strlen(p) != strlen(line))
Why not simply useif(p != line)
here? That should be equivalent, easier to understand and faster (strlen scans the entire string).isalnum(...) == 0
Personal preference maybe, but I'd write that expression as!isalnum(...)
since it's easier to understand the meaning this way.memmove(p+(strlen(destStr)-strlen(srcStr)),p,sizeof(p));
This looks wrong to me. It will move a number of characters depending on your pointer size, which makes no sense, and if srcStr is longer than destStr, the destination of the move might be a position before the start of the line buffer. If you want to move the remainder of the line to adjust for the changed length, try this:memmove(p+strlen(destStr), p+strlen(srcStr), strlen(p+strlen(srcStr)+1);
The +1 is important to move the null terminator as well. Of course, you need to ensure that the line buffer actually provides enough space.