@@ -22,6 +22,7 @@ use char;
2222use char:: Char ;
2323use clone:: { Clone , DeepClone } ;
2424use container:: { Container , Mutable } ;
25+ use num:: Times ;
2526use iter:: { Iterator , FromIterator , Extendable , range} ;
2627use iter:: { Filter , AdditiveIterator , Map } ;
2728use iter:: { Invert , DoubleEndedIterator , ExactSize } ;
@@ -938,6 +939,7 @@ static TAG_CONT_U8: u8 = 128u8;
938939
939940/// Unsafe operations
940941pub mod raw {
942+ use option:: { Option , Some } ;
941943 use cast;
942944 use libc;
943945 use ptr;
@@ -1092,20 +1094,29 @@ pub mod raw {
10921094 }
10931095
10941096 /// Parses a C "multistring", eg windows env values or
1095- /// the req->ptr result in a uv_fs_readdir() call
1097+ /// the req->ptr result in a uv_fs_readdir() call.
1098+ /// Optionally, a `count` can be passed in, limiting the
1099+ /// parsing to only being done `count`-times.
10961100 #[ inline]
1097- pub unsafe fn from_c_multistring ( c : * libc:: c_char ) -> ~[ ~str ] {
1101+ pub unsafe fn from_c_multistring ( buf : * libc:: c_char , count : Option < uint > ) -> ~[ ~str ] {
10981102 #[ fixed_stack_segment] ; #[ inline( never) ] ;
10991103
1100- let mut curr_ptr: uint = c as uint ;
1104+ let mut curr_ptr: uint = buf as uint ;
11011105 let mut result = ~[ ] ;
1102- while ( * ( curr_ptr as * libc:: c_char ) != 0 as libc:: c_char ) {
1106+ let mut ctr = 0 ;
1107+ let ( limited_count, limit) = match count {
1108+ Some ( limit) => ( true , limit) ,
1109+ None => ( false , 0 )
1110+ } ;
1111+ while ( * ( curr_ptr as * libc:: c_char ) != 0 as libc:: c_char
1112+ && ( ( limited_count && ctr < limit) || !limited_count) ) {
11031113 let env_pair = from_c_str (
11041114 curr_ptr as * libc:: c_char ) ;
11051115 result. push ( env_pair) ;
11061116 curr_ptr +=
11071117 libc:: strlen ( curr_ptr as * libc:: c_char ) as uint
11081118 + 1 ;
1119+ ctr += 1 ;
11091120 }
11101121 result
11111122 }
@@ -1127,10 +1138,11 @@ pub mod raw {
11271138
11281139 #[test]
11291140 fn test_str_multistring_parsing() {
1141+ use option::None;
11301142 unsafe {
11311143 let input = bytes!(" zero", "\x00 " , "one" , "\x00 " , "\x00 " ) ;
11321144 let ptr = vec:: raw:: to_ptr ( input) ;
1133- let mut result = from_c_multistring ( ptr as * libc:: c_char ) ;
1145+ let mut result = from_c_multistring ( ptr as * libc:: c_char , None ) ;
11341146 assert ! ( result. len( ) == 2 ) ;
11351147 let mut ctr = 0 ;
11361148 for x in result. iter ( ) {
0 commit comments