@@ -282,45 +282,17 @@ impl FromIterator<KeyValueMetadata> for Baggage {
282
282
}
283
283
284
284
fn encode ( s : & str ) -> String {
285
- let special_characters = [ b'.' , b'-' , b'_' , b'~' ] ;
286
- let mut encoded_string = String :: with_capacity ( s. len ( ) * 3 ) ;
285
+ let mut encoded_string = String :: with_capacity ( s. len ( ) ) ;
287
286
288
- let bytes = s. as_bytes ( ) ;
289
- let mut i = 0 ;
290
-
291
- while i < bytes. len ( ) {
292
- let byte = bytes[ i] ;
293
-
294
- match byte {
295
- b' ' => encoded_string. push_str ( "%20" ) ,
296
- byte if byte. is_ascii_alphanumeric ( ) || special_characters. contains ( & byte) => {
297
- encoded_string. push ( byte as char )
298
- }
299
- _ => {
300
- if byte. is_ascii ( ) {
301
- encoded_string. push_str ( & format ! ( "%{:02X}" , byte) ) ;
302
- } else {
303
- let start = i;
304
- let mut end = start + 1 ;
305
-
306
- while end < bytes. len ( ) && !bytes[ end] . is_ascii ( ) {
307
- end += 1 ;
308
- }
309
-
310
- // Encoding each byte of the multi-byte character
311
- for & multi_byte in & bytes[ start..end] {
312
- encoded_string. push_str ( & format ! ( "%{:02X}" , multi_byte) ) ;
313
- }
314
-
315
- // Adjust `i` to skip over the bytes we've just encoded
316
- i = end - 1 ;
317
- }
287
+ for byte in s. as_bytes ( ) {
288
+ match * byte {
289
+ b'a' ..=b'z' | b'A' ..=b'Z' | b'0' ..=b'9' | b'.' | b'-' | b'_' | b'~' => {
290
+ encoded_string. push ( * byte as char )
318
291
}
292
+ b' ' => encoded_string. push_str ( "%20" ) ,
293
+ _ => encoded_string. push_str ( & format ! ( "%{:02X}" , byte) ) ,
319
294
}
320
-
321
- i += 1 ;
322
295
}
323
-
324
296
encoded_string
325
297
}
326
298
0 commit comments