-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathurl.cpp
More file actions
72 lines (56 loc) · 1.73 KB
/
url.cpp
File metadata and controls
72 lines (56 loc) · 1.73 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
//
// Created by Lynx on 2025-05-13.
//
#include "url.h"
#include <string>
bool endsWith(const std::string &fullString, const std::string &ending) {
if (ending.size() > fullString.size())
return false;
return fullString.compare(fullString.size() - ending.size(), ending.size(), ending) == 0;
}
std::string toShortDomain(std::string domainFull, int nSubdomains) {
std::string domainShort;
int countedDots = 0;
for (int i = 0; i < domainFull.length(); i++) {
char c = domainFull[i];
if (countedDots >= nSubdomains)
domainShort += c;
if (c == '.')
countedDots++;
}
return domainShort;
}
void parseUrl(std::string url, std::string &dShort, std::string &dFull, std::string &uPath) {
// sft://
// https://sub.sld.tld/path -> my.net, /path
// a.co/b
std::string domainFull; // sub.sld.tld
std::string sld;
std::string tld;
std::string path;
int nSubdomains = -1;// because of the dot before TLD
bool isPath = false;
std::string urlIter; // iterate over URL string
for (int i = 0; i < url.length(); i++) {
char c = url[i];
if (c == '/') // WHEN / IS HIT
isPath = true;
if (endsWith(urlIter, "://")) { // END OF PROTOCOL
domainFull = "";
path = "";
isPath = false; // '://' looks like a path
}
if (isPath) // AFTER PATH
path += c;
else { // BEFORE PATH
domainFull += c;
if (c == '.') // COUNT DOTS
nSubdomains++;
}
urlIter += c;
}
path = path.empty() ? "/" : path;
dFull = domainFull;
dShort = toShortDomain(domainFull, nSubdomains);
uPath = path;
}