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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
use crate::url::Parser;
pub struct Utils;
impl Utils {
pub fn substring_after_scheme<'a>(parser: &Parser, input: &'a str) -> &'a str {
let scheme = parser.mixout_scheme(input);
let double_slash_length = 2;
match scheme.clone() {
Some(v) => input.get(v.len() + double_slash_length + 1..).unwrap(),
None => input,
}
}
pub fn substring_after_login<'a>(parser: &Parser, input: &'a str) -> &'a str {
let input = Utils::substring_after_scheme(&parser, input);
match input.find("@") {
Some(pos) => &input[pos + 1..],
None => input,
}
}
pub fn substring_after_port<'a>(parser: &Parser, input: &'a str) -> &'a str {
let input = Utils::substring_after_scheme(&parser, input);
let port = parser.mixout_port(input);
if input.find(":").is_some() {
let (pos_port, len_port_string) = match port {
Some(v) => (input.find(&v.to_string()).unwrap(), v.to_string().len() + 1),
None => (0, 0),
};
return input.get(pos_port + len_port_string..).unwrap();
}
return input;
}
pub fn substring_before_port<'a>(parser: &Parser, input: &'a str) -> &'a str {
let port = parser.mixout_port(input);
let pos_port = match port {
Some(v) => input.find(&v.to_string()).unwrap() - 1,
None => input.len(),
};
return input.get(..pos_port).unwrap();
}
pub fn substring_from_path_begin<'a>(parser: &Parser, input: &'a str) -> &'a str {
let input = Utils::substring_after_scheme(&parser, input);
match input.find("/") {
Some(pos) => &input[pos..],
None => input,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_substring_after_scheme_works_when_typical() {
let input =
"https://user:pass@www.example.co.uk:443/blog/article/search?docid=720&hl=en#dayone";
let expected = "user:pass@www.example.co.uk:443/blog/article/search?docid=720&hl=en#dayone"
.to_string();
let parser = Parser::new(None);
let result = Utils::substring_after_scheme(&parser, input);
assert_eq!(result, expected);
}
#[test]
fn test_substring_after_port_works_when_typical() {
let input = "https://www.example.co.uk:443/blog/article/search?docid=720&hl=en#dayone";
let expected = "blog/article/search?docid=720&hl=en#dayone".to_string();
let parser = Parser::new(None);
let result = Utils::substring_after_port(&parser, input);
assert_eq!(result, expected);
}
#[test]
fn test_substring_after_port_works_when_no_scheme() {
let input = "user:pass@www.example.co.uk:443/blog/article/search?docid=720&hl=en#dayone";
let expected = "blog/article/search?docid=720&hl=en#dayone".to_string();
let parser = Parser::new(None);
let result = Utils::substring_after_port(&parser, input);
assert_eq!(result, expected);
}
#[test]
fn test_substring_before_port_works_when_typical() {
let input = "https://www.example.co.uk:443/blog/article/search?docid=720&hl=en#dayone";
let expected = "https://www.example.co.uk".to_string();
let parser = Parser::new(None);
let result = Utils::substring_before_port(&parser, input);
assert_eq!(result, expected);
}
#[test]
fn test_substring_after_login_works_when_typical() {
let input =
"https://user:pass@www.example.co.uk:443/blog/article/search?docid=720&hl=en#dayone";
let expected =
"www.example.co.uk:443/blog/article/search?docid=720&hl=en#dayone".to_string();
let parser = Parser::new(None);
let result = Utils::substring_after_login(&parser, input);
assert_eq!(result, expected);
}
#[test]
fn test_substring_from_path_begin_works_when_typical() {
let input = "https://www.example.co.uk:443/blog/article/search?docid=720&hl=en#dayone";
let expected = "/blog/article/search?docid=720&hl=en#dayone".to_string();
let parser = Parser::new(None);
let result = Utils::substring_from_path_begin(&parser, input);
assert_eq!(result, expected);
}
#[test]
fn test_substring_from_path_begin_works_when_no_port() {
let input = "https://www.example.co.uk/blog/article/search?docid=720&hl=en#dayone";
let expected = "/blog/article/search?docid=720&hl=en#dayone".to_string();
let parser = Parser::new(None);
let result = Utils::substring_from_path_begin(&parser, input);
assert_eq!(result, expected);
}
#[test]
fn test_substring_after_port_works_when_colon_in_url() {
let input = "http://en.wikipedia.org/wiki/Template:Welcome";
let expected = "en.wikipedia.org/wiki/Template:Welcome".to_string();
let parser = Parser::new(None);
let result = Utils::substring_after_port(&parser, input);
assert_eq!(result, expected);
}
}