mirror of
https://github.com/YGGverse/aquatic.git
synced 2026-03-31 17:55:36 +00:00
aquatic_ws: deserialize 20 byte strings: improve code
This commit is contained in:
parent
761952513f
commit
9030944c02
1 changed files with 16 additions and 16 deletions
|
|
@ -20,28 +20,28 @@ impl<'de> Visitor<'de> for TwentyByteVisitor {
|
|||
// var infoHash = 'abcd..'; // 40 hexadecimals
|
||||
// Buffer.from(infoHash, 'hex').toString('binary');
|
||||
// ```
|
||||
// which produces a UTF16 string with each char having only the low
|
||||
// byte set. Here, we extract it by casting to u8.
|
||||
// which seemingly produces a UTF16 string with each char having only
|
||||
// the "low byte" set. Here, we extract it by casting it to an u8.
|
||||
|
||||
let mut arr = [0u8; 20];
|
||||
let mut max_i = 0;
|
||||
let mut char_iter = value.chars();
|
||||
|
||||
for (i, (a, c)) in arr.iter_mut().zip(value.chars()).enumerate(){
|
||||
if c as u32 > 255 {
|
||||
return Err(E::custom(format!(
|
||||
"character not in single byte range: {}",
|
||||
c
|
||||
)))
|
||||
for a in arr.iter_mut(){
|
||||
if let Some(c) = char_iter.next(){
|
||||
if c as u32 > 255 {
|
||||
return Err(E::custom(format!(
|
||||
"character not in single byte range: {:#?}",
|
||||
c
|
||||
)));
|
||||
}
|
||||
|
||||
*a = c as u8;
|
||||
} else {
|
||||
return Err(E::custom(format!("not 20 bytes: {:#?}", value)));
|
||||
}
|
||||
*a = c as u8;
|
||||
max_i = i;
|
||||
}
|
||||
|
||||
if max_i == 19 {
|
||||
Ok(arr)
|
||||
} else {
|
||||
Err(E::custom(format!("not 20 bytes: {}", value)))
|
||||
}
|
||||
Ok(arr)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue