File tree 1 file changed +24
-1
lines changed
1 file changed +24
-1
lines changed Original file line number Diff line number Diff line change @@ -37,6 +37,29 @@ impl Index<Range<Position>> for Url {
37
37
}
38
38
}
39
39
40
+ // Counts how many base-10 digits are required to represent n in the given base
41
+ fn count_digits ( n : u16 ) -> usize {
42
+ match n {
43
+ 0 ..=9 => 1 ,
44
+ 10 ..=99 => 2 ,
45
+ 100 ..=999 => 3 ,
46
+ 1000 ..=9999 => 4 ,
47
+ 10000 ..=65535 => 5 ,
48
+ }
49
+ }
50
+
51
+ #[ test]
52
+ fn test_count_digits ( ) {
53
+ assert_eq ! ( count_digits( 0 ) , 1 ) ;
54
+ assert_eq ! ( count_digits( 1 ) , 1 ) ;
55
+ assert_eq ! ( count_digits( 9 ) , 1 ) ;
56
+ assert_eq ! ( count_digits( 10 ) , 2 ) ;
57
+ assert_eq ! ( count_digits( 99 ) , 2 ) ;
58
+ assert_eq ! ( count_digits( 100 ) , 3 ) ;
59
+ assert_eq ! ( count_digits( 9999 ) , 4 ) ;
60
+ assert_eq ! ( count_digits( 65535 ) , 5 ) ;
61
+ }
62
+
40
63
/// Indicates a position within a URL based on its components.
41
64
///
42
65
/// A range of positions can be used for slicing `Url`:
@@ -152,7 +175,7 @@ impl Url {
152
175
Position :: AfterPort => {
153
176
if let Some ( port) = self . port {
154
177
debug_assert ! ( self . byte_at( self . host_end) == b':' ) ;
155
- self . host_end as usize + ":" . len ( ) + port . to_string ( ) . len ( )
178
+ self . host_end as usize + ":" . len ( ) + count_digits ( port )
156
179
} else {
157
180
self . host_end as usize
158
181
}
You can’t perform that action at this time.
0 commit comments