]> git.lizzy.rs Git - rust.git/blobdiff - src/libserialize/leb128.rs
Rollup merge of #67637 - Mark-Simulacrum:primitive-mod, r=dtolnay
[rust.git] / src / libserialize / leb128.rs
index 88ce6d81d75784c5829c48ab7e11eb63704e28a0..1fe6a309e9650a90f39f8cf639aff03538268e28 100644 (file)
@@ -1,40 +1,18 @@
-#[inline]
-pub fn write_to_vec(vec: &mut Vec<u8>, byte: u8) {
-    vec.push(byte);
-}
-
-#[cfg(target_pointer_width = "32")]
-const USIZE_LEB128_SIZE: usize = 5;
-#[cfg(target_pointer_width = "64")]
-const USIZE_LEB128_SIZE: usize = 10;
-
-macro_rules! leb128_size {
-    (u16) => (3);
-    (u32) => (5);
-    (u64) => (10);
-    (u128) => (19);
-    (usize) => (USIZE_LEB128_SIZE);
-}
-
 macro_rules! impl_write_unsigned_leb128 {
-    ($fn_name:ident, $int_ty:ident) => (
+    ($fn_name:ident, $int_ty:ident) => {
         #[inline]
         pub fn $fn_name(out: &mut Vec<u8>, mut value: $int_ty) {
-            for _ in 0 .. leb128_size!($int_ty) {
-                let mut byte = (value & 0x7F) as u8;
-                value >>= 7;
-                if value != 0 {
-                    byte |= 0x80;
-                }
-
-                write_to_vec(out, byte);
-
-                if value == 0 {
+            loop {
+                if value < 0x80 {
+                    out.push(value as u8);
                     break;
+                } else {
+                    out.push(((value & 0x7f) | 0x80) as u8);
+                    value >>= 7;
                 }
             }
         }
-    )
+    };
 }
 
 impl_write_unsigned_leb128!(write_u16_leb128, u16);
@@ -43,33 +21,26 @@ pub fn $fn_name(out: &mut Vec<u8>, mut value: $int_ty) {
 impl_write_unsigned_leb128!(write_u128_leb128, u128);
 impl_write_unsigned_leb128!(write_usize_leb128, usize);
 
-
 macro_rules! impl_read_unsigned_leb128 {
-    ($fn_name:ident, $int_ty:ident) => (
+    ($fn_name:ident, $int_ty:ident) => {
         #[inline]
         pub fn $fn_name(slice: &[u8]) -> ($int_ty, usize) {
-            let mut result: $int_ty = 0;
+            let mut result = 0;
             let mut shift = 0;
             let mut position = 0;
-
-            for _ in 0 .. leb128_size!($int_ty) {
-                let byte = unsafe {
-                    *slice.get_unchecked(position)
-                };
+            loop {
+                let byte = slice[position];
                 position += 1;
-                result |= ((byte & 0x7F) as $int_ty) << shift;
                 if (byte & 0x80) == 0 {
-                    break;
+                    result |= (byte as $int_ty) << shift;
+                    return (result, position);
+                } else {
+                    result |= ((byte & 0x7F) as $int_ty) << shift;
                 }
                 shift += 7;
             }
-
-            // Do a single bounds check at the end instead of for every byte.
-            assert!(position <= slice.len());
-
-            (result, position)
         }
-    )
+    };
 }
 
 impl_read_unsigned_leb128!(read_u16_leb128, u16);
@@ -78,8 +49,6 @@ pub fn $fn_name(slice: &[u8]) -> ($int_ty, usize) {
 impl_read_unsigned_leb128!(read_u128_leb128, u128);
 impl_read_unsigned_leb128!(read_usize_leb128, usize);
 
-
-
 #[inline]
 /// encodes an integer using signed leb128 encoding and stores
 /// the result using a callback function.
@@ -88,13 +57,14 @@ pub fn $fn_name(slice: &[u8]) -> ($int_ty, usize) {
 /// that is to be written to with the byte to be encoded
 /// at that position.
 pub fn write_signed_leb128_to<W>(mut value: i128, mut write: W)
-    where W: FnMut(u8)
+where
+    W: FnMut(u8),
 {
     loop {
         let mut byte = (value as u8) & 0x7f;
         value >>= 7;
-        let more = !(((value == 0) && ((byte & 0x40) == 0)) ||
-                     ((value == -1) && ((byte & 0x40) != 0)));
+        let more =
+            !(((value == 0) && ((byte & 0x40) == 0)) || ((value == -1) && ((byte & 0x40) != 0)));
 
         if more {
             byte |= 0x80; // Mark this byte to show that more bytes will follow.
@@ -110,7 +80,7 @@ pub fn write_signed_leb128_to<W>(mut value: i128, mut write: W)
 
 #[inline]
 pub fn write_signed_leb128(out: &mut Vec<u8>, value: i128) {
-    write_signed_leb128_to(value, |v| write_to_vec(out, v))
+    write_signed_leb128_to(value, |v| out.push(v))
 }
 
 #[inline]