File tree 1 file changed +27
-0
lines changed
1 file changed +27
-0
lines changed Original file line number Diff line number Diff line change @@ -1539,6 +1539,21 @@ fn as_c_str<T>(s: str, f: fn(*libc::c_char) -> T) -> T unsafe {
1539
1539
as_buf ( s) { |buf| f ( buf as * libc:: c_char ) }
1540
1540
}
1541
1541
1542
+
1543
+ #[ doc = "
1544
+ Work with the byte buffer and length of a slice.
1545
+
1546
+ The unpacked length is one byte longer than the 'official' indexable
1547
+ length of the string. This is to permit probing the byte past the
1548
+ indexable area for a null byte, as is the case in slices pointing
1549
+ to full strings, or suffixes of them.
1550
+ " ]
1551
+ fn unpack_slice < T > ( s : str /& , f : fn ( * u8 , uint ) -> T ) -> T unsafe {
1552
+ let v : * ( * u8 , uint ) = :: unsafe:: reinterpret_cast ( ptr:: addr_of ( s) ) ;
1553
+ let ( buf, len) = * v;
1554
+ f ( buf, len)
1555
+ }
1556
+
1542
1557
#[ doc = "
1543
1558
Reserves capacity for exactly `n` bytes in the given string, not including
1544
1559
the null terminator.
@@ -2706,4 +2721,16 @@ mod tests {
2706
2721
}
2707
2722
assert found_b;
2708
2723
}
2724
+
2725
+ #[ test]
2726
+ fn test_unpack_slice ( ) unsafe {
2727
+ let a = "hello" ;
2728
+ unpack_slice ( a) { |buf, len|
2729
+ assert a[ 0 ] == 'h' as u8 ;
2730
+ assert * buf == 'h' as u8 ;
2731
+ assert len == 6 u;
2732
+ assert * ptr:: offset ( buf, 4 u) == 'o' as u8 ;
2733
+ assert * ptr:: offset ( buf, 5 u) == 0u8 ;
2734
+ }
2735
+ }
2709
2736
}
You can’t perform that action at this time.
0 commit comments