Преглед на файлове

Avoid string allocation to get length of port (#823)

Quentin Santos преди 3 години
родител
ревизия
1092960635
променени са 1 файла, в които са добавени 24 реда и са изтрити 1 реда
  1. 24 1
      url/src/slicing.rs

+ 24 - 1
url/src/slicing.rs

@@ -37,6 +37,29 @@ impl Index<Range<Position>> for Url {
     }
     }
 }
 }
 
 
+// Counts how many base-10 digits are required to represent n in the given base
+fn count_digits(n: u16) -> usize {
+    match n {
+        0..=9 => 1,
+        10..=99 => 2,
+        100..=999 => 3,
+        1000..=9999 => 4,
+        10000..=65535 => 5,
+    }
+}
+
+#[test]
+fn test_count_digits() {
+    assert_eq!(count_digits(0), 1);
+    assert_eq!(count_digits(1), 1);
+    assert_eq!(count_digits(9), 1);
+    assert_eq!(count_digits(10), 2);
+    assert_eq!(count_digits(99), 2);
+    assert_eq!(count_digits(100), 3);
+    assert_eq!(count_digits(9999), 4);
+    assert_eq!(count_digits(65535), 5);
+}
+
 /// Indicates a position within a URL based on its components.
 /// Indicates a position within a URL based on its components.
 ///
 ///
 /// A range of positions can be used for slicing `Url`:
 /// A range of positions can be used for slicing `Url`:
@@ -152,7 +175,7 @@ impl Url {
             Position::AfterPort => {
             Position::AfterPort => {
                 if let Some(port) = self.port {
                 if let Some(port) = self.port {
                     debug_assert!(self.byte_at(self.host_end) == b':');
                     debug_assert!(self.byte_at(self.host_end) == b':');
-                    self.host_end as usize + ":".len() + port.to_string().len()
+                    self.host_end as usize + ":".len() + count_digits(port)
                 } else {
                 } else {
                     self.host_end as usize
                     self.host_end as usize
                 }
                 }