C
Christopher Benson-Manica
I have a C-style string (null-terminated) that consists of items in one of the
following formats:
14 characters
5 characters space 8 characters
6 characters colon 8 characters
5 characters colon 8 characters
Items are delimited by semicolons or commas. I have to produce a string
delimited only by semicolons and containing items in the first two formats
only. For example,
"AAAAAAAAAAAAAA,AAAAAA:AAAAAAAA;AAAAA AAAAAAAA,AAAAA:AAAAAAAA" ->
"AAAAAAAAAAAAAA;AAAAAAAAAAAAAA;AAAAA AAAAAAAA;AAAAA AAAAAAAA"
Posting to comp.lang.c yielded the following:
int myfunc( const char *idlist )
{
int items=0;
char *newstr=(char *)malloc( strlen(idlist)+1 );
if( !newstr ) {
return( -2 );
}
int srcidx=0;
int destidx=0;
int chars=0;
for( ; idlist[srcidx] ; srcidx++ ) {
if( idlist[srcidx] == ':' ) {
if( chars == 5 ) {
newstr[destidx++]=' ';
chars++;
}
else if( chars != 6 )
return( -1 ); // Invalid format
}
}
else if( idlist[srcidx] == ';' || idlist[srcidx] == ',' ) {
if( chars != 14 ) { // Invalid format
return( -1 );
}
newstr[destidx++]=';';
chars=0;
items++;
}
else if( ++chars > 14 ) {
return( -1 );
}
else {
newstr[destidx++]=idlist[srcidx];
}
}
newstr[destidx]='\0';
if( chars == 14 ) {
items++;
}
else if( !items || chars ) { // items == 0 || chars != 0
return( -1 );
}
printf( "The string '%s' has %d items.\n", newstr, items );
/* Call a function using newstr here */
free( newstr );
return( 0 );
}
I'd like to know how to improve this function (specifically, the call to
malloc()) to make it more like typical C++. One thing: Don't tell me to use
std::string's, because it isn't an option (the C++ code at my company uses
C-style strings almost exclusively).
following formats:
14 characters
5 characters space 8 characters
6 characters colon 8 characters
5 characters colon 8 characters
Items are delimited by semicolons or commas. I have to produce a string
delimited only by semicolons and containing items in the first two formats
only. For example,
"AAAAAAAAAAAAAA,AAAAAA:AAAAAAAA;AAAAA AAAAAAAA,AAAAA:AAAAAAAA" ->
"AAAAAAAAAAAAAA;AAAAAAAAAAAAAA;AAAAA AAAAAAAA;AAAAA AAAAAAAA"
Posting to comp.lang.c yielded the following:
int myfunc( const char *idlist )
{
int items=0;
char *newstr=(char *)malloc( strlen(idlist)+1 );
if( !newstr ) {
return( -2 );
}
int srcidx=0;
int destidx=0;
int chars=0;
for( ; idlist[srcidx] ; srcidx++ ) {
if( idlist[srcidx] == ':' ) {
if( chars == 5 ) {
newstr[destidx++]=' ';
chars++;
}
else if( chars != 6 )
return( -1 ); // Invalid format
}
}
else if( idlist[srcidx] == ';' || idlist[srcidx] == ',' ) {
if( chars != 14 ) { // Invalid format
return( -1 );
}
newstr[destidx++]=';';
chars=0;
items++;
}
else if( ++chars > 14 ) {
return( -1 );
}
else {
newstr[destidx++]=idlist[srcidx];
}
}
newstr[destidx]='\0';
if( chars == 14 ) {
items++;
}
else if( !items || chars ) { // items == 0 || chars != 0
return( -1 );
}
printf( "The string '%s' has %d items.\n", newstr, items );
/* Call a function using newstr here */
free( newstr );
return( 0 );
}
I'd like to know how to improve this function (specifically, the call to
malloc()) to make it more like typical C++. One thing: Don't tell me to use
std::string's, because it isn't an option (the C++ code at my company uses
C-style strings almost exclusively).