Skip to content

Commit e053bff

Browse files
committed
std: Fix c_str.iter() and add test
1 parent 5eaa4d1 commit e053bff

File tree

1 file changed

+20
-3
lines changed

1 file changed

+20
-3
lines changed

src/libstd/c_str.rs

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -148,10 +148,10 @@ pub struct CStringIterator<'self> {
148148

149149
impl<'self> Iterator<libc::c_char> for CStringIterator<'self> {
150150
fn next(&mut self) -> Option<libc::c_char> {
151-
if self.ptr.is_null() {
151+
let ch = unsafe { *self.ptr };
152+
if ch == 0 {
152153
None
153154
} else {
154-
let ch = unsafe { *self.ptr };
155155
self.ptr = ptr::offset(self.ptr, 1);
156156
Some(ch)
157157
}
@@ -163,6 +163,7 @@ mod tests {
163163
use super::*;
164164
use libc;
165165
use ptr;
166+
use option::{Some, None};
166167

167168
#[test]
168169
fn test_to_c_str() {
@@ -210,7 +211,23 @@ mod tests {
210211
#[should_fail]
211212
#[ignore(cfg(windows))]
212213
fn test_with_ref_empty_fail() {
213-
let c_str = CString::new(ptr::null(), false);
214+
let c_str = unsafe { CString::new(ptr::null(), false) };
214215
c_str.with_ref(|_| ());
215216
}
217+
218+
#[test]
219+
fn test_iterator() {
220+
let c_str = "".to_c_str();
221+
let mut iter = c_str.iter();
222+
assert_eq!(iter.next(), None);
223+
224+
let c_str = "hello".to_c_str();
225+
let mut iter = c_str.iter();
226+
assert_eq!(iter.next(), Some('h' as libc::c_char));
227+
assert_eq!(iter.next(), Some('e' as libc::c_char));
228+
assert_eq!(iter.next(), Some('l' as libc::c_char));
229+
assert_eq!(iter.next(), Some('l' as libc::c_char));
230+
assert_eq!(iter.next(), Some('o' as libc::c_char));
231+
assert_eq!(iter.next(), None);
232+
}
216233
}

0 commit comments

Comments
 (0)