| 407 | } |
| 408 | |
| 409 | fn normalize_encoding_name(encoding: &str) -> Cow<'_, str> { |
| 410 | // _Py_normalize_encoding: collapse non-alphanumeric/non-dot chars into |
| 411 | // single underscore, strip non-ASCII, lowercase ASCII letters. |
| 412 | let needs_transform = encoding |
| 413 | .bytes() |
| 414 | .any(|b| b.is_ascii_uppercase() || !b.is_ascii_alphanumeric() && b != b'.'); |
| 415 | if !needs_transform { |
| 416 | return encoding.into(); |
| 417 | } |
| 418 | let mut out = String::with_capacity(encoding.len()); |
| 419 | let mut punct = false; |
| 420 | for c in encoding.chars() { |
| 421 | if c.is_ascii_alphanumeric() || c == '.' { |
| 422 | if punct && !out.is_empty() { |
| 423 | out.push('_'); |
| 424 | } |
| 425 | out.push(c.to_ascii_lowercase()); |
| 426 | punct = false; |
| 427 | } else { |
| 428 | punct = true; |
| 429 | } |
| 430 | } |
| 431 | out.into() |
| 432 | } |
| 433 | |
| 434 | #[derive(Eq, PartialEq)] |
| 435 | enum StandardEncoding { |