Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: C vs perl

by broquaint (Abbot)
on Apr 28, 2002 at 14:20 UTC ( [id://162655]=note: print w/replies, xml ) Need Help??


in reply to C vs perl

That is some mighty fat C. While I am all for people using perl in situations like this I have to respect my elders and come in to defend C's ability to be compact when it wants to be.
char* crlf2ptag(char* str) { char *ret, *pstr, *pret; /* ok, this could be done better but it's only a string ;-) */ ret = (char*) malloc(strlen(str) << 2 + 8); strncpy(ret, "<p>", 3); pstr = str; pret = &ret[3]; while(*pstr != '\0') if(*pstr == '\r' && *(pstr+1) != '\0' && *(pstr+1) == '\n') { strncpy(pret, "</p><p>", 7); pret += 7; pstr += 2; } else *pret++ = *pstr++; ret[strlen(ret) - 3] = '\0'; return ret; }
This compiles under cygwin's gcc 2.95 in win98 and I'd be surprised if there were problems elsewhere (make sure you include string.h though!). I'm sure this could be golfed to heck with even more pointer trickery, but my point is that the as long as you are a competent craftsmen it doesn't really matter what tools you use.
HTH

_________
broquaint

update: now checks for \r and \n to appease abstracts and should allocate enough space for all but the most \r\n ridden strings.

Replies are listed 'Best First'.
Re: C vs perl
by Dominus (Parson) on Apr 28, 2002 at 15:04 UTC
    Says broquaint:
    /* ok, this could be done better but it's only a string ;-) */ ret = (char*) malloc(strlen(str) + strlen(str));
    String or not, it's clear that if you were going to do that, you should have done this instead:
    ret = (char*) malloc(strlen(str) * 2);
    Also, your function does not work. (I think you forgot to test it before you posted.) You need to have
    pret += 7;
    in the if block.

    --
    Mark Dominus
    Perl Paraphernalia

      Also, your function does not work. (I think you forgot to test it before you posted.)
      I'd walked away from the computer and was going about my rainy afternoon before a little light-bulb appeared over my head and I ran back to add the offending line in hope that no one would notice. But the *second* I saw you in Other Users I knew it was coming ;-)

      A lesson learned for the day - think before posting kids.

      _________
      broquaint

Re: Re: C vs perl
by meonkeys (Chaplain) on Apr 28, 2002 at 18:45 UTC
    ++broquaint (after fixes, of course). A mighty phat response. One question since I am still learning C... when you do
    ret = (char*) malloc(strlen(str) + strlen(str));
    Are ret[0], ret[1], ret[2], ... ret[n] all set to '\0'? My manual for malloc() says "The memory is not cleared", but in my debugger it looks like all these values are zero.

    Also, I don't think you need to cast malloc(). In fact, it might hide errors.

    ---
    "A Jedi uses the Force for knowledge and defense, never for attack."
      malloc just hands you back some memory that it has handy. If it's newly allocated from the system, odds are that it'll be zeroed. (On most OSes these days freshly minted process memory comes pre-zeroed since it's no more expensive to hand you zeroed memory as any other type) malloc keeps a free list, though, and when you free() some memory, you may get that same chunk back later, with whatever gook might be in it.

      If you want guaranteed zeroed memory, use calloc() instead. It zeroes the memory before it's handed to you, and generally in the way most efficient for the OS you're on.

      Also, I don't think you need to cast malloc(). In fact, it might hide errors.

      The type of malloc is void*, I think I remember a compiler telling me that char* is not the same as void*. Consequently I had to cast malloc to char*.
Re: Re: C vs perl
by abstracts (Hermit) on Apr 28, 2002 at 21:35 UTC
    First of all, this does NOT replace crlf pairs with </p><p>. Period. Second of all, there is a buffer overflow as you malloc(2*strlen(str)). Please pass your routine "1\r2\r3\r" and see how it will give you a nice SEGFAULT (which you might not even see since you're ona Win98 box).
Re: Re: C vs perl
by czrdup (Acolyte) on Apr 29, 2002 at 17:42 UTC
    I did some thinking and the best I could come up with in a short amount of time is this:
    char* destroycrlf(char* original) { char *ret, *token, sep[] = "\r"; ret = (char *)calloc(strlen(original) * 7 + 8,sizeof(char)); token = strtok( original, sep ); sprintf(ret ,"<p>" ); while (token) { if (token[0] == '\n') sprintf(ret, "%s<\\p><p>%s", ret, &token[1]); else sprintf(ret, "%s%s", ret, token); token = strtok( NULL, sep ); } sprintf(ret, "%s<\\p>", ret); return ret; }
    Maybe not the shortest, but it should work and is fairly quick.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://162655]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (9)
As of 2024-04-23 10:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found