MCPcopy Index your code
hub / github.com/RustPython/RustPython / normalize_encoding_name

Function normalize_encoding_name

crates/vm/src/codecs.rs:409–432  ·  view source on GitHub ↗
(encoding: &str)

Source from the content-addressed store, hash-verified

407}
408
409fn 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)]
435enum StandardEncoding {

Callers 3

register_manualMethod · 0.85
lookupMethod · 0.85
forgetMethod · 0.85

Calls 6

charsMethod · 0.80
to_ascii_lowercaseMethod · 0.80
bytesMethod · 0.45
lenMethod · 0.45
is_emptyMethod · 0.45
pushMethod · 0.45

Tested by

no test coverage detected