Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 65d7c44

Browse files
optimizations in URL implementation (#1833)
1 parent 4d23017 commit 65d7c44

File tree

2 files changed

+21
-26
lines changed

2 files changed

+21
-26
lines changed

‎src/url.rs‎

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -384,53 +384,49 @@ impl PyMultiHostUrl {
384384
let host_offset = scheme.len() + 3;
385385

386386
let mut full_url = self.ref_url.unicode_string(py).into_owned();
387-
full_url.insert(host_offset,',');
387+
letmut extra_hosts = String::new();
388388

389389
// special urls will have had a trailing slash added, non-special urls will not
390390
// hence we need to remove the last char if the scheme is special
391391
#[allow(clippy::bool_to_int_with_if)]
392392
let sub = if scheme_is_special(scheme) { 1 } else { 0 };
393393

394-
let hosts = extra_urls
395-
.iter()
396-
.map(|url| {
397-
let str = unicode_url(url.as_str(), url);
398-
str[host_offset..str.len() - sub].to_string()
399-
})
400-
.collect::<Vec<String>>()
401-
.join(",");
402-
full_url.insert_str(host_offset, &hosts);
394+
for url in extra_urls {
395+
let str = unicode_url(url.as_str(), url);
396+
extra_hosts.push_str(&str[host_offset..str.len() - sub]);
397+
extra_hosts.push(',');
398+
}
399+
400+
full_url.insert_str(host_offset, &extra_hosts);
403401
Cow::Owned(full_url)
404402
} else {
405403
self.ref_url.unicode_string(py)
406404
}
407405
}
408406

409-
pub fn __str__(&self, py: Python<'_>) -> String {
407+
pub fn __str__(&self, py: Python<'_>) -> Cow<'_,str> {
410408
if let Some(extra_urls) = &self.extra_urls {
411409
let scheme = self.ref_url.lib_url.scheme();
412410
let host_offset = scheme.len() + 3;
413411

414412
let mut full_url = self.ref_url.serialized(py).to_string();
415-
full_url.insert(host_offset,',');
413+
letmut extra_hosts = String::new();
416414

417415
// special urls will have had a trailing slash added, non-special urls will not
418416
// hence we need to remove the last char if the scheme is special
419417
#[allow(clippy::bool_to_int_with_if)]
420418
let sub = if scheme_is_special(scheme) { 1 } else { 0 };
421419

422-
let hosts = extra_urls
423-
.iter()
424-
.map(|url| {
425-
let str = url.as_str();
426-
&str[host_offset..str.len() - sub]
427-
})
428-
.collect::<Vec<&str>>()
429-
.join(",");
430-
full_url.insert_str(host_offset, &hosts);
431-
full_url
420+
for url in extra_urls {
421+
let str = url.as_str();
422+
extra_hosts.push_str(&str[host_offset..str.len() - sub]);
423+
extra_hosts.push(',');
424+
}
425+
426+
full_url.insert_str(host_offset, &extra_hosts);
427+
Cow::Owned(full_url)
432428
} else {
433-
self.ref_url.__str__(py).to_string()
429+
Cow::Borrowed(self.ref_url.__str__(py))
434430
}
435431
}
436432

@@ -463,7 +459,7 @@ impl PyMultiHostUrl {
463459
self.clone().into_py_any(py)
464460
}
465461

466-
fn __getnewargs__(&self, py: Python<'_>) -> (String,) {
462+
fn __getnewargs__(&self, py: Python<'_>) -> (Cow<'_,str>,) {
467463
(self.__str__(py),)
468464
}
469465

‎src/validators/url.rs‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use std::borrow::Cow;
21
use std::cell::RefCell;
32
use std::iter::Peekable;
43
use std::str::Chars;
@@ -214,7 +213,7 @@ impl UrlValidator {
214213

215214
let either_str_owned;
216215
let url_str = if let Some(multi_host_url) = downcast_python_input::<PyMultiHostUrl>(input) {
217-
Cow::Owned(multi_host_url.get().__str__(py))
216+
multi_host_url.get().__str__(py)
218217
} else if let Ok(either_str) = input.validate_str(strict, false).map(ValidationMatch::into_inner) {
219218
either_str_owned = either_str; // to extend the lifetime outside the if let
220219
either_str_owned.as_cow()?

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /