Skip to content

Commit 6318288

Browse files
committed
std: more work on from_c_multistring.. let it take an optional len param
1 parent 71c7798 commit 6318288

File tree

2 files changed

+18
-6
lines changed

2 files changed

+18
-6
lines changed

src/libstd/os.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ pub fn env() -> ~[(~str,~str)] {
196196
if (ch as uint == 0) {
197197
fail!("os::env() failure getting env string from OS: %s", os::last_os_error());
198198
}
199-
result = unsafe { str::raw::from_c_multistring(ch as *libc::c_char) };
199+
result = unsafe { str::raw::from_c_multistring(ch as *libc::c_char, None) };
200200
FreeEnvironmentStringsA(ch);
201201
result
202202
}

src/libstd/str.rs

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ use char;
2222
use char::Char;
2323
use clone::{Clone, DeepClone};
2424
use container::{Container, Mutable};
25+
use num::Times;
2526
use iter::{Iterator, FromIterator, Extendable, range};
2627
use iter::{Filter, AdditiveIterator, Map};
2728
use iter::{Invert, DoubleEndedIterator, ExactSize};
@@ -938,6 +939,7 @@ static TAG_CONT_U8: u8 = 128u8;
938939

939940
/// Unsafe operations
940941
pub 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

Comments
 (0)