2 use crate::borrow::Cow::{Borrowed, Owned};
3 use crate::collections::hash_map::DefaultHasher;
4 use crate::hash::{Hash, Hasher};
5 use crate::os::raw::c_char;
12 let ptr = data.as_ptr() as *const c_char;
14 assert_eq!(CStr::from_ptr(ptr).to_bytes(), b"123");
15 assert_eq!(CStr::from_ptr(ptr).to_bytes_with_nul(), b"123\0");
21 let s = CString::new("1234").unwrap();
22 assert_eq!(s.as_bytes(), b"1234");
23 assert_eq!(s.as_bytes_with_nul(), b"1234\0");
27 fn build_with_zero1() {
28 assert!(CString::new(&b"\0"[..]).is_err());
31 fn build_with_zero2() {
32 assert!(CString::new(vec![0]).is_err());
36 fn build_with_zero3() {
38 let s = CString::from_vec_unchecked(vec![0]);
39 assert_eq!(s.as_bytes(), b"\0");
45 let s = CString::new(&b"abc\x01\x02\n\xE2\x80\xA6\xFF"[..]).unwrap();
46 assert_eq!(format!("{:?}", s), r#""abc\x01\x02\n\xe2\x80\xa6\xff""#);
52 let s = CStr::from_ptr(b"12\0".as_ptr() as *const _);
53 assert_eq!(s.to_bytes(), b"12");
54 assert_eq!(s.to_bytes_with_nul(), b"12\0");
60 let data = b"123\xE2\x80\xA6\0";
61 let ptr = data.as_ptr() as *const c_char;
63 assert_eq!(CStr::from_ptr(ptr).to_str(), Ok("123…"));
64 assert_eq!(CStr::from_ptr(ptr).to_string_lossy(), Borrowed("123…"));
66 let data = b"123\xE2\0";
67 let ptr = data.as_ptr() as *const c_char;
69 assert!(CStr::from_ptr(ptr).to_str().is_err());
70 assert_eq!(CStr::from_ptr(ptr).to_string_lossy(), Owned::<str>(format!("123\u{FFFD}")));
77 let ptr = data.as_ptr() as *const c_char;
79 let owned = unsafe { CStr::from_ptr(ptr).to_owned() };
80 assert_eq!(owned.as_bytes_with_nul(), data);
85 let data = b"123\xE2\xFA\xA6\0";
86 let ptr = data.as_ptr() as *const c_char;
87 let cstr: &'static CStr = unsafe { CStr::from_ptr(ptr) };
89 let mut s = DefaultHasher::new();
91 let cstr_hash = s.finish();
92 let mut s = DefaultHasher::new();
93 CString::new(&data[..data.len() - 1]).unwrap().hash(&mut s);
94 let cstring_hash = s.finish();
96 assert_eq!(cstr_hash, cstring_hash);
100 fn from_bytes_with_nul() {
102 let cstr = CStr::from_bytes_with_nul(data);
103 assert_eq!(cstr.map(CStr::to_bytes), Ok(&b"123"[..]));
104 let cstr = CStr::from_bytes_with_nul(data);
105 assert_eq!(cstr.map(CStr::to_bytes_with_nul), Ok(&b"123\0"[..]));
108 let cstr = CStr::from_bytes_with_nul(data);
109 let cstr_unchecked = CStr::from_bytes_with_nul_unchecked(data);
110 assert_eq!(cstr, Ok(cstr_unchecked));
115 fn from_bytes_with_nul_unterminated() {
117 let cstr = CStr::from_bytes_with_nul(data);
118 assert!(cstr.is_err());
122 fn from_bytes_with_nul_interior() {
123 let data = b"1\023\0";
124 let cstr = CStr::from_bytes_with_nul(data);
125 assert!(cstr.is_err());
130 let orig: &[u8] = b"Hello, world!\0";
131 let cstr = CStr::from_bytes_with_nul(orig).unwrap();
132 let boxed: Box<CStr> = Box::from(cstr);
133 let cstring = cstr.to_owned().into_boxed_c_str().into_c_string();
134 assert_eq!(cstr, &*boxed);
135 assert_eq!(&*boxed, &*cstring);
136 assert_eq!(&*cstring, cstr);
141 let boxed = <Box<CStr>>::default();
142 assert_eq!(boxed.to_bytes_with_nul(), &[0]);
146 fn test_c_str_clone_into() {
147 let mut c_string = CString::new("lorem").unwrap();
148 let c_ptr = c_string.as_ptr();
149 let c_str = CStr::from_bytes_with_nul(b"ipsum\0").unwrap();
150 c_str.clone_into(&mut c_string);
151 assert_eq!(c_str, c_string.as_c_str());
152 // The exact same size shouldn't have needed to move its allocation
153 assert_eq!(c_ptr, c_string.as_ptr());
158 let orig: &[u8] = b"Hello, world!\0";
159 let cstr = CStr::from_bytes_with_nul(orig).unwrap();
160 let rc: Rc<CStr> = Rc::from(cstr);
161 let arc: Arc<CStr> = Arc::from(cstr);
163 assert_eq!(&*rc, cstr);
164 assert_eq!(&*arc, cstr);
166 let rc2: Rc<CStr> = Rc::from(cstr.to_owned());
167 let arc2: Arc<CStr> = Arc::from(cstr.to_owned());
169 assert_eq!(&*rc2, cstr);
170 assert_eq!(&*arc2, cstr);
174 fn cstr_const_constructor() {
175 const CSTR: &CStr = unsafe { CStr::from_bytes_with_nul_unchecked(b"Hello, world!\0") };
177 assert_eq!(CSTR.to_str().unwrap(), "Hello, world!");
181 fn cstr_index_from() {
182 let original = b"Hello, world!\0";
183 let cstr = CStr::from_bytes_with_nul(original).unwrap();
184 let result = CStr::from_bytes_with_nul(&original[7..]).unwrap();
186 assert_eq!(&cstr[7..], result);
191 fn cstr_index_from_empty() {
192 let original = b"Hello, world!\0";
193 let cstr = CStr::from_bytes_with_nul(original).unwrap();
194 let _ = &cstr[original.len()..];