File tree Expand file tree Collapse file tree 2 files changed +28
-1
lines changed Expand file tree Collapse file tree 2 files changed +28
-1
lines changed Original file line number Diff line number Diff line change 1
1
---
2
- refs/heads/master: 956bc773c684fe0f9e3ebc165cadb0b02d918805
2
+ refs/heads/master: 75cf13ec7279ff7a56e3329ab6d6d8fc03e66e4f
3
3
refs/heads/snap-stage1: e33de59e47c5076a89eadeb38f4934f58a3618a6
4
4
refs/heads/snap-stage3: 4a81779abd786ff22d71434c6d9a5917ea4cdfff
5
5
refs/heads/try: 2898dcc5d97da9427ac367542382b6239d9c0bbf
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