language: C++ 4.7.2 (gcc-4.7.2)
date: 155 days 10 hours ago
link:
visibility: public
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <iostream>
#include <string>
 
typedef std::string::size_type pos_t;
 
std::string cleanLinks (const std::string & s)
{
    pos_t openBrackets = s.find("[[");
    // Set the result as anything before the first link.
    std::string result = s.substr(0,openBrackets);
    if ( std::string::npos == openBrackets )
        return result;
 
    // Search on from the first opening brackts to the closing brackets.
    pos_t closeBrackets = s.find("]]", openBrackets);
    if ( std::string::npos == closeBrackets )
        return result;
 
    // At this point, result contains anything up to the first link ("[[").
    // If there are no links, we won't get here.
 
    // Make a string consisting of just the (first) link.
    std::string linkStr = s.substr(openBrackets, closeBrackets-openBrackets+2);
 
    // If the link contains a |, split it out into text, and add that to
    // the result. Otherwise add the whole thing to the result unchanged.
    pos_t pipePos = linkStr.find("|");
    if ( std::string::npos == pipePos )
    {
        result += linkStr;
    }
    else
    {
        result += linkStr.substr(2, pipePos-2);
    }
 
    // Clean up any links in the rest of the string, then give it all back.
    return result + cleanLinks(s.substr(closeBrackets+2, std::string::npos));
}
 
int main()
{
    std::string inp =
        "This is a link [[abcd 1234|xyz 1234]]  "
        "[[India]] [[abcd 1234|xyz 1234]]";
    std::cout << '\"' << inp << "\"\n\"" << cleanLinks(inp) << "\"\n";
    return 0;
}