Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: C vs perl

by abstracts (Hermit)
on Apr 28, 2002 at 07:56 UTC ( [id://162631]=note: print w/replies, xml ) Need Help??


in reply to C vs perl

Hello

Your C code is broken somwhat, let's see:

extern char * danCGIReplacedCRLF(char * szFixMe){ int i,j,iLen=0,iNewLen=0; int iCRLFcount=0; char* szResult=NULL; iLen=strlen(szFixMe); for (i=0;i<=iLen;i++){ /* this should be i < len */ if (szFixMe[i]==(int) '\n' || szFixMe[i]==(int) '\r') iCRLFcount++; /* and here you're counting how many \r or \n you see, not how many "\r\n" sequences */ } /* you don't need the <p>+NULL+text, you need <p>+text */ // need space for first <p> +NULL+ text // + replace each CR &LF + last " <\p>" iNewLen=3+1+strlen(szFixMe)+(iCRLFcount*8)+5; szResult = malloc(iNewLen); if (szResult){ /* what's wrong with strcpy(szResult, "<p>"); ? */ szResult[0]='<'; szResult[1]='p'; szResult[2]='>'; szResult[3]='\0'; i=0; j=3; while (i<=iLen) if (szFixMe[i]==(int) '\n' || szFixMe[i]==(int) '\r'){ /* here again, you're checking for \r or \n, now the pair */ strcat(szResult," </p><p>"); j+=8; /* and now I'm completely lost!, why are you looking for \n\r? I thought we were looking for \r\n. */ // deal with Newline CR pairs if(szFixMe[i]==(int) '\n' && szFixMe[i+1]==(int) '\r' || szFixMe[i]==(int) '\r' || szFixMe[i+1]==(int) ' +\n') i+=2; else i++; }else{ szResult[j]=szFixMe[i]; j++; szResult[j]='\0'; //dest string in strcat needs termin +ating \0 i++; } //outer loop & if statement //end of new document needs </p> to match at start strcat(szResult," </p>"); } // if(szResult !=NULL ) return szResult; }
Hmmm, I don't think this C code even comes closely to the Perl code given by you. Here one way to do it. This code aims to be simple and fast, rather than optimally fast. Enjoy :-).
/*********************************************************/ #if 0 we want to do the following: sub MakePtag{ my ($fixme)=@_; # take in our parameters $fixme='<p>'.$fixme; # Prepend a <p> tag to our string $fixme=~s|(\r\n)|</p><p>|g; # replace all \r\n with <\p><p> $fixme.='</p>'; # Append closing </p> tag return $fixme; } #endif char* make_p_tag(char* str){ int len = 0; int cr = 0; /* this will tell you if we found \r */ char *s = str; char *newstr, *s2; /* s#\r\n#<p></p>#, increases len by 5 (7-2)*/ while(*s != '\0'){ /* while we have more data */ ++len; if(*s == '\r'){ cr = 1; /* we found one */ } else if(*s == '\n' && cr){ /* we found \n and \r in the prev step */ cr = 0; /* forget we found a \r */ len += 5; } else { cr = 0; /* also forget we found an isolated \r*/ } ++s; } s = str; s2 = newstr = calloc(len+8, sizeof(char)); strcpy(s2,"<p>"); s2 += 3; while(*s != '\0'){ *s2 = *s; /* copy this char to the output string */ if(*s == '\r'){ cr = 1; /* mark that we put a \r */ } else if(*s == '\n' && cr){ /* now we placed \r\n */ cr = 0; strcpy(s2-1,"</p><p>"); /* put </p><p> */ s2 += 5; /* and advance */ } else { cr = 0; /* forget it */ } ++s; ++s2; } strcpy(s2,"</p>"); /* put terminating </p> */ s2+=4; *s2 = '\0'; return newstr; }
Hope this helps.

PS: You need </P> not <\P>
PS2: I know this is the *Perl* monastery, but I thought this shouldn't hurt :-)

Update: meant to say strcpy instead of strcat:

/* what's wrong with strcpy(szResult, "<p>"); ? */

Replies are listed 'Best First'.
Re: Re: C vs perl
by mandog (Curate) on Apr 29, 2002 at 01:14 UTC

    Thanks for the corrections. --My C code is pretty ugly. Especially the \r\n vs \n\r thing .

    For what it is worth I **do** need to allocate another byte for the terminating \0

    strlen does **not** include the terminating null see man strlen



    email: mandog
      My comment was regarding the following statement:
      szResult[3]='\0';
      So, basically, you were putting '<', 'p', '>', '\0' at the beginning on the szResult. You need not put the null there.

        I'm probably missing something but...

        >man strcat
        [snip] DESCRIPTION The strcat() function appends a copy of the string pointed to by the s2 parameter (including the terminating null byte) to the end of the string pointed to by the s1 parameter. The initial byte of s2 overwrites the null byte at the end of the string pointed to by s1. When operating on overlapping strings, the behavior of this function is unreliable.

        How does strcat know where to append the second string w/o a null byte in string 1 ?



        email: mandog

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (3)
As of 2024-04-25 20:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found