Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

C++ String Compare

Name: Anonymous 2011-09-21 16:58

Sup niggaz. This shit is throwing an exception when it gets to this code in runtime. Any ideas?

- Assume "in" is a string comprised of "http://google.com/derp"
- found is a link found, can be w/e string

As far as I can tell, it explodes on the line where I'm trying to compare the 2 strings, as for some reason the loop is not concatenating the compare string - any idea why that is?


bool isLinkExternal(string in, string found)
{
    string compare = "";
    bool isExternal = false;

    //Get domain of original link
    for (int i = 6; in[i] != '/' ; i++)
    {
        compare += in[i];
    }
   
    //Compare domains of original and found links
    if (found.compare(7,compare.length(),compare) == 0)
        isExternal = true;

    return isExternal;
}


The way I see it, the loop starts at 7 chars in, which excludes the initial "http://". It should then keep going, concatenating each char into the compare string until a slash is found. The last bit is to compare that string to the portion of the string "in" of the same length, starting at the same point (7 chars in, for length of compare)

Name: Anonymous 2011-09-22 2:33

>>11,12
holy shit enterprise bloat

>>16

if (in.compare(0,comp.length(),comp) == 0)
  return false;
else
  return true;


Did you just finish CS101?

You do not need to do ANY copying of data around whatsoever. The solution would look something like this (not tested, written in 30 seconds, no warranties expressed or implied, etc.)

int isLinkExternal(char *in, char *found) {
 return !strncmp(in+7,found+7,strchr(in+7,'/')-in);
}


...and people wonder why their goddamn browser is so slow.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List