J
jacob navia
Hi guys!
I like C because is fun. So, I wrote this function for the lcc-win32
standard library: strrepl.
I thought that with so many "C heads" around, maybe we could improve it
in a collective brainstorming session.
Let's discuss some C here, for a change
Specs:
-----
Function: strrepl
Synopsis
#include <string.h>
char *strrepl(char *InputString, char *StringToFind,
char *Replacement, char *Output);
Description
The strrepl function replaces in InputString all occurrences of
StringToFind by Replacement, writing the modified contents into the
Output string. The original input string is not modified.
If the Output argument is NULL, strrepl will return the space that would
be needed (including the terminating zero) for the replacement.
If Replacement is NULL and Output is not NULL, all occurrences of
StringToFind will be erased.
Returns
The strrepl function returns the needed length for the replacements if
its Output argument is NULL. If not, it returns the number of
replacements done.
Code:
-----
#include <string.h>
int strrepl(char *InputString,char *StringToFind,
char *StringToReplace,char *output)
{
char *offset = NULL, *CurrentPointer = NULL;
int insertlen;
int findlen = strlen(StringToFind);
int result = 0;
if (StringToReplace)
insertlen = strlen(StringToReplace);
else
insertlen = 0;
if (output) {
if (output != InputString)
memmove(output,InputString,strlen(InputString)+1);
InputString = output;
}
else
result = strlen(InputString)+1;
while (*InputString) {
offset = strstr(!offset?InputString:CurrentPointer,StringToFind);
if (offset == NULL)
break;
CurrentPointer = (offset + (output ? insertlen : findlen));
if (output) {
strcpy (offset, (offset + findlen));
memmove (offset + insertlen,
offset, strlen (offset) + 1);
if (insertlen)
memcpy (offset, StringToReplace, insertlen);
result++;
}
else {
result -= findlen;
result += insertlen;
}
}
return result;
}
All kinds of comments are welcome, regarding the code, the interface
design, the documentation, etc etc.
jacob
I like C because is fun. So, I wrote this function for the lcc-win32
standard library: strrepl.
I thought that with so many "C heads" around, maybe we could improve it
in a collective brainstorming session.
Let's discuss some C here, for a change
Specs:
-----
Function: strrepl
Synopsis
#include <string.h>
char *strrepl(char *InputString, char *StringToFind,
char *Replacement, char *Output);
Description
The strrepl function replaces in InputString all occurrences of
StringToFind by Replacement, writing the modified contents into the
Output string. The original input string is not modified.
If the Output argument is NULL, strrepl will return the space that would
be needed (including the terminating zero) for the replacement.
If Replacement is NULL and Output is not NULL, all occurrences of
StringToFind will be erased.
Returns
The strrepl function returns the needed length for the replacements if
its Output argument is NULL. If not, it returns the number of
replacements done.
Code:
-----
#include <string.h>
int strrepl(char *InputString,char *StringToFind,
char *StringToReplace,char *output)
{
char *offset = NULL, *CurrentPointer = NULL;
int insertlen;
int findlen = strlen(StringToFind);
int result = 0;
if (StringToReplace)
insertlen = strlen(StringToReplace);
else
insertlen = 0;
if (output) {
if (output != InputString)
memmove(output,InputString,strlen(InputString)+1);
InputString = output;
}
else
result = strlen(InputString)+1;
while (*InputString) {
offset = strstr(!offset?InputString:CurrentPointer,StringToFind);
if (offset == NULL)
break;
CurrentPointer = (offset + (output ? insertlen : findlen));
if (output) {
strcpy (offset, (offset + findlen));
memmove (offset + insertlen,
offset, strlen (offset) + 1);
if (insertlen)
memcpy (offset, StringToReplace, insertlen);
result++;
}
else {
result -= findlen;
result += insertlen;
}
}
return result;
}
All kinds of comments are welcome, regarding the code, the interface
design, the documentation, etc etc.
jacob