@@ -45,9 +45,95 @@ impl Authority {
4545 /// let authority = Authority::from_static("example.com");
4646 /// assert_eq!(authority.host(), "example.com");
4747 /// ```
48- pub fn from_static ( src : & ' static str ) -> Self {
49- Authority :: from_shared ( Bytes :: from_static ( src. as_bytes ( ) ) )
50- . expect ( "static str is not valid authority" )
48+ #[ inline]
49+ #[ allow( unconditional_panic) ]
50+ pub const fn from_static ( src : & ' static str ) -> Self {
51+ let bytes = src. as_bytes ( ) ;
52+
53+ if bytes. len ( ) == 0 {
54+ #[ allow( clippy:: no_effect, clippy:: out_of_bounds_indexing) ]
55+ ( [ ] as [ u8 ; 0 ] ) [ 0 ] ;
56+ }
57+
58+ let mut colon_cnt: u32 = 0 ;
59+ let mut start_bracket: bool = false ;
60+ let mut end_bracket: bool = false ;
61+ let mut has_percent: bool = false ;
62+ let mut at_sign_pos: usize = bytes. len ( ) ;
63+ const MAX_COLONS : u32 = 8 ;
64+
65+ let mut i: usize = 0 ;
66+ while i < bytes. len ( ) {
67+ let b = bytes[ i] ;
68+
69+ if b == b'/' || b == b'?' || b == b'#' {
70+ #[ allow( clippy:: no_effect, clippy:: out_of_bounds_indexing) ]
71+ ( [ ] as [ u8 ; 0 ] ) [ 0 ] ;
72+ }
73+
74+ let ch = URI_CHARS [ b as usize ] ;
75+ if ch == 0 {
76+ if b == b'%' {
77+ has_percent = true ;
78+ } else {
79+ #[ allow( clippy:: no_effect, clippy:: out_of_bounds_indexing) ]
80+ ( [ ] as [ u8 ; 0 ] ) [ 0 ] ;
81+ }
82+ } else if ch == b':' {
83+ if colon_cnt >= MAX_COLONS {
84+ #[ allow( clippy:: no_effect, clippy:: out_of_bounds_indexing) ]
85+ ( [ ] as [ u8 ; 0 ] ) [ 0 ] ;
86+ }
87+ colon_cnt += 1 ;
88+ } else if ch == b'[' {
89+ if has_percent || start_bracket {
90+ #[ allow( clippy:: no_effect, clippy:: out_of_bounds_indexing) ]
91+ ( [ ] as [ u8 ; 0 ] ) [ 0 ] ;
92+ }
93+ start_bracket = true ;
94+ } else if ch == b']' {
95+ if !start_bracket || end_bracket {
96+ #[ allow( clippy:: no_effect, clippy:: out_of_bounds_indexing) ]
97+ ( [ ] as [ u8 ; 0 ] ) [ 0 ] ;
98+ }
99+ end_bracket = true ;
100+
101+ // Forget IPv6 internals
102+ colon_cnt = 0 ;
103+ has_percent = false ;
104+ } else if ch == b'@' {
105+ at_sign_pos = i;
106+
107+ colon_cnt = 0 ;
108+ has_percent = false ;
109+ }
110+
111+ i += 1 ;
112+ }
113+
114+ if ( start_bracket && !end_bracket) || ( !start_bracket && end_bracket) {
115+ #[ allow( clippy:: no_effect, clippy:: out_of_bounds_indexing) ]
116+ ( [ ] as [ u8 ; 0 ] ) [ 0 ] ;
117+ }
118+
119+ if colon_cnt > 1 {
120+ #[ allow( clippy:: no_effect, clippy:: out_of_bounds_indexing) ]
121+ ( [ ] as [ u8 ; 0 ] ) [ 0 ] ;
122+ }
123+
124+ if bytes. len ( ) > 0 && at_sign_pos == bytes. len ( ) - 1 {
125+ #[ allow( clippy:: no_effect, clippy:: out_of_bounds_indexing) ]
126+ ( [ ] as [ u8 ; 0 ] ) [ 0 ] ;
127+ }
128+
129+ if has_percent {
130+ #[ allow( clippy:: no_effect, clippy:: out_of_bounds_indexing) ]
131+ ( [ ] as [ u8 ; 0 ] ) [ 0 ] ;
132+ }
133+
134+ Authority {
135+ data : ByteStr :: from_static ( src) ,
136+ }
51137 }
52138
53139 /// Attempt to convert a `Bytes` buffer to a `Authority`.
0 commit comments