diff options
| author | 1011X <1011XXXXX@gmail.com> | 2020-07-03 22:11:10 -0400 |
|---|---|---|
| committer | David Tolnay <dtolnay@gmail.com> | 2020-07-18 12:14:32 -0700 |
| commit | f08aae6a2bbd5c509029da592ea3a5634a7bc743 (patch) | |
| tree | 5b080ab9dcd0056238fe0854386bd818d2633f50 /src | |
| parent | 9a13ef2251531d5856ca62dd8822c9b8139f479a (diff) | |
| download | rust-f08aae6a2bbd5c509029da592ea3a5634a7bc743.tar.gz rust-f08aae6a2bbd5c509029da592ea3a5634a7bc743.zip | |
impl Index<RangeFrom<usize>> for CStr
Diffstat (limited to 'src')
| -rw-r--r-- | src/libstd/ffi/c_str.rs | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/src/libstd/ffi/c_str.rs b/src/libstd/ffi/c_str.rs index dca1fdde482..ce254f73619 100644 --- a/src/libstd/ffi/c_str.rs +++ b/src/libstd/ffi/c_str.rs @@ -1551,6 +1551,27 @@ impl ops::Index<ops::RangeFull> for CString { } } +#[stable(feature = "cstr_range_from", since = "1.45.0")] +impl ops::Index<ops::RangeFrom<usize>> for CStr { + type Output = CStr; + + fn index(&self, index: ops::RangeFrom<usize>) -> &CStr { + let bytes = self.to_bytes_with_nul(); + // we need to manually check the starting index to account for the null + // byte, since otherwise we could get an empty string that doesn't end + // in a null. + if index.start < bytes.len() { + unsafe { CStr::from_bytes_with_nul_unchecked(&bytes[index.start..]) } + } else { + panic!( + "index out of bounds: the len is {} but the index is {}", + bytes.len(), + index.start + ); + } + } +} + #[stable(feature = "cstring_asref", since = "1.7.0")] impl AsRef<CStr> for CStr { #[inline] @@ -1747,4 +1768,21 @@ mod tests { assert_eq!(CSTR.to_str().unwrap(), "Hello, world!"); } + + #[test] + fn cstr_index_from() { + let original = b"Hello, world!\0"; + let cstr = CStr::from_bytes_with_nul(original).unwrap(); + let result = CStr::from_bytes_with_nul(&original[7..]).unwrap(); + + assert_eq!(&cstr[7..], result); + } + + #[test] + #[should_panic] + fn cstr_index_from_empty() { + let original = b"Hello, world!\0"; + let cstr = CStr::from_bytes_with_nul(original).unwrap(); + let _ = &cstr[original.len()..]; + } } |
