diff --git a/src/lib.rs b/src/lib.rs index 949c277..fd9515c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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 { diff --git a/tests/lib.rs b/tests/lib.rs index d5e634c..8ececcd 100644 --- a/tests/lib.rs +++ b/tests/lib.rs @@ -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); } }