@@ -79,6 +79,7 @@ use str;
79
79
use vec:: { ImmutableVector , MutableVector } ;
80
80
use vec;
81
81
use rt:: global_heap:: malloc_raw;
82
+ use unstable:: raw:: Slice ;
82
83
83
84
/// The representation of a C String.
84
85
///
@@ -169,6 +170,7 @@ impl CString {
169
170
}
170
171
171
172
/// Converts the CString into a `&[u8]` without copying.
173
+ /// Includes the terminating NUL byte.
172
174
///
173
175
/// # Failure
174
176
///
@@ -177,7 +179,21 @@ impl CString {
177
179
pub fn as_bytes < ' a > ( & ' a self ) -> & ' a [ u8 ] {
178
180
if self . buf . is_null ( ) { fail ! ( "CString is null!" ) ; }
179
181
unsafe {
180
- cast:: transmute ( ( self . buf , self . len ( ) + 1 ) )
182
+ cast:: transmute ( Slice { data : self . buf , len : self . len ( ) + 1 } )
183
+ }
184
+ }
185
+
186
+ /// Converts the CString into a `&[u8]` without copying.
187
+ /// Does not include the terminating NUL byte.
188
+ ///
189
+ /// # Failure
190
+ ///
191
+ /// Fails if the CString is null.
192
+ #[ inline]
193
+ pub fn as_bytes_no_nul < ' a > ( & ' a self ) -> & ' a [ u8 ] {
194
+ if self . buf . is_null ( ) { fail ! ( "CString is null!" ) ; }
195
+ unsafe {
196
+ cast:: transmute ( Slice { data : self . buf , len : self . len ( ) } )
181
197
}
182
198
}
183
199
@@ -189,8 +205,7 @@ impl CString {
189
205
/// Fails if the CString is null.
190
206
#[ inline]
191
207
pub fn as_str < ' a > ( & ' a self ) -> Option < & ' a str > {
192
- let buf = self . as_bytes ( ) ;
193
- let buf = buf. slice_to ( buf. len ( ) -1 ) ; // chop off the trailing NUL
208
+ let buf = self . as_bytes_no_nul ( ) ;
194
209
str:: from_utf8 ( buf)
195
210
}
196
211
@@ -417,7 +432,7 @@ mod tests {
417
432
let expected = [ "zero" , "one" ] ;
418
433
let mut it = expected. iter ( ) ;
419
434
let result = from_c_multistring ( ptr as * libc:: c_char , None , |c| {
420
- let cbytes = c. as_bytes ( ) . slice_to ( c . len ( ) ) ;
435
+ let cbytes = c. as_bytes_no_nul ( ) ;
421
436
assert_eq ! ( cbytes, it. next( ) . unwrap( ) . as_bytes( ) ) ;
422
437
} ) ;
423
438
assert_eq ! ( result, 2 ) ;
@@ -552,13 +567,31 @@ mod tests {
552
567
assert_eq ! ( c_str. as_bytes( ) , bytes!( "foo" , 0xff , 0 ) ) ;
553
568
}
554
569
570
+ #[ test]
571
+ fn test_as_bytes_no_nul ( ) {
572
+ let c_str = "hello" . to_c_str ( ) ;
573
+ assert_eq ! ( c_str. as_bytes_no_nul( ) , bytes!( "hello" ) ) ;
574
+ let c_str = "" . to_c_str ( ) ;
575
+ let exp: & [ u8 ] = [ ] ;
576
+ assert_eq ! ( c_str. as_bytes_no_nul( ) , exp) ;
577
+ let c_str = bytes ! ( "foo" , 0xff ) . to_c_str ( ) ;
578
+ assert_eq ! ( c_str. as_bytes_no_nul( ) , bytes!( "foo" , 0xff ) ) ;
579
+ }
580
+
555
581
#[ test]
556
582
#[ should_fail]
557
583
fn test_as_bytes_fail ( ) {
558
584
let c_str = unsafe { CString :: new ( ptr:: null ( ) , false ) } ;
559
585
c_str. as_bytes ( ) ;
560
586
}
561
587
588
+ #[ test]
589
+ #[ should_fail]
590
+ fn test_as_bytes_no_nul_fail ( ) {
591
+ let c_str = unsafe { CString :: new ( ptr:: null ( ) , false ) } ;
592
+ c_str. as_bytes_no_nul ( ) ;
593
+ }
594
+
562
595
#[ test]
563
596
fn test_as_str ( ) {
564
597
let c_str = "hello" . to_c_str ( ) ;
0 commit comments