Skip to content

Commit ad357d0

Browse files
jgirardetyouknowone
authored andcommitted
normalize_encoding
1 parent f113342 commit ad357d0

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

vm/src/obj/objbyteinner.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,25 @@ pub struct ByteInnerNewOptions {
7474
encoding: OptionalArg<PyStringRef>,
7575
}
7676

77+
//same algorithm as cpython
78+
pub fn normalize_encoding(encoding: &str) -> String {
79+
let mut res = String::new();
80+
let mut punct = false;
81+
82+
for c in encoding.chars() {
83+
if c.is_alphanumeric() || c == '.' {
84+
if punct && !res.is_empty() {
85+
res.push('_')
86+
}
87+
res.push(c.to_ascii_lowercase());
88+
punct = false;
89+
} else {
90+
punct = true;
91+
}
92+
}
93+
res
94+
}
95+
7796
impl ByteInnerNewOptions {
7897
pub fn get_value(self, vm: &VirtualMachine) -> PyResult<PyByteInner> {
7998
// First handle bytes(string, encoding[, errors])

0 commit comments

Comments
 (0)