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

C++ String Parsing

Name: Anonymous 2011-09-20 0:32

A little explanation before I get to my copypasta....Each line in an array (already populated), parseLinks is called to determine if at any point in that line a quotation mark is found as one of the characters. If it is, I want to begin copying from the next letter, until the letter before a closing quotation mark. From there, I want to determine if the string is in-fact a link, which is where the compare comes in. Links are passed back to main, whereas an empty string is passed back if it isn't a link. (and a counter I have in main isn't iterated, so it skips that one and tries to re-write in that array block...but that's irrelevant)

I'm guessing my code is fucked up for the detection of the quotation mark(s), because I'm not getting any values sent back in the string.


string parseLinks(string str)
{
    const int len = str.length();
    string link = "";
    bool quotes = false, islink = false;
    string compare[5] = {".htm",".html",".php",".asp",".pdf"};
   
    //Parse all quoted text
    for (int i = 0; i != len; i++)
    {
        //Change bool if quote found
        if (str[i] == '"')
        {
            if (quotes == false)
                quotes = true;
            else
                quotes = false;
        }

        //If bool true, and char is not a quote, add to link string
        if (quotes == true && str[i] != '"')
            link += str[i];
    }

    //Discard non-link text
    for (int i = 0; i < 5; i++)
    {
        if (link.compare(compare[i]) == 0)
            islink = true;
    }

    //If not a link, empty return string
    if (islink == false)
        link = "";

    return link;
}

Name: Anonymous 2011-09-20 1:49



    //Parse all quoted text
    int i, c=0;
    for (i = 0; i <= len; i++)
    {

    //>> bool true, and char is not a quote, add to link string
        if ((quotes == true) + ((int)str[i] != 34) == 2)
            c++;
            link[c] = str[i]; //??

        //Change bool if quote found
        if ((int)str[i] == 34)
            quotes = !quotes;
    }


doesn't look like it's broken though....

Name: Anonymous 2011-09-20 3:30

string::compare doesn't work like that

Name: Anonymous 2011-09-20 3:50

#include <string>
#include <list>
#include <algorithm>

void parseNigger(const std::string& nigger_in, std::list<std::string>& nigger_out)
{
    static const std::string compare[5] = {".htm",".html",".php",".asp",".pdf"};
    std::string::size_type nigglet1 = nigger_in.find('"'), nigglet2;

    while(nigglet1 != std::string::npos)
    {
        nigglet2 = nigger_in.find('"', nigglet1 + 1);
        if(nigglet2 == std::string::npos)
            break;
        std::string s = nigger_in.substr(nigglet1 + 1, nigglet2 - nigglet1 - 1);
        for(int nig = 0; nig < 5; ++nig)
        {
            if(std::search(s.begin(), s.end(), compare[nig].begin(), compare[nig].end()) != s.end())
                nigger_out.push_back(s);
        }
        nigglet1 = nigger_in.find('"', nigglet2 + 1);
    }
}

Name: Anonymous 2011-09-20 11:49

Nothing? Son I am disappoint.

Name: Anonymous 2011-09-20 11:53

Woops, browser refresh was made of fail, but I don't have time to look into your responses right now. I'll check back a bit later, thanks.

Name: Anonymous 2011-09-20 12:10

function parseLinks(str) {
   return str.match(/"(?:(?:https?|ftp|file):\/\/www\.|ftp\.)[-A-Z0-9+&#\/%=~_|$?!:,.]*[A-Z0-9+&#\/%=~_|$]"/gi);
}

Name: Anonymous 2011-09-20 12:27

or:

IHaveAnArrayThatsAlreadyPopulated.reduce(function(p, n, i) {
    p = p.concat(
            n.match(/"(?:(?:https?|ftp|file):\/\/www\.|ftp\.)[-A-Z0-9+&#\/%=~_|$?!:,.]*[A-Z0-9+&#\/%=~_|$]"/gi)
            .map(function(v) {
                return v.replace('"', '');
            })
        );
}, []);

Name: Anonymous 2011-09-20 13:19

>>7
Sorry, I'm not pro enough to understand how that works.

>>2
Yeah, that part works perfectly fine.

I've narrowed it down to the string compare. Minus that, it works fine, but I need the comparison in order to throw out strings that aren't links.

I tried the compare based on: http://www.cplusplus.com/reference/string/string/compare/

However, I used the wrong one. I would need the equivalent of:

  string str1 ("green apple");
  string str2 ("red apple");

  if (str1.compare(6,5,"apple") == 0)
    cout << str1 << " is an apple\n";

The thing I don't understand though, is what the 6 & 5 are for?

Name: Anonymous 2011-09-20 13:33

Ok, so it's position of the start of compared string, and length of compared string, so I'd be looking for something like:

string comp (".php");
link.compare((comp.len() - 4), 4, comp);

...or something along those lines

Name: Anonymous 2011-09-20 13:36

Badabing! Figured it out, here is the working compare code I got:

    //Discard non-link text
    for (int i = 0; i < 5; i++)
    {
        if (link.compare((link.length() - compare[i].length()),compare[i].length(),compare[i]) == 0)
            islink = true;
    }

Name: Anonymous 2011-09-20 19:28

>>11
Or you could have just looked at >>4.

Name: Anonymous 2011-09-20 19:29

>>5
Get out and take your stupid memes with you.

Name: Anonymous 2011-09-21 16:24

>>13

Welcome to the interweb
We got fun 'n' games
We got all the memes you want
Honey we know the memes
We are the people that can find
The Belair you may need
If you got the funny honey
We got your disease (AIDS)

Name: Anonymous 2011-09-21 16:26

>>12

True, but I hate when people have to type the namespace for every little fucking thing, instead of just listing the namespace and not having to do it everytime, because I can't be bothered to dig out that garbage

Name: Anonymous 2011-09-21 22:26

>>15
Look upon this man, /prog/. Look upon this man and weep for humanity.

Name: Anonymous 2011-09-21 23:05

>>16
I don't understand his post. Would you mind translating it to English?

Name: Anonymous 2011-09-22 2:46

What the hell are they teaching kids these days? Your algorithm is overly complicated and inefficient.

strchr your '"'. There's the start of the string you want. Then strchr for a second '"' from there and you got the end. The difference between the two, +1, is your length. Now you can use that to compare directly. None of that stupid bool bullshit, having to allocate memory for another string, and appending one character at a time to it.

Same here:

    for (int i = 0; i < 5; i++)
    {
        if (link.compare(compare[i]) == 0)
            islink = true;
    }

    //If not a link, empty return string
    if (islink == false)
        link = "";

    return link;

How about returning the damn string as soon as you've found it? Then if the loop finished you KNOW you haven't found one so you can just return the empty string right there. No need to keep checking something since islink isn't going back to false once you set it.

You say "it's only 5 iterations, whatever". No. This is a weakness in your thinking. Always think of the simplest algorithm you can, and if it isn't obvious to you, you need to think more. Programmers stuck in your mindset are the reason software is turning to shit today --- because they can't see the obvious solution and resort to something overly complex.

Name: Anonymous 2011-09-22 3:08

>>18
s/+1/-1/

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