diff options
| author | Erick Tryzelaar <erick.tryzelaar@gmail.com> | 2013-07-22 21:41:46 -0700 |
|---|---|---|
| committer | Erick Tryzelaar <erick.tryzelaar@gmail.com> | 2013-07-23 16:56:22 -0700 |
| commit | 7af56bb92199eabcfee52c43739761b18872a2c1 (patch) | |
| tree | 5a50c048e33bf6f655ac393facdb338d9650a619 /src/libstd | |
| parent | 9fdec67a6761ae43d8fae80e10b5a7a1e0ca7c0d (diff) | |
| download | rust-7af56bb92199eabcfee52c43739761b18872a2c1.tar.gz rust-7af56bb92199eabcfee52c43739761b18872a2c1.zip | |
std: move StrUtil::as_c_str into StrSlice
Diffstat (limited to 'src/libstd')
| -rw-r--r-- | src/libstd/io.rs | 2 | ||||
| -rw-r--r-- | src/libstd/os.rs | 23 | ||||
| -rw-r--r-- | src/libstd/path.rs | 37 | ||||
| -rw-r--r-- | src/libstd/prelude.rs | 2 | ||||
| -rw-r--r-- | src/libstd/ptr.rs | 16 | ||||
| -rw-r--r-- | src/libstd/rt/logging.rs | 8 | ||||
| -rw-r--r-- | src/libstd/rt/uv/uvll.rs | 4 | ||||
| -rw-r--r-- | src/libstd/run.rs | 10 | ||||
| -rw-r--r-- | src/libstd/str.rs | 74 |
9 files changed, 73 insertions, 103 deletions
diff --git a/src/libstd/io.rs b/src/libstd/io.rs index 6335588db50..fed4eb26dbe 100644 --- a/src/libstd/io.rs +++ b/src/libstd/io.rs @@ -63,7 +63,7 @@ use iterator::IteratorUtil; use ptr; use result; use str; -use str::{StrSlice, OwnedStr, StrUtil}; +use str::{StrSlice, OwnedStr}; use to_str::ToStr; use uint; use vec; diff --git a/src/libstd/os.rs b/src/libstd/os.rs index 2c3ce86ef78..8c118d0be76 100644 --- a/src/libstd/os.rs +++ b/src/libstd/os.rs @@ -243,12 +243,11 @@ pub fn env() -> ~[(~str,~str)] { pub fn getenv(n: &str) -> Option<~str> { unsafe { do with_env_lock { - let s = str::as_c_str(n, |s| libc::getenv(s)); + let s = n.as_c_str(|s| libc::getenv(s as *libc::c_char)); if ptr::null::<u8>() == cast::transmute(s) { - None::<~str> + None } else { - let s = cast::transmute(s); - Some::<~str>(str::raw::from_buf(s)) + Some(str::raw::from_buf(cast::transmute(s))) } } } @@ -277,8 +276,8 @@ pub fn getenv(n: &str) -> Option<~str> { pub fn setenv(n: &str, v: &str) { unsafe { do with_env_lock { - do str::as_c_str(n) |nbuf| { - do str::as_c_str(v) |vbuf| { + do n.to_str().as_c_str |nbuf| { + do v.to_str().as_c_str |vbuf| { libc::funcs::posix01::unistd::setenv(nbuf, vbuf, 1); } } @@ -309,7 +308,7 @@ pub fn unsetenv(n: &str) { fn _unsetenv(n: &str) { unsafe { do with_env_lock { - do str::as_c_str(n) |nbuf| { + do n.to_str().as_c_str |nbuf| { libc::funcs::posix01::unistd::unsetenv(nbuf); } } @@ -465,7 +464,7 @@ pub fn self_exe_path() -> Option<Path> { use libc::funcs::posix01::unistd::readlink; let mut path_str = str::with_capacity(TMPBUF_SZ); - let len = do str::as_c_str(path_str) |buf| { + let len = do path_str.as_c_str |buf| { let buf = buf as *mut c_char; do "/proc/self/exe".as_c_str |proc_self_buf| { readlink(proc_self_buf, buf, TMPBUF_SZ as size_t) @@ -598,7 +597,7 @@ pub fn walk_dir(p: &Path, f: &fn(&Path) -> bool) -> bool { /// Indicates whether a path represents a directory pub fn path_is_dir(p: &Path) -> bool { unsafe { - do str::as_c_str(p.to_str()) |buf| { + do p.to_str().as_c_str |buf| { rustrt::rust_path_is_dir(buf) != 0 as c_int } } @@ -607,7 +606,7 @@ pub fn path_is_dir(p: &Path) -> bool { /// Indicates whether a path exists pub fn path_exists(p: &Path) -> bool { unsafe { - do str::as_c_str(p.to_str()) |buf| { + do p.to_str().as_c_str |buf| { rustrt::rust_path_exists(buf) != 0 as c_int } } @@ -924,7 +923,7 @@ pub fn copy_file(from: &Path, to: &Path) -> bool { fclose(ostream); // Give the new file the old file's permissions - if do str::as_c_str(to.to_str()) |to_buf| { + if do to.to_str().as_c_str |to_buf| { libc::chmod(to_buf, from_mode as libc::mode_t) } != 0 { return false; // should be a condition... @@ -1290,7 +1289,7 @@ pub fn glob(pattern: &str) -> ~[Path] { } let mut g = default_glob_t(); - do str::as_c_str(pattern) |c_pattern| { + do pattern.as_c_str |c_pattern| { unsafe { libc::glob(c_pattern, 0, ptr::null(), &mut g) } }; do(|| { diff --git a/src/libstd/path.rs b/src/libstd/path.rs index 3ec3407b1cc..ef7a055b0e7 100644 --- a/src/libstd/path.rs +++ b/src/libstd/path.rs @@ -23,7 +23,6 @@ use iterator::IteratorUtil; use libc; use option::{None, Option, Some}; use str::{OwnedStr, Str, StrSlice, StrVector}; -use str; use to_str::ToStr; use ascii::{AsciiCast, AsciiStr}; use vec::{OwnedVector, ImmutableVector}; @@ -342,13 +341,11 @@ mod stat { #[cfg(target_os = "win32")] impl WindowsPath { pub fn stat(&self) -> Option<libc::stat> { - unsafe { - do str::as_c_str(self.to_str()) |buf| { - let mut st = stat::arch::default_stat(); - match libc::stat(buf, &mut st) { - 0 => Some(st), - _ => None, - } + do self.to_str().as_c_str |buf| { + let mut st = stat::arch::default_stat(); + match unsafe { libc::stat(buf, &mut st) } { + 0 => Some(st), + _ => None, } } } @@ -378,13 +375,11 @@ impl WindowsPath { #[cfg(not(target_os = "win32"))] impl PosixPath { pub fn stat(&self) -> Option<libc::stat> { - unsafe { - do str::as_c_str(self.to_str()) |buf| { - let mut st = stat::arch::default_stat(); - match libc::stat(buf, &mut st) { - 0 => Some(st), - _ => None, - } + do self.to_str().as_c_str |buf| { + let mut st = stat::arch::default_stat(); + match unsafe { libc::stat(buf as *libc::c_char, &mut st) } { + 0 => Some(st), + _ => None, } } } @@ -458,13 +453,11 @@ impl PosixPath { #[cfg(unix)] impl PosixPath { pub fn lstat(&self) -> Option<libc::stat> { - unsafe { - do str::as_c_str(self.to_str()) |buf| { - let mut st = stat::arch::default_stat(); - match libc::lstat(buf, &mut st) { - 0 => Some(st), - _ => None, - } + do self.to_str().as_c_str |buf| { + let mut st = stat::arch::default_stat(); + match unsafe { libc::lstat(buf, &mut st) } { + 0 => Some(st), + _ => None, } } } diff --git a/src/libstd/prelude.rs b/src/libstd/prelude.rs index e3e042a4947..0d9446bcfca 100644 --- a/src/libstd/prelude.rs +++ b/src/libstd/prelude.rs @@ -62,7 +62,7 @@ pub use path::PosixPath; pub use path::WindowsPath; pub use ptr::RawPtr; pub use ascii::{Ascii, AsciiCast, OwnedAsciiCast, AsciiStr, ToBytesConsume}; -pub use str::{Str, StrVector, StrSlice, OwnedStr, StrUtil, NullTerminatedStr}; +pub use str::{Str, StrVector, StrSlice, OwnedStr, NullTerminatedStr}; pub use from_str::{FromStr}; pub use to_bytes::IterBytes; pub use to_str::{ToStr, ToStrConsume}; diff --git a/src/libstd/ptr.rs b/src/libstd/ptr.rs index c2b59d51347..29564bd9728 100644 --- a/src/libstd/ptr.rs +++ b/src/libstd/ptr.rs @@ -461,17 +461,13 @@ pub mod ptr_tests { #[test] fn test_position() { - use str::as_c_str; use libc::c_char; let s = ~"hello"; unsafe { - assert!(2u == as_c_str(s, |p| position(p, - |c| *c == 'l' as c_char))); - assert!(4u == as_c_str(s, |p| position(p, - |c| *c == 'o' as c_char))); - assert!(5u == as_c_str(s, |p| position(p, - |c| *c == 0 as c_char))); + assert!(2u == s.as_c_str(|p| position(p, |c| *c == 'l' as c_char))); + assert!(4u == s.as_c_str(|p| position(p, |c| *c == 'o' as c_char))); + assert!(5u == s.as_c_str(|p| position(p, |c| *c == 0 as c_char))); } } @@ -480,9 +476,9 @@ pub mod ptr_tests { let s0 = ~"hello"; let s1 = ~"there"; let s2 = ~"thing"; - do str::as_c_str(s0) |p0| { - do str::as_c_str(s1) |p1| { - do str::as_c_str(s2) |p2| { + do s0.as_c_str |p0| { + do s1.as_c_str |p1| { + do s2.as_c_str |p2| { let v = ~[p0, p1, p2, null()]; do v.as_imm_buf |vp, len| { assert_eq!(unsafe { buf_len(vp) }, 3u); diff --git a/src/libstd/rt/logging.rs b/src/libstd/rt/logging.rs index 84186180aa6..11d11daebc2 100644 --- a/src/libstd/rt/logging.rs +++ b/src/libstd/rt/logging.rs @@ -46,17 +46,15 @@ impl Logger for StdErrLogger { /// per-module global logging flags based on the logging spec pub fn init(crate_map: *u8) { use os; - use str; + use str::StrSlice; use ptr; use option::{Some, None}; let log_spec = os::getenv("RUST_LOG"); match log_spec { Some(spec) => { - do str::as_c_str(spec) |s| { - unsafe { - rust_update_log_settings(crate_map, s); - } + do spec.as_c_str |buf| { + unsafe { rust_update_log_settings(crate_map, buf) } } } None => { diff --git a/src/libstd/rt/uv/uvll.rs b/src/libstd/rt/uv/uvll.rs index 62bf8f27af9..1f27a577684 100644 --- a/src/libstd/rt/uv/uvll.rs +++ b/src/libstd/rt/uv/uvll.rs @@ -382,12 +382,12 @@ pub unsafe fn as_sockaddr_in6(addr: *sockaddr) -> *sockaddr_in6 { } pub unsafe fn malloc_ip4_addr(ip: &str, port: int) -> *sockaddr_in { - do str::as_c_str(ip) |ip_buf| { + do ip.as_c_str |ip_buf| { rust_uv_ip4_addrp(ip_buf as *u8, port as libc::c_int) } } pub unsafe fn malloc_ip6_addr(ip: &str, port: int) -> *sockaddr_in6 { - do str::as_c_str(ip) |ip_buf| { + do ip.as_c_str |ip_buf| { rust_uv_ip6_addrp(ip_buf as *u8, port as libc::c_int) } } diff --git a/src/libstd/run.rs b/src/libstd/run.rs index c0b46ba273d..d8fc68d6422 100644 --- a/src/libstd/run.rs +++ b/src/libstd/run.rs @@ -507,7 +507,7 @@ fn spawn_process_os(prog: &str, args: &[~str], do with_envp(env) |envp| { do with_dirp(dir) |dirp| { - do str::as_c_str(cmd) |cmdp| { + do cmd.as_c_str |cmdp| { let created = CreateProcessA(ptr::null(), cast::transmute(cmdp), ptr::mut_null(), ptr::mut_null(), TRUE, 0, envp, dirp, &mut si, &mut pi); @@ -696,12 +696,12 @@ fn spawn_process_os(prog: &str, args: &[~str], #[cfg(unix)] fn with_argv<T>(prog: &str, args: &[~str], cb: &fn(**libc::c_char) -> T) -> T { - let mut argptrs = ~[str::as_c_str(prog, |b| b)]; + let mut argptrs = ~[prog.as_c_str(|b| b)]; let mut tmps = ~[]; for args.iter().advance |arg| { let t = @(*arg).clone(); tmps.push(t); - argptrs.push(str::as_c_str(*t, |b| b)); + argptrs.push(t.as_c_str(|b| b)); } argptrs.push(ptr::null()); argptrs.as_imm_buf(|buf, _len| cb(buf)) @@ -723,7 +723,7 @@ fn with_envp<T>(env: Option<&[(~str, ~str)]>, cb: &fn(*c_void) -> T) -> T { &(ref k, ref v) => { let kv = @fmt!("%s=%s", *k, *v); tmps.push(kv); - ptrs.push(str::as_c_str(*kv, |b| b)); + ptrs.push(kv.as_c_str(|b| b)); } } } @@ -761,7 +761,7 @@ fn with_envp<T>(env: Option<&[(~str, ~str)]>, cb: &fn(*mut c_void) -> T) -> T { fn with_dirp<T>(d: Option<&Path>, cb: &fn(*libc::c_char) -> T) -> T { match d { - Some(dir) => str::as_c_str(dir.to_str(), cb), + Some(dir) => dir.to_str().as_c_str(cb), None => cb(ptr::null()) } } diff --git a/src/libstd/str.rs b/src/libstd/str.rs index c16e87000f5..ab3d362ba79 100644 --- a/src/libstd/str.rs +++ b/src/libstd/str.rs @@ -773,51 +773,6 @@ static TAG_THREE_B: uint = 224u; static MAX_THREE_B: uint = 65536u; static TAG_FOUR_B: uint = 240u; -/** - * A dummy trait to hold all the utility methods that we implement on strings. - */ -pub trait StrUtil { - /** - * Work with the byte buffer of a string as a null-terminated C string. - * - * Allows for unsafe manipulation of strings, which is useful for foreign - * interop. This is similar to `str::as_buf`, but guarantees null-termination. - * If the given slice is not already null-terminated, this function will - * allocate a temporary, copy the slice, null terminate it, and pass - * that instead. - * - * # Example - * - * ~~~ {.rust} - * let s = "PATH".as_c_str(|path| libc::getenv(path)); - * ~~~ - */ - fn as_c_str<T>(self, f: &fn(*libc::c_char) -> T) -> T; -} - -impl<'self> StrUtil for &'self str { - #[inline] - fn as_c_str<T>(self, f: &fn(*libc::c_char) -> T) -> T { - do self.as_buf |buf, len| { - // NB: len includes the trailing null. - assert!(len > 0); - if unsafe { *(ptr::offset(buf,len-1)) != 0 } { - to_owned(self).as_c_str(|s| f(s)) - } else { - f(buf as *libc::c_char) - } - } - } -} - -/** - * Deprecated. Use the `as_c_str` method on strings instead. - */ -#[inline] -pub fn as_c_str<T>(s: &str, f: &fn(*libc::c_char) -> T) -> T { - s.as_c_str(f) -} - /// Unsafe operations pub mod raw { use cast; @@ -1271,6 +1226,7 @@ pub trait StrSlice<'self> { fn subslice_offset(&self, inner: &str) -> uint; fn as_buf<T>(&self, f: &fn(*u8, uint) -> T) -> T; + fn as_c_str<T>(&self, f: &fn(*libc::c_char) -> T) -> T; } /// Extension methods for strings @@ -2027,6 +1983,34 @@ impl<'self> StrSlice<'self> for &'self str { f(buf, len) } } + + /** + * Work with the byte buffer of a string as a null-terminated C string. + * + * Allows for unsafe manipulation of strings, which is useful for foreign + * interop. This is similar to `str::as_buf`, but guarantees null-termination. + * If the given slice is not already null-terminated, this function will + * allocate a temporary, copy the slice, null terminate it, and pass + * that instead. + * + * # Example + * + * ~~~ {.rust} + * let s = "PATH".as_c_str(|path| libc::getenv(path)); + * ~~~ + */ + #[inline] + fn as_c_str<T>(&self, f: &fn(*libc::c_char) -> T) -> T { + do self.as_buf |buf, len| { + // NB: len includes the trailing null. + assert!(len > 0); + if unsafe { *(ptr::offset(buf, len - 1)) != 0 } { + self.to_owned().as_c_str(|s| f(s)) + } else { + f(buf as *libc::c_char) + } + } + } } #[allow(missing_doc)] |
