@@ -22,6 +22,7 @@ use char;
22
22
use char:: Char ;
23
23
use clone:: { Clone , DeepClone } ;
24
24
use container:: { Container , Mutable } ;
25
+ use num:: Times ;
25
26
use iter:: { Iterator , FromIterator , Extendable , range} ;
26
27
use iter:: { Filter , AdditiveIterator , Map } ;
27
28
use iter:: { Invert , DoubleEndedIterator , ExactSize } ;
@@ -938,6 +939,7 @@ static TAG_CONT_U8: u8 = 128u8;
938
939
939
940
/// Unsafe operations
940
941
pub mod raw {
942
+ use option:: { Option , Some } ;
941
943
use cast;
942
944
use libc;
943
945
use ptr;
@@ -1092,20 +1094,29 @@ pub mod raw {
1092
1094
}
1093
1095
1094
1096
/// 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.
1096
1100
#[ 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 ] {
1098
1102
#[ fixed_stack_segment] ; #[ inline( never) ] ;
1099
1103
1100
- let mut curr_ptr: uint = c as uint ;
1104
+ let mut curr_ptr: uint = buf as uint ;
1101
1105
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) ) {
1103
1113
let env_pair = from_c_str (
1104
1114
curr_ptr as * libc:: c_char ) ;
1105
1115
result. push ( env_pair) ;
1106
1116
curr_ptr +=
1107
1117
libc:: strlen ( curr_ptr as * libc:: c_char ) as uint
1108
1118
+ 1 ;
1119
+ ctr += 1 ;
1109
1120
}
1110
1121
result
1111
1122
}
@@ -1127,10 +1138,11 @@ pub mod raw {
1127
1138
1128
1139
#[test]
1129
1140
fn test_str_multistring_parsing() {
1141
+ use option::None;
1130
1142
unsafe {
1131
1143
let input = bytes!(" zero", "\x00 " , "one" , "\x00 " , "\x00 " ) ;
1132
1144
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 ) ;
1134
1146
assert ! ( result. len( ) == 2 ) ;
1135
1147
let mut ctr = 0 ;
1136
1148
for x in result. iter ( ) {
0 commit comments