File tree 2 files changed +20
-0
lines changed
2 files changed +20
-0
lines changed Original file line number Diff line number Diff line change @@ -22,6 +22,12 @@ pub struct ada_string {
22
22
impl ada_string {
23
23
#[ must_use]
24
24
pub const fn as_str ( & self ) -> & ' static str {
25
+ // We need to handle length 0 since data will be `nullptr`
26
+ // Not handling will result in a panic due to core::slice::from_raw_parts
27
+ // implementation
28
+ if self . length == 0 {
29
+ return "" ;
30
+ }
25
31
unsafe {
26
32
let slice = core:: slice:: from_raw_parts ( self . data . cast ( ) , self . length ) ;
27
33
core:: str:: from_utf8_unchecked ( slice)
@@ -37,6 +43,12 @@ pub struct ada_owned_string {
37
43
38
44
impl AsRef < str > for ada_owned_string {
39
45
fn as_ref ( & self ) -> & str {
46
+ // We need to handle length 0 since data will be `nullptr`
47
+ // Not handling will result in a panic due to core::slice::from_raw_parts
48
+ // implementation
49
+ if self . length == 0 {
50
+ return "" ;
51
+ }
40
52
unsafe {
41
53
let slice = core:: slice:: from_raw_parts ( self . data . cast ( ) , self . length ) ;
42
54
core:: str:: from_utf8_unchecked ( slice)
Original file line number Diff line number Diff line change @@ -1079,4 +1079,12 @@ mod test {
1079
1079
assert_eq ! ( first. href( ) , "https://lemire.me/" ) ;
1080
1080
assert_eq ! ( second. href( ) , "https://yagiz.co/" ) ;
1081
1081
}
1082
+
1083
+ #[ test]
1084
+ fn should_handle_empty_host ( ) {
1085
+ // Ref: https://github.com/ada-url/rust/issues/74
1086
+ let url = Url :: parse ( "file:///C:/Users/User/Documents/example.pdf" , None ) . unwrap ( ) ;
1087
+ assert_eq ! ( url. host( ) , "" ) ;
1088
+ assert_eq ! ( url. hostname( ) , "" ) ;
1089
+ }
1082
1090
}
You can’t perform that action at this time.
0 commit comments