forked from orazaro/accelerated
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path07-08-xref-urls.cpp
More file actions
119 lines (103 loc) · 2.98 KB
/
07-08-xref-urls.cpp
File metadata and controls
119 lines (103 loc) · 2.98 KB
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#include <iostream>
#include <vector>
#include <string>
#include <map>
#include <cctype>
#include <sstream>
bool not_url_char(char c)
{
static const std::string url_ch = "~;/?:@=&$-_.+!*'(),";
return !(
isalnum(c) ||
find(url_ch.begin(), url_ch.end(), c) != url_ch.end()
);
}
std::string::const_iterator
url_beg_book(std::string::const_iterator b, std::string::const_iterator e)
{
using namespace std;
static const string sep = "://";
typedef string::const_iterator iter;
iter i = b;
while((i = search(i, e, sep.begin(), sep.end())) != e) {
//make sure the separator isn't at the beginning or end of the line
if(i != b && i + sep.size() != e) {
iter beg = i;
while(beg != b && isalpha(beg[-1]))
--beg;
// is there at least one appropriate character
// before and after the separator?
if(beg != i && !not_url_char(i[sep.size()]))
return beg;
}
i += sep.size();
}
return e;
}
std::string::const_iterator
url_end(std::string::const_iterator b, std::string::const_iterator e)
{
return find_if(b, e, not_url_char);
}
std::vector<std::string> find_urls(const std::string& s)
{
using namespace std;
vector<string> ret;
typedef string::const_iterator iter;
iter b = s.begin(), e = s.end();
// look through the entire input
while(b != e) {
// look for one or more letters followed by ://
b = url_beg_book(b, e);
// if we found it
if(b != e) {
// get the rest of the URL
iter after = url_end(b, e);
ret.push_back(string(b, after));
b = after;
}
}
return ret;
}
typedef std::map<std::string, std::vector<int> > Xref;
Xref xref(std::istream& in,
std::vector<std::string> find_words(const std::string&) = find_urls)
{
using namespace std;
string line;
int line_number = 0;
Xref ret;
while(getline(in, line)) {
++line_number;
vector<string> words = find_words(line);
for(vector<string>::const_iterator it = words.begin();
it != words.end(); ++it) {
vector<int>& lines = ret[*it];
if(find(lines.begin(), lines.end(), line_number) == lines.end())
lines.push_back(line_number);
}
}
return ret;
}
int main()
{
using namespace std;
Xref ret = xref(cin);
// write results
for(Xref::const_iterator it = ret.begin();
it != ret.end(); ++it) {
stringstream ss;
pair<const string, vector<int> > p = *it;
ss << p.first << " : ";
vector<int>::const_iterator line_it = p.second.begin();
ss << *line_it; ++line_it;
while(line_it != p.second.end()) {
ss << ", "; ss << *line_it;
++line_it;
}
string out = ss.str();
if(out.size() >= 77) {out.resize(76); out+="...";}
cout << out << endl;
}
return 0;
}