eculocate

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

lib.rs (965B)


      1 #![no_std]
      2 
      3 use core::iter::Iterator;
      4 
      5 pub fn ptr_data<'b>(labels : &[&str], buf : &'b mut[u8]) -> &'b mut [u8] {
      6     let mut offset = 0;
      7     for l in labels.iter() {
      8 	buf[offset] = l.len() as u8;
      9 	for (i, &item) in l.as_bytes().iter().enumerate() {
     10 	    buf[offset + 1 + i] = item;
     11 	}
     12 	offset = offset + l.len() + 1;
     13     }
     14     buf[offset] = 0;
     15     &mut buf[..(offset+1)]
     16 }
     17 
     18 
     19 #[cfg(test)]
     20 mod tests {
     21     extern crate std;
     22     use super::*;
     23 
     24     #[test]
     25     fn test_ptr_data() {
     26 	let mut buf : [u8; 32] = [0; 32];
     27 	assert_eq!(ptr_data(&["hello"], &mut buf),
     28 		   [5, 104, 101, 108, 108, 111,  0   ]);
     29 
     30 	assert_eq!(ptr_data(&[], &mut buf),
     31 		   [0]);
     32 
     33     	assert_eq!(ptr_data(&["hello", "world"], &mut buf),
     34 		   [5, 104, 101, 108, 108, 111,
     35 		    5, 119, 111, 114, 108, 100,
     36 		   0]);
     37 
     38    	assert_eq!(ptr_data(&["hello", "cool", "world"], &mut buf),
     39 		   [5, 104, 101, 108, 108, 111,
     40 		    4, 99, 111, 111, 108,
     41 		    5, 119, 111, 114, 108, 100,
     42 		   0]);
     43     }
     44 }