code review

  • Thread starter E. Robert Tisdale
  • Start date
E

Emmanuel Delahaye

E. Robert Tisdale said:
char *dot_to_underscore(const char *s)
{
char *t = malloc(strlen(s) + 1);

You must be a fake of ERT.
if(t != NULL)
{
char *u;
strcpy(t, s);
u = t;
while(*u)
{
if(*u == '.')
{
*u = '_';
}
++u;
}
}
return t;
}

Is there any way to improve the code above?

Just a proposal:

#include <string.h>

char *dot_to_underscore_dyn (const char *const s)
{
char *t = NULL;

if (s != NULL)
{
size_t const size = strlen (s) + 1;

t = malloc (size);

if (t != NULL)
{
char *u = t;
memcpy(t, s, size);

while (*u)
{
if (*u == '.')
{
*u = '_';
}
++u;
}
}
}
return t;
}
 
I

Irrwahn Grausewitz

E. Robert Tisdale said:
char *dot_to_underscore(const char *s)
{
char *t = malloc(strlen(s) + 1);
if(t != NULL)
{
char *u;
strcpy(t, s);
u = t;
while(*u)
{
if(*u == '.')
{
*u = '_';
}
++u;
}
}
return t;
}


Is there any way to improve the code above?

/* Saturday-morning-too-much-coffee-version ;-) */

#include <stdlib.h>
#include <string.h>

char *dot_to_underscore( const char *s )
{
char *res = malloc( strlen( s ) + 1 );

if ( res != NULL )
{
char *p = res;
while ( *p++ = *s == '.' ? '_' : *s )
s++;
}
return res;
}


Regards
 
E

E. Robert Tisdale

E. Robert Tisdale said:
char *dot_to_underscore(const char *s)
{
char *t = malloc(strlen(s) + 1);
if(t != NULL)
{
char *u;
strcpy(t, s);
u = t;
while(*u)
{
if(*u == '.')
{
*u = '_';
}
++u;
}
}
return t;
}


Is there any way to improve the code above?

Assuming the following inclusions:

#include <stdlib.h>
#include <string.h>

I might write:

inline
char* dot_to_underscore(const char* s) {
char* const t = (char*)malloc(strlen(s) + 1);
if(NULL != t) {
char *u = t;
while('\0' != (*u = *s)) {
if ('.' == *u)
*u = '_';
++u;
++s;
}
}
return t;
}

and include it in my main.c source file with:

#include <stdio.h>

int main(int argc, char* argv[]) {
const char* const t = dot_to_underscore(argv[1]);
fprintf(stdout, "%s\n", t);
free((void*)t);
return 0;
}
> gcc -Wall -std=c99 -pedantic -O2 -o main main.c
> ./main 'users.powernet.co.uk'
users_powernet_co_uk
 
I

Irrwahn Grausewitz

E. Robert Tisdale said:
E. Robert Tisdale wrote:


Assuming the following inclusions:

#include <stdlib.h>
#include <string.h>

I might write:

inline
char* dot_to_underscore(const char* s) {
char* const t = (char*)malloc(strlen(s) + 1);
Unnecessary cast of malloc return value. *SCNR*
if(NULL != t) {
char *u = t;
while('\0' != (*u = *s)) {
if ('.' == *u)
*u = '_';
++u;
++s;
}
}
return t;
}

and include it in my main.c source file with:

#include <stdio.h>

int main(int argc, char* argv[]) {
const char* const t = dot_to_underscore(argv[1]);

Undefined behavior, if program is invoked without argument(s).
fprintf(stdout, "%s\n", t);
free((void*)t);
return 0;
}

users_powernet_co_uk

Status of C99 inline function support is "Broken" for current
releases of gcc, according to gnu.org (modulo caching errors).

Do not resort to an inferior implementation, when you want to
use certain C99 features in your code.
</OT>

Now, what an improvement ...

Regards
 
C

Capstar

Morris said:
Woops, indeed.

One more time, to see if I can get the header files right, add the
string terminator, and remove some of the clutter (braces) that
interferes with readability (-:

#include <stdlib.h>
#include <string.h>
char *dot_to_underscore(const char *s)
{ char *t=s,*u;
if (s && (t = u = malloc(strlen(s) + 1)))
do *u++ = (*s == '.') ? '_' : *s;
while (*s++);
return t;
}

Not being an expert or anything, but wouldn't it be better to change
char *t=s,*u;
to
char *t=NULL,*u;

I believe the result is the same, because t will be NULL if s == NULL
and if malloc fails, just as in the original code, but if compiled with:
gcc -c -W -Wall -ansi -pedantic dot_to_underscore.c

it will suppress:
dot_to_underscore.c: In function `dot_to_underscore':
dot_to_underscore.c:5: warning: initialization discards qualifiers from
pointer target type

Mark
 
R

Richard Heathfield

E. Robert Tisdale said:
char *dot_to_underscore(const char *s)
{
char *t = malloc(strlen(s) + 1);
if(t != NULL)
{
char *u;
strcpy(t, s);
u = t;
while(*u)
{
if(*u == '.')
{
*u = '_';
}
++u;
}
}
return t;
}


Is there any way to improve the code above?


Incredible. I didn't recognise this code! But it's mine all right. Mr
Tisdale found it at http://users.powernet.co.uk/eton/c/emgen.c

Thanks for the suggestions for improvements, folks.
 
J

Joona I Palaste

Incredible. I didn't recognise this code! But it's mine all right. Mr
Tisdale found it at http://users.powernet.co.uk/eton/c/emgen.c
Thanks for the suggestions for improvements, folks.

Algorithmically, the function is so trivial that I thought Tisdale had
come up with the same implementation as you by accident, but then I
checked your web site. It matches your code's syntax and writing style
exactly to the last character. Now there is no doubt. Tisdale used your
code without saying it was yours.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,139
Messages
2,570,807
Members
47,356
Latest member
Tommyhotly

Latest Threads

Top