@@ -61,7 +61,7 @@ impl String {
61
61
#[ inline]
62
62
#[ deprecated = "Replaced by string::raw::from_parts" ]
63
63
pub unsafe fn from_raw_parts ( length : uint , capacity : uint , ptr : * mut u8 ) -> String {
64
- raw:: from_parts ( length , capacity , ptr )
64
+ raw:: from_parts ( ptr , length , capacity )
65
65
}
66
66
67
67
#[ allow( missing_doc) ]
@@ -571,6 +571,7 @@ impl<S: Str> Add<S, String> for String {
571
571
572
572
pub mod raw {
573
573
use core:: mem;
574
+ use core:: ptr:: RawPtr ;
574
575
use core:: raw:: Slice ;
575
576
576
577
use super :: String ;
@@ -582,21 +583,13 @@ pub mod raw {
582
583
/// * We call `Vec::from_raw_parts` to get a `Vec<u8>`
583
584
/// * We assume that the `Vec` contains valid UTF-8
584
585
#[ inline]
585
- pub unsafe fn from_parts ( length : uint , capacity : uint , ptr : * mut u8 ) -> String {
586
+ pub unsafe fn from_parts ( buf : * mut u8 , length : uint , capacity : uint ) -> String {
586
587
String {
587
- vec : Vec :: from_raw_parts ( length, capacity, ptr ) ,
588
+ vec : Vec :: from_raw_parts ( length, capacity, buf ) ,
588
589
}
589
590
}
590
591
591
- /// Converts a vector of bytes to a new `String` without checking if
592
- /// it contains valid UTF-8. This is unsafe because it assumes that
593
- /// the utf-8-ness of the vector has already been validated.
594
- #[ inline]
595
- pub unsafe fn from_utf8 ( bytes : Vec < u8 > ) -> String {
596
- String { vec : bytes }
597
- }
598
-
599
- /// Create a Rust string from a *u8 buffer of the given length
592
+ /// Create `String` from a *u8 buffer of the given length
600
593
///
601
594
/// This function is unsafe because of two reasons:
602
595
/// * A raw pointer is dereferenced and transmuted to `&[u8]`
@@ -609,6 +602,27 @@ pub mod raw {
609
602
} ) ;
610
603
self :: from_utf8 ( slice. to_vec ( ) )
611
604
}
605
+
606
+ /// Create a `String` from a null-terminated *u8 buffer
607
+ ///
608
+ /// This function is unsafe because we dereference memory until we find the NUL character,
609
+ /// which is not guaranteed to be present. Additionaly, the slice is not checked to see
610
+ /// whether it contains valid UTF-8
611
+ pub unsafe fn from_buf ( buf : * const u8 ) -> String {
612
+ let mut len = 0 ;
613
+ while * buf. offset ( len) != 0 {
614
+ len += 1 ;
615
+ }
616
+ self :: from_buf_len ( buf, len as uint )
617
+ }
618
+
619
+ /// Converts a vector of bytes to a new `String` without checking if
620
+ /// it contains valid UTF-8. This is unsafe because it assumes that
621
+ /// the utf-8-ness of the vector has already been validated.
622
+ #[ inline]
623
+ pub unsafe fn from_utf8 ( bytes : Vec < u8 > ) -> String {
624
+ String { vec : bytes }
625
+ }
612
626
}
613
627
614
628
#[ cfg( test) ]
@@ -776,6 +790,16 @@ mod tests {
776
790
}
777
791
}
778
792
793
+ #[ test]
794
+ fn test_from_buf ( ) {
795
+ unsafe {
796
+ let a = vec ! [ 65 , 65 , 65 , 65 , 65 , 65 , 65 , 0 ] ;
797
+ let b = a. as_ptr ( ) ;
798
+ let c = super :: raw:: from_buf ( b) ;
799
+ assert_eq ! ( c, String :: from_str( "AAAAAAA" ) ) ;
800
+ }
801
+ }
802
+
779
803
#[ test]
780
804
fn test_push_bytes ( ) {
781
805
let mut s = String :: from_str ( "ABC" ) ;
0 commit comments