Skip to content

Commit 5f2cc10

Browse files
committed
simply encode logic
1 parent 82f0108 commit 5f2cc10

File tree

1 file changed

+7
-35
lines changed

1 file changed

+7
-35
lines changed

opentelemetry/src/baggage.rs

+7-35
Original file line numberDiff line numberDiff line change
@@ -282,45 +282,17 @@ impl FromIterator<KeyValueMetadata> for Baggage {
282282
}
283283

284284
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());
287286

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)
318291
}
292+
b' ' => encoded_string.push_str("%20"),
293+
_ => encoded_string.push_str(&format!("%{:02X}", byte)),
319294
}
320-
321-
i += 1;
322295
}
323-
324296
encoded_string
325297
}
326298

0 commit comments

Comments
 (0)