diff options
| author | bors <bors@rust-lang.org> | 2013-08-09 21:56:17 -0700 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2013-08-09 21:56:17 -0700 |
| commit | 60f5011005eda4f08f0c36fe56e4df07ae9a903f (patch) | |
| tree | 4f3636735525b80b6aa3c6fc37b88a5f9057548c /src/test | |
| parent | e81e81f234731a31fad9afdc2045bef3fbdf1109 (diff) | |
| parent | ee59aacac490edd619db1c4e2fcd848f793bc3b9 (diff) | |
| download | rust-60f5011005eda4f08f0c36fe56e4df07ae9a903f.tar.gz rust-60f5011005eda4f08f0c36fe56e4df07ae9a903f.zip | |
auto merge of #8296 : erickt/rust/remove-str-trailing-nulls, r=erickt
This PR fixes #7235 and #3371, which removes trailing nulls from `str` types. Instead, it replaces the creation of c strings with a new type, `std::c_str::CString`, which wraps a malloced byte array, and respects: * No interior nulls * Ends with a trailing null
Diffstat (limited to 'src/test')
| -rw-r--r-- | src/test/compile-fail/static-slice-not-null-terminated.rs | 21 | ||||
| -rw-r--r-- | src/test/run-pass/c-stack-returning-int64.rs | 4 | ||||
| -rw-r--r-- | src/test/run-pass/const-str-ptr.rs | 23 | ||||
| -rw-r--r-- | src/test/run-pass/foreign-fn-linkname.rs | 5 |
4 files changed, 19 insertions, 34 deletions
diff --git a/src/test/compile-fail/static-slice-not-null-terminated.rs b/src/test/compile-fail/static-slice-not-null-terminated.rs deleted file mode 100644 index 3cfaa57d540..00000000000 --- a/src/test/compile-fail/static-slice-not-null-terminated.rs +++ /dev/null @@ -1,21 +0,0 @@ -// Copyright 2013 The Rust Project Developers. See the COPYRIGHT -// file at the top-level directory of this distribution and at -// http://rust-lang.org/COPYRIGHT. -// -// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or -// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license -// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -fn main() { - let _ = (~"foo").as_bytes_with_null(); - let _ = (@"foo").as_bytes_with_null(); - - // a plain static slice is null terminated, but such a slice can - // be sliced shorter (i.e. become non-null terminated) and still - // have the static lifetime - let foo: &'static str = "foo"; - let _ = foo.as_bytes_with_null(); - //~^ ERROR does not implement any method in scope named `as_bytes_with_null` -} diff --git a/src/test/run-pass/c-stack-returning-int64.rs b/src/test/run-pass/c-stack-returning-int64.rs index 9775f1ef45a..e91c11f5cd0 100644 --- a/src/test/run-pass/c-stack-returning-int64.rs +++ b/src/test/run-pass/c-stack-returning-int64.rs @@ -20,11 +20,11 @@ mod libc { } fn atol(s: ~str) -> int { - s.as_imm_buf(|x, _len| unsafe { libc::atol(x) }) + s.to_c_str().with_ref(|x| unsafe { libc::atol(x as *u8) }) } fn atoll(s: ~str) -> i64 { - s.as_imm_buf(|x, _len| unsafe { libc::atoll(x) }) + s.to_c_str().with_ref(|x| unsafe { libc::atoll(x as *u8) }) } pub fn main() { diff --git a/src/test/run-pass/const-str-ptr.rs b/src/test/run-pass/const-str-ptr.rs index 2f0cd3c611f..0e7d6d9f16a 100644 --- a/src/test/run-pass/const-str-ptr.rs +++ b/src/test/run-pass/const-str-ptr.rs @@ -10,15 +10,20 @@ use std::str; -static a: [u8, ..3] = ['h' as u8, 'i' as u8, 0 as u8]; -static c: &'static [u8, ..3] = &a; -static b: *u8 = c as *u8; +static A: [u8, ..2] = ['h' as u8, 'i' as u8]; +static B: &'static [u8, ..2] = &A; +static C: *u8 = B as *u8; pub fn main() { - let foo = &a as *u8; - assert_eq!(unsafe { str::raw::from_bytes(a) }, ~"hi\x00"); - assert_eq!(unsafe { str::raw::from_buf(foo) }, ~"hi"); - assert_eq!(unsafe { str::raw::from_buf(b) }, ~"hi"); - assert!(unsafe { *b == a[0] }); - assert!(unsafe { *(&c[0] as *u8) == a[0] }); + unsafe { + let foo = &A as *u8; + assert_eq!(str::raw::from_bytes(A), ~"hi"); + assert_eq!(str::raw::from_buf_len(foo, A.len()), ~"hi"); + assert_eq!(str::raw::from_buf_len(C, B.len()), ~"hi"); + assert!(*C == A[0]); + assert!(*(&B[0] as *u8) == A[0]); + + let bar = str::raw::from_bytes(A).to_c_str(); + assert_eq!(bar.with_ref(|buf| str::raw::from_c_str(buf)), ~"hi"); + } } diff --git a/src/test/run-pass/foreign-fn-linkname.rs b/src/test/run-pass/foreign-fn-linkname.rs index 38f36dd258b..b5a114ef223 100644 --- a/src/test/run-pass/foreign-fn-linkname.rs +++ b/src/test/run-pass/foreign-fn-linkname.rs @@ -26,8 +26,9 @@ mod libc { fn strlen(str: ~str) -> uint { unsafe { // C string is terminated with a zero - let bytes = str.to_bytes_with_null(); - return libc::my_strlen(vec::raw::to_ptr(bytes)); + do str.to_c_str().with_ref |buf| { + libc::my_strlen(buf as *u8) + } } } |
