简体   繁体   English

用C中的另一个字符串替换子字符串

[英]Replacing a substring with another string in C

I am writing a code to replace all MACROS with its value. 我正在编写代码,用其值替换所有MACROS。 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. 如果我的宏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*/}

       } 

Since I'm using memmove and memcopy for the first time, I'm in doubt whether the above code is stable and works properly. 由于我是第一次使用memmovememcopy ,因此我怀疑上面的代码是否稳定并且可以正常工作。

Is the above code correct? 上面的代码正确吗? And is the above code stable for all cases of inputs? 上面的代码对于所有输入情况都是稳定的吗?

I see at least three problems: 我至少看到三个问题:

  1. memmove shouldn't use sizeof(p) which is always going to be fixed (say 4), it should use strlen(line) - (p + strlen(p) - line) memmove不应使用总是固定的sizeof(p) (例如4),而应使用strlen(line) - (p + strlen(p) - line)
  2. You need to handle the case where replacing the macro increases the length of the line beyond 100 您需要处理以下情况:替换宏会使行的长度超过100
  3. You need to handle cases where the macro label is surrounded by symbols. 您需要处理宏标签被符号包围的情况。 ie, _MACRO_ is not the same as MACRO. 即_MACRO_与MACRO不同。

if(strlen(p) != strlen(line)) Why not simply use if(p != line) here? if(strlen(p) != strlen(line))为什么不在这里简单地使用if(p != line) That should be equivalent, easier to understand and faster (strlen scans the entire string). 那应该是等效的,更容易理解和更快(strlen扫描整个字符串)。

isalnum(...) == 0 Personal preference maybe, but I'd write that expression as !isalnum(...) since it's easier to understand the meaning this way. isalnum(...) == 0可能是个人喜好,但我将该表达式写为!isalnum(...)因为这样更容易理解其含义。

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. 它将根据指针大小移动许多字符,这没有任何意义,并且如果srcStr长于destStr,则移动的目标可能是行缓冲区开始之前的位置。 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. 如果要移动行的其余部分以适应更改的长度,请尝试以下操作: memmove(p+strlen(destStr), p+strlen(srcStr), strlen(p+strlen(srcStr)+1); + 1对于移动空终止符也很重要,当然,您需要确保行缓冲区实际提供了足够的空间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM