Skip to content

Commit

Permalink
unsigned var long fix
Browse files Browse the repository at this point in the history
  • Loading branch information
ismaileke committed Sep 17, 2024
1 parent 6bb7ee9 commit ba28e2b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 33 deletions.
29 changes: 10 additions & 19 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -404,34 +404,25 @@ pub mod binary {
}

pub fn get_unsigned_var_long(&mut self) -> u64 {
let bytes = self.get(10);

match bytes {
Ok(byte) => {
let mut value = 0u64;
for i in 0..10 {
let b = byte[i];
value |= ((b & 0x7f) as u64) << (i * 7);
if b & 0x80 == 0 {
return value;
}
}
0
}
Err(err) => {
println!("Error get_unsigned_var_long(): {}", err);
0
let mut value = 0u64;
for i in 0..10 {
let b = self.get_byte();
value |= ((b & 0x7f) as u64) << (i * 7);
if b & 0x80 == 0 {
return value;
}
}
0
}

pub fn put_unsigned_var_long(&mut self, mut value: u64) {
let mut buf = Vec::new();
while value > 0x7f {
buf.push((value & 0x7f) as u8 | 0x80);
while value >= 0x80 {
buf.push((value as u8) | 0x80);
value >>= 7;
}
buf.push(value as u8);
self.put(buf);
}

pub fn get_var_long(&mut self) -> i64 {
Expand Down
19 changes: 5 additions & 14 deletions tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,12 @@ mod tests {

#[test]
fn test() {
let mut stream = Stream::new(vec![0, 0, 40, 76, 111, 103, 105, 110, 32, 116, 105, 109, 101, 111, 117, 116, 32, 40, 69, 114, 114, 111, 114, 32, 73, 68, 58, 32, 100, 100, 54, 48, 45, 99, 54, 51, 100, 45, 99, 56, 53, 97, 41, 0], 0);
let mut stream = Stream::new(vec![], 0);

let reason = stream.get_var_int();//bunda da sıkıntı var gibi?
let skip_message = stream.get_bool();
println!("Reason: {}", reason);
println!("Skip Msg: {}", skip_message);
if skip_message {
let mut length = stream.get_unsigned_var_int();
let message = String::from_utf8(stream.get(length).unwrap()).unwrap();
length = stream.get_unsigned_var_int();
let filtered_message = String::from_utf8(stream.get(length).unwrap()).unwrap();
println!("message: {}", message);
println!("filtered_message: {}", filtered_message);

}
stream.put_unsigned_var_long(1234456);
println!("stream: {:?}", stream.get_buffer());
let result = stream.get_unsigned_var_long();
println!("result: {}", result);

}
}

0 comments on commit ba28e2b

Please sign in to comment.