2013-08-03 19:13:14 -05:00
|
|
|
// Copyright 2012 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.
|
|
|
|
|
2013-09-17 21:42:07 -05:00
|
|
|
/*!
|
|
|
|
|
|
|
|
C-string manipulation and management
|
|
|
|
|
|
|
|
This modules provides the basic methods for creating and manipulating
|
|
|
|
null-terminated strings for use with FFI calls (back to C). Most C APIs require
|
|
|
|
that the string being passed to them is null-terminated, and by default rust's
|
|
|
|
string types are *not* null terminated.
|
|
|
|
|
|
|
|
The other problem with translating Rust strings to C strings is that Rust
|
|
|
|
strings can validly contain a null-byte in the middle of the string (0 is a
|
|
|
|
valid unicode codepoint). This means that not all Rust strings can actually be
|
|
|
|
translated to C strings.
|
|
|
|
|
|
|
|
# Creation of a C string
|
|
|
|
|
|
|
|
A C string is managed through the `CString` type defined in this module. It
|
|
|
|
"owns" the internal buffer of characters and will automatically deallocate the
|
|
|
|
buffer when the string is dropped. The `ToCStr` trait is implemented for `&str`
|
|
|
|
and `&[u8]`, but the conversions can fail due to some of the limitations
|
|
|
|
explained above.
|
|
|
|
|
|
|
|
This also means that currently whenever a C string is created, an allocation
|
|
|
|
must be performed to place the data elsewhere (the lifetime of the C string is
|
|
|
|
not tied to the lifetime of the original string/data buffer). If C strings are
|
|
|
|
heavily used in applications, then caching may be advisable to prevent
|
|
|
|
unnecessary amounts of allocations.
|
|
|
|
|
|
|
|
An example of creating and using a C string would be:
|
|
|
|
|
2013-09-23 19:20:36 -05:00
|
|
|
```rust
|
2013-09-17 21:42:07 -05:00
|
|
|
use std::libc;
|
|
|
|
externfn!(fn puts(s: *libc::c_char))
|
|
|
|
|
|
|
|
let my_string = "Hello, world!";
|
|
|
|
|
|
|
|
// Allocate the C string with an explicit local that owns the string. The
|
|
|
|
// `c_buffer` pointer will be deallocated when `my_c_string` goes out of scope.
|
|
|
|
let my_c_string = my_string.to_c_str();
|
|
|
|
do my_c_string.with_ref |c_buffer| {
|
|
|
|
unsafe { puts(c_buffer); }
|
|
|
|
}
|
|
|
|
|
|
|
|
// Don't save off the allocation of the C string, the `c_buffer` will be
|
|
|
|
// deallocated when this block returns!
|
|
|
|
do my_string.with_c_str |c_buffer| {
|
|
|
|
unsafe { puts(c_buffer); }
|
|
|
|
}
|
2013-09-23 19:20:36 -05:00
|
|
|
```
|
2013-09-17 21:42:07 -05:00
|
|
|
|
|
|
|
*/
|
|
|
|
|
2013-08-03 19:13:14 -05:00
|
|
|
use cast;
|
2013-09-18 14:32:35 -05:00
|
|
|
use container::Container;
|
2013-09-08 10:01:16 -05:00
|
|
|
use iter::{Iterator, range};
|
2013-08-03 19:13:14 -05:00
|
|
|
use libc;
|
|
|
|
use ops::Drop;
|
|
|
|
use option::{Option, Some, None};
|
|
|
|
use ptr::RawPtr;
|
|
|
|
use ptr;
|
|
|
|
use str::StrSlice;
|
2013-09-18 14:32:35 -05:00
|
|
|
use str;
|
|
|
|
use vec::{CopyableVector, ImmutableVector, MutableVector};
|
|
|
|
use unstable::intrinsics;
|
2013-08-14 21:19:29 -05:00
|
|
|
|
|
|
|
/// Resolution options for the `null_byte` condition
|
|
|
|
pub enum NullByteResolution {
|
|
|
|
/// Truncate at the null byte
|
|
|
|
Truncate,
|
|
|
|
/// Use a replacement byte
|
|
|
|
ReplaceWith(libc::c_char)
|
|
|
|
}
|
|
|
|
|
|
|
|
condition! {
|
2013-09-03 21:39:14 -05:00
|
|
|
// This should be &[u8] but there's a lifetime issue (#5370).
|
2013-09-17 01:34:40 -05:00
|
|
|
pub null_byte: (~[u8]) -> NullByteResolution;
|
2013-08-14 21:19:29 -05:00
|
|
|
}
|
2013-08-03 19:13:14 -05:00
|
|
|
|
2013-08-04 20:37:55 -05:00
|
|
|
/// The representation of a C String.
|
|
|
|
///
|
|
|
|
/// This structure wraps a `*libc::c_char`, and will automatically free the
|
|
|
|
/// memory it is pointing to when it goes out of scope.
|
2013-08-03 19:13:14 -05:00
|
|
|
pub struct CString {
|
|
|
|
priv buf: *libc::c_char,
|
2013-08-04 20:37:55 -05:00
|
|
|
priv owns_buffer_: bool,
|
2013-08-03 19:13:14 -05:00
|
|
|
}
|
|
|
|
|
2013-08-05 21:55:07 -05:00
|
|
|
impl CString {
|
2013-08-04 20:37:55 -05:00
|
|
|
/// Create a C String from a pointer.
|
2013-08-06 23:11:10 -05:00
|
|
|
pub unsafe fn new(buf: *libc::c_char, owns_buffer: bool) -> CString {
|
2013-08-04 20:37:55 -05:00
|
|
|
CString { buf: buf, owns_buffer_: owns_buffer }
|
2013-08-03 19:13:14 -05:00
|
|
|
}
|
|
|
|
|
2013-08-04 20:37:55 -05:00
|
|
|
/// Unwraps the wrapped `*libc::c_char` from the `CString` wrapper.
|
2013-08-14 21:18:24 -05:00
|
|
|
/// Any ownership of the buffer by the `CString` wrapper is forgotten.
|
2013-08-04 20:37:55 -05:00
|
|
|
pub unsafe fn unwrap(self) -> *libc::c_char {
|
|
|
|
let mut c_str = self;
|
|
|
|
c_str.owns_buffer_ = false;
|
|
|
|
c_str.buf
|
2013-08-03 19:13:14 -05:00
|
|
|
}
|
|
|
|
|
2013-08-04 20:37:55 -05:00
|
|
|
/// Calls a closure with a reference to the underlying `*libc::c_char`.
|
|
|
|
///
|
|
|
|
/// # Failure
|
|
|
|
///
|
|
|
|
/// Fails if the CString is null.
|
2013-08-03 19:13:14 -05:00
|
|
|
pub fn with_ref<T>(&self, f: &fn(*libc::c_char) -> T) -> T {
|
2013-08-04 20:37:55 -05:00
|
|
|
if self.buf.is_null() { fail!("CString is null!"); }
|
2013-08-03 19:13:14 -05:00
|
|
|
f(self.buf)
|
|
|
|
}
|
|
|
|
|
2013-08-04 20:37:55 -05:00
|
|
|
/// Calls a closure with a mutable reference to the underlying `*libc::c_char`.
|
|
|
|
///
|
|
|
|
/// # Failure
|
|
|
|
///
|
|
|
|
/// Fails if the CString is null.
|
2013-08-03 19:13:14 -05:00
|
|
|
pub fn with_mut_ref<T>(&mut self, f: &fn(*mut libc::c_char) -> T) -> T {
|
2013-08-04 20:37:55 -05:00
|
|
|
if self.buf.is_null() { fail!("CString is null!"); }
|
2013-08-06 23:08:39 -05:00
|
|
|
f(unsafe { cast::transmute_mut_unsafe(self.buf) })
|
2013-08-03 19:13:14 -05:00
|
|
|
}
|
|
|
|
|
2013-08-04 20:37:55 -05:00
|
|
|
/// Returns true if the CString is a null.
|
|
|
|
pub fn is_null(&self) -> bool {
|
2013-08-03 19:13:14 -05:00
|
|
|
self.buf.is_null()
|
|
|
|
}
|
|
|
|
|
2013-08-04 20:37:55 -05:00
|
|
|
/// Returns true if the CString is not null.
|
|
|
|
pub fn is_not_null(&self) -> bool {
|
2013-08-03 19:13:14 -05:00
|
|
|
self.buf.is_not_null()
|
|
|
|
}
|
|
|
|
|
2013-08-04 20:37:55 -05:00
|
|
|
/// Returns whether or not the `CString` owns the buffer.
|
|
|
|
pub fn owns_buffer(&self) -> bool {
|
|
|
|
self.owns_buffer_
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Converts the CString into a `&[u8]` without copying.
|
|
|
|
///
|
|
|
|
/// # Failure
|
|
|
|
///
|
|
|
|
/// Fails if the CString is null.
|
2013-09-15 22:30:03 -05:00
|
|
|
#[inline]
|
2013-08-05 21:55:07 -05:00
|
|
|
pub fn as_bytes<'a>(&'a self) -> &'a [u8] {
|
2013-08-04 20:37:55 -05:00
|
|
|
if self.buf.is_null() { fail!("CString is null!"); }
|
2013-08-03 19:13:14 -05:00
|
|
|
unsafe {
|
2013-09-18 14:21:30 -05:00
|
|
|
cast::transmute((self.buf, self.len() + 1))
|
2013-08-03 19:13:14 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-15 22:30:03 -05:00
|
|
|
/// Converts the CString into a `&str` without copying.
|
|
|
|
/// Returns None if the CString is not UTF-8 or is null.
|
|
|
|
#[inline]
|
|
|
|
pub fn as_str<'a>(&'a self) -> Option<&'a str> {
|
|
|
|
if self.buf.is_null() { return None; }
|
|
|
|
let buf = self.as_bytes();
|
|
|
|
let buf = buf.slice_to(buf.len()-1); // chop off the trailing NUL
|
|
|
|
str::from_utf8_slice_opt(buf)
|
|
|
|
}
|
|
|
|
|
2013-08-04 20:37:55 -05:00
|
|
|
/// Return a CString iterator.
|
2013-08-14 21:31:13 -05:00
|
|
|
pub fn iter<'a>(&'a self) -> CStringIterator<'a> {
|
2013-08-03 19:13:14 -05:00
|
|
|
CStringIterator {
|
|
|
|
ptr: self.buf,
|
|
|
|
lifetime: unsafe { cast::transmute(self.buf) },
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Drop for CString {
|
2013-09-16 20:18:07 -05:00
|
|
|
fn drop(&mut self) {
|
2013-08-14 20:41:40 -05:00
|
|
|
#[fixed_stack_segment]; #[inline(never)];
|
2013-08-06 23:08:39 -05:00
|
|
|
if self.owns_buffer_ {
|
2013-08-03 19:13:14 -05:00
|
|
|
unsafe {
|
|
|
|
libc::free(self.buf as *libc::c_void)
|
2013-08-04 20:37:55 -05:00
|
|
|
}
|
2013-08-03 19:13:14 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-18 14:21:30 -05:00
|
|
|
impl Container for CString {
|
|
|
|
#[inline]
|
|
|
|
fn len(&self) -> uint {
|
|
|
|
unsafe {
|
|
|
|
ptr::position(self.buf, |c| *c == 0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-04 20:37:55 -05:00
|
|
|
/// A generic trait for converting a value to a CString.
|
2013-08-03 19:13:14 -05:00
|
|
|
pub trait ToCStr {
|
2013-08-14 21:19:29 -05:00
|
|
|
/// Copy the receiver into a CString.
|
|
|
|
///
|
|
|
|
/// # Failure
|
|
|
|
///
|
|
|
|
/// Raises the `null_byte` condition if the receiver has an interior null.
|
2013-08-03 19:13:14 -05:00
|
|
|
fn to_c_str(&self) -> CString;
|
2013-08-14 21:19:29 -05:00
|
|
|
|
|
|
|
/// Unsafe variant of `to_c_str()` that doesn't check for nulls.
|
|
|
|
unsafe fn to_c_str_unchecked(&self) -> CString;
|
2013-08-14 21:21:59 -05:00
|
|
|
|
|
|
|
/// Work with a temporary CString constructed from the receiver.
|
|
|
|
/// The provided `*libc::c_char` will be freed immediately upon return.
|
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
2013-09-23 19:20:36 -05:00
|
|
|
/// ```rust
|
2013-08-14 21:21:59 -05:00
|
|
|
/// let s = "PATH".with_c_str(|path| libc::getenv(path))
|
2013-09-23 19:20:36 -05:00
|
|
|
/// ```
|
2013-08-14 21:21:59 -05:00
|
|
|
///
|
|
|
|
/// # Failure
|
|
|
|
///
|
|
|
|
/// Raises the `null_byte` condition if the receiver has an interior null.
|
|
|
|
#[inline]
|
|
|
|
fn with_c_str<T>(&self, f: &fn(*libc::c_char) -> T) -> T {
|
|
|
|
self.to_c_str().with_ref(f)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Unsafe variant of `with_c_str()` that doesn't check for nulls.
|
|
|
|
#[inline]
|
|
|
|
unsafe fn with_c_str_unchecked<T>(&self, f: &fn(*libc::c_char) -> T) -> T {
|
|
|
|
self.to_c_str_unchecked().with_ref(f)
|
|
|
|
}
|
2013-08-03 19:13:14 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'self> ToCStr for &'self str {
|
2013-08-04 20:37:55 -05:00
|
|
|
#[inline]
|
2013-08-03 19:13:14 -05:00
|
|
|
fn to_c_str(&self) -> CString {
|
|
|
|
self.as_bytes().to_c_str()
|
|
|
|
}
|
2013-08-14 21:19:29 -05:00
|
|
|
|
|
|
|
#[inline]
|
|
|
|
unsafe fn to_c_str_unchecked(&self) -> CString {
|
|
|
|
self.as_bytes().to_c_str_unchecked()
|
|
|
|
}
|
2013-09-18 14:32:35 -05:00
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn with_c_str<T>(&self, f: &fn(*libc::c_char) -> T) -> T {
|
|
|
|
self.as_bytes().with_c_str(f)
|
|
|
|
}
|
2013-09-20 18:48:07 -05:00
|
|
|
|
|
|
|
#[inline]
|
|
|
|
unsafe fn with_c_str_unchecked<T>(&self, f: &fn(*libc::c_char) -> T) -> T {
|
|
|
|
self.as_bytes().with_c_str_unchecked(f)
|
|
|
|
}
|
2013-08-03 19:13:14 -05:00
|
|
|
}
|
|
|
|
|
2013-09-18 14:32:35 -05:00
|
|
|
// The length of the stack allocated buffer for `vec.with_c_str()`
|
2013-09-20 15:25:02 -05:00
|
|
|
static BUF_LEN: uint = 128;
|
2013-09-18 14:32:35 -05:00
|
|
|
|
2013-08-03 19:13:14 -05:00
|
|
|
impl<'self> ToCStr for &'self [u8] {
|
|
|
|
fn to_c_str(&self) -> CString {
|
2013-08-14 20:41:40 -05:00
|
|
|
#[fixed_stack_segment]; #[inline(never)];
|
2013-08-14 21:19:29 -05:00
|
|
|
let mut cs = unsafe { self.to_c_str_unchecked() };
|
|
|
|
do cs.with_mut_ref |buf| {
|
2013-09-18 14:32:35 -05:00
|
|
|
check_for_null(*self, buf);
|
2013-08-14 21:19:29 -05:00
|
|
|
}
|
|
|
|
cs
|
|
|
|
}
|
2013-08-03 19:13:14 -05:00
|
|
|
|
2013-08-14 21:19:29 -05:00
|
|
|
unsafe fn to_c_str_unchecked(&self) -> CString {
|
2013-08-14 20:41:40 -05:00
|
|
|
#[fixed_stack_segment]; #[inline(never)];
|
2013-08-14 21:19:29 -05:00
|
|
|
do self.as_imm_buf |self_buf, self_len| {
|
|
|
|
let buf = libc::malloc(self_len as libc::size_t + 1) as *mut u8;
|
|
|
|
if buf.is_null() {
|
|
|
|
fail!("failed to allocate memory!");
|
2013-08-03 19:13:14 -05:00
|
|
|
}
|
2013-08-14 21:19:29 -05:00
|
|
|
|
|
|
|
ptr::copy_memory(buf, self_buf, self_len);
|
|
|
|
*ptr::mut_offset(buf, self_len as int) = 0;
|
|
|
|
|
|
|
|
CString::new(buf as *libc::c_char, true)
|
2013-08-03 19:13:14 -05:00
|
|
|
}
|
|
|
|
}
|
2013-09-18 14:32:35 -05:00
|
|
|
|
|
|
|
fn with_c_str<T>(&self, f: &fn(*libc::c_char) -> T) -> T {
|
|
|
|
if self.len() < BUF_LEN {
|
|
|
|
do self.as_imm_buf |self_buf, self_len| {
|
|
|
|
unsafe {
|
|
|
|
let mut buf: [u8, .. BUF_LEN] = intrinsics::uninit();
|
|
|
|
|
|
|
|
do buf.as_mut_buf |buf, _| {
|
|
|
|
ptr::copy_memory(buf, self_buf, self_len);
|
|
|
|
*ptr::mut_offset(buf, self_len as int) = 0;
|
|
|
|
|
|
|
|
check_for_null(*self, buf as *mut libc::c_char);
|
|
|
|
|
|
|
|
f(buf as *libc::c_char)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
self.to_c_str().with_ref(f)
|
|
|
|
}
|
|
|
|
}
|
2013-09-20 18:48:07 -05:00
|
|
|
|
|
|
|
unsafe fn with_c_str_unchecked<T>(&self, f: &fn(*libc::c_char) -> T) -> T {
|
|
|
|
if self.len() < BUF_LEN {
|
|
|
|
do self.as_imm_buf |self_buf, self_len| {
|
|
|
|
let mut buf: [u8, .. BUF_LEN] = intrinsics::uninit();
|
|
|
|
|
|
|
|
do buf.as_mut_buf |buf, _| {
|
|
|
|
ptr::copy_memory(buf, self_buf, self_len);
|
|
|
|
*ptr::mut_offset(buf, self_len as int) = 0;
|
|
|
|
|
|
|
|
f(buf as *libc::c_char)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
self.to_c_str().with_ref(f)
|
|
|
|
}
|
|
|
|
}
|
2013-09-18 14:32:35 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn check_for_null(v: &[u8], buf: *mut libc::c_char) {
|
|
|
|
for i in range(0, v.len()) {
|
|
|
|
unsafe {
|
|
|
|
let p = buf.offset(i as int);
|
|
|
|
if *p == 0 {
|
|
|
|
match null_byte::cond.raise(v.to_owned()) {
|
|
|
|
Truncate => break,
|
|
|
|
ReplaceWith(c) => *p = c
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-08-03 19:13:14 -05:00
|
|
|
}
|
|
|
|
|
2013-08-04 20:37:55 -05:00
|
|
|
/// External iterator for a CString's bytes.
|
|
|
|
///
|
|
|
|
/// Use with the `std::iterator` module.
|
2013-08-03 19:13:14 -05:00
|
|
|
pub struct CStringIterator<'self> {
|
|
|
|
priv ptr: *libc::c_char,
|
|
|
|
priv lifetime: &'self libc::c_char, // FIXME: #5922
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'self> Iterator<libc::c_char> for CStringIterator<'self> {
|
|
|
|
fn next(&mut self) -> Option<libc::c_char> {
|
2013-08-06 23:06:12 -05:00
|
|
|
let ch = unsafe { *self.ptr };
|
|
|
|
if ch == 0 {
|
2013-08-03 19:13:14 -05:00
|
|
|
None
|
|
|
|
} else {
|
2013-08-09 00:22:52 -05:00
|
|
|
self.ptr = unsafe { ptr::offset(self.ptr, 1) };
|
2013-08-03 19:13:14 -05:00
|
|
|
Some(ch)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
use libc;
|
|
|
|
use ptr;
|
2013-08-06 23:06:12 -05:00
|
|
|
use option::{Some, None};
|
2013-08-03 19:13:14 -05:00
|
|
|
|
|
|
|
#[test]
|
2013-09-15 22:30:03 -05:00
|
|
|
fn test_str_to_c_str() {
|
2013-08-03 19:13:14 -05:00
|
|
|
do "".to_c_str().with_ref |buf| {
|
|
|
|
unsafe {
|
|
|
|
assert_eq!(*ptr::offset(buf, 0), 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
do "hello".to_c_str().with_ref |buf| {
|
|
|
|
unsafe {
|
|
|
|
assert_eq!(*ptr::offset(buf, 0), 'h' as libc::c_char);
|
|
|
|
assert_eq!(*ptr::offset(buf, 1), 'e' as libc::c_char);
|
|
|
|
assert_eq!(*ptr::offset(buf, 2), 'l' as libc::c_char);
|
|
|
|
assert_eq!(*ptr::offset(buf, 3), 'l' as libc::c_char);
|
|
|
|
assert_eq!(*ptr::offset(buf, 4), 'o' as libc::c_char);
|
|
|
|
assert_eq!(*ptr::offset(buf, 5), 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-15 22:30:03 -05:00
|
|
|
#[test]
|
|
|
|
fn test_vec_to_c_str() {
|
|
|
|
let b: &[u8] = [];
|
|
|
|
do b.to_c_str().with_ref |buf| {
|
|
|
|
unsafe {
|
|
|
|
assert_eq!(*ptr::offset(buf, 0), 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
do bytes!("hello").to_c_str().with_ref |buf| {
|
|
|
|
unsafe {
|
|
|
|
assert_eq!(*ptr::offset(buf, 0), 'h' as libc::c_char);
|
|
|
|
assert_eq!(*ptr::offset(buf, 1), 'e' as libc::c_char);
|
|
|
|
assert_eq!(*ptr::offset(buf, 2), 'l' as libc::c_char);
|
|
|
|
assert_eq!(*ptr::offset(buf, 3), 'l' as libc::c_char);
|
|
|
|
assert_eq!(*ptr::offset(buf, 4), 'o' as libc::c_char);
|
|
|
|
assert_eq!(*ptr::offset(buf, 5), 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
do bytes!("foo", 0xff).to_c_str().with_ref |buf| {
|
|
|
|
unsafe {
|
|
|
|
assert_eq!(*ptr::offset(buf, 0), 'f' as libc::c_char);
|
|
|
|
assert_eq!(*ptr::offset(buf, 1), 'o' as libc::c_char);
|
|
|
|
assert_eq!(*ptr::offset(buf, 2), 'o' as libc::c_char);
|
|
|
|
assert_eq!(*ptr::offset(buf, 3), 0xff);
|
|
|
|
assert_eq!(*ptr::offset(buf, 4), 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-03 19:13:14 -05:00
|
|
|
#[test]
|
2013-08-04 20:37:55 -05:00
|
|
|
fn test_is_null() {
|
2013-08-06 23:11:10 -05:00
|
|
|
let c_str = unsafe { CString::new(ptr::null(), false) };
|
2013-08-04 20:37:55 -05:00
|
|
|
assert!(c_str.is_null());
|
|
|
|
assert!(!c_str.is_not_null());
|
2013-08-03 19:13:14 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2013-08-04 20:37:55 -05:00
|
|
|
fn test_unwrap() {
|
2013-08-14 20:41:40 -05:00
|
|
|
#[fixed_stack_segment]; #[inline(never)];
|
|
|
|
|
2013-08-04 20:37:55 -05:00
|
|
|
let c_str = "hello".to_c_str();
|
|
|
|
unsafe { libc::free(c_str.unwrap() as *libc::c_void) }
|
2013-08-03 19:13:14 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2013-08-04 20:37:55 -05:00
|
|
|
fn test_with_ref() {
|
2013-08-14 20:41:40 -05:00
|
|
|
#[fixed_stack_segment]; #[inline(never)];
|
|
|
|
|
2013-08-03 19:13:14 -05:00
|
|
|
let c_str = "hello".to_c_str();
|
|
|
|
let len = unsafe { c_str.with_ref(|buf| libc::strlen(buf)) };
|
2013-08-04 20:37:55 -05:00
|
|
|
assert!(!c_str.is_null());
|
|
|
|
assert!(c_str.is_not_null());
|
2013-08-03 19:13:14 -05:00
|
|
|
assert_eq!(len, 5);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[should_fail]
|
2013-08-04 20:37:55 -05:00
|
|
|
fn test_with_ref_empty_fail() {
|
2013-08-06 23:06:12 -05:00
|
|
|
let c_str = unsafe { CString::new(ptr::null(), false) };
|
2013-08-03 19:13:14 -05:00
|
|
|
c_str.with_ref(|_| ());
|
|
|
|
}
|
2013-08-06 23:06:12 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_iterator() {
|
|
|
|
let c_str = "".to_c_str();
|
|
|
|
let mut iter = c_str.iter();
|
|
|
|
assert_eq!(iter.next(), None);
|
|
|
|
|
|
|
|
let c_str = "hello".to_c_str();
|
|
|
|
let mut iter = c_str.iter();
|
|
|
|
assert_eq!(iter.next(), Some('h' as libc::c_char));
|
|
|
|
assert_eq!(iter.next(), Some('e' as libc::c_char));
|
|
|
|
assert_eq!(iter.next(), Some('l' as libc::c_char));
|
|
|
|
assert_eq!(iter.next(), Some('l' as libc::c_char));
|
|
|
|
assert_eq!(iter.next(), Some('o' as libc::c_char));
|
|
|
|
assert_eq!(iter.next(), None);
|
|
|
|
}
|
2013-08-14 21:19:29 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_to_c_str_fail() {
|
|
|
|
use c_str::null_byte::cond;
|
|
|
|
|
|
|
|
let mut error_happened = false;
|
|
|
|
do cond.trap(|err| {
|
|
|
|
assert_eq!(err, bytes!("he", 0, "llo").to_owned())
|
|
|
|
error_happened = true;
|
|
|
|
Truncate
|
|
|
|
}).inside {
|
|
|
|
"he\x00llo".to_c_str()
|
|
|
|
};
|
|
|
|
assert!(error_happened);
|
|
|
|
|
|
|
|
do cond.trap(|_| {
|
|
|
|
ReplaceWith('?' as libc::c_char)
|
|
|
|
}).inside(|| "he\x00llo".to_c_str()).with_ref |buf| {
|
|
|
|
unsafe {
|
|
|
|
assert_eq!(*buf.offset(0), 'h' as libc::c_char);
|
|
|
|
assert_eq!(*buf.offset(1), 'e' as libc::c_char);
|
|
|
|
assert_eq!(*buf.offset(2), '?' as libc::c_char);
|
|
|
|
assert_eq!(*buf.offset(3), 'l' as libc::c_char);
|
|
|
|
assert_eq!(*buf.offset(4), 'l' as libc::c_char);
|
|
|
|
assert_eq!(*buf.offset(5), 'o' as libc::c_char);
|
|
|
|
assert_eq!(*buf.offset(6), 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_to_c_str_unchecked() {
|
|
|
|
unsafe {
|
|
|
|
do "he\x00llo".to_c_str_unchecked().with_ref |buf| {
|
|
|
|
assert_eq!(*buf.offset(0), 'h' as libc::c_char);
|
|
|
|
assert_eq!(*buf.offset(1), 'e' as libc::c_char);
|
|
|
|
assert_eq!(*buf.offset(2), 0);
|
|
|
|
assert_eq!(*buf.offset(3), 'l' as libc::c_char);
|
|
|
|
assert_eq!(*buf.offset(4), 'l' as libc::c_char);
|
|
|
|
assert_eq!(*buf.offset(5), 'o' as libc::c_char);
|
|
|
|
assert_eq!(*buf.offset(6), 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-09-15 22:30:03 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_as_bytes() {
|
|
|
|
let c_str = "hello".to_c_str();
|
|
|
|
assert_eq!(c_str.as_bytes(), bytes!("hello", 0));
|
|
|
|
let c_str = "".to_c_str();
|
|
|
|
assert_eq!(c_str.as_bytes(), bytes!(0));
|
|
|
|
let c_str = bytes!("foo", 0xff).to_c_str();
|
|
|
|
assert_eq!(c_str.as_bytes(), bytes!("foo", 0xff, 0));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[should_fail]
|
|
|
|
fn test_as_bytes_fail() {
|
|
|
|
let c_str = unsafe { CString::new(ptr::null(), false) };
|
|
|
|
c_str.as_bytes();
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_as_str() {
|
|
|
|
let c_str = "hello".to_c_str();
|
|
|
|
assert_eq!(c_str.as_str(), Some("hello"));
|
|
|
|
let c_str = "".to_c_str();
|
|
|
|
assert_eq!(c_str.as_str(), Some(""));
|
|
|
|
let c_str = bytes!("foo", 0xff).to_c_str();
|
|
|
|
assert_eq!(c_str.as_str(), None);
|
|
|
|
let c_str = unsafe { CString::new(ptr::null(), false) };
|
|
|
|
assert_eq!(c_str.as_str(), None);
|
|
|
|
}
|
2013-08-03 19:13:14 -05:00
|
|
|
}
|
2013-09-18 14:32:45 -05:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod bench {
|
|
|
|
use iter::range;
|
|
|
|
use libc;
|
|
|
|
use option::Some;
|
|
|
|
use ptr;
|
|
|
|
use extra::test::BenchHarness;
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn check(s: &str, c_str: *libc::c_char) {
|
|
|
|
do s.as_imm_buf |s_buf, s_len| {
|
|
|
|
for i in range(0, s_len) {
|
|
|
|
unsafe {
|
|
|
|
assert_eq!(
|
|
|
|
*ptr::offset(s_buf, i as int) as libc::c_char,
|
|
|
|
*ptr::offset(c_str, i as int));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static s_short: &'static str = "Mary";
|
|
|
|
static s_medium: &'static str = "Mary had a little lamb";
|
|
|
|
static s_long: &'static str = "\
|
|
|
|
Mary had a little lamb, Little lamb
|
|
|
|
Mary had a little lamb, Little lamb
|
|
|
|
Mary had a little lamb, Little lamb
|
|
|
|
Mary had a little lamb, Little lamb
|
|
|
|
Mary had a little lamb, Little lamb
|
|
|
|
Mary had a little lamb, Little lamb";
|
|
|
|
|
|
|
|
fn bench_to_str(bh: &mut BenchHarness, s: &str) {
|
|
|
|
do bh.iter {
|
|
|
|
let c_str = s.to_c_str();
|
|
|
|
do c_str.with_ref |c_str_buf| {
|
|
|
|
check(s, c_str_buf)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
|
|
|
fn bench_to_c_str_short(bh: &mut BenchHarness) {
|
|
|
|
bench_to_str(bh, s_short)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
|
|
|
fn bench_to_c_str_medium(bh: &mut BenchHarness) {
|
|
|
|
bench_to_str(bh, s_medium)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
|
|
|
fn bench_to_c_str_long(bh: &mut BenchHarness) {
|
|
|
|
bench_to_str(bh, s_long)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn bench_to_c_str_unchecked(bh: &mut BenchHarness, s: &str) {
|
|
|
|
do bh.iter {
|
|
|
|
let c_str = unsafe { s.to_c_str_unchecked() };
|
|
|
|
do c_str.with_ref |c_str_buf| {
|
|
|
|
check(s, c_str_buf)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
|
|
|
fn bench_to_c_str_unchecked_short(bh: &mut BenchHarness) {
|
|
|
|
bench_to_c_str_unchecked(bh, s_short)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
|
|
|
fn bench_to_c_str_unchecked_medium(bh: &mut BenchHarness) {
|
|
|
|
bench_to_c_str_unchecked(bh, s_medium)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
|
|
|
fn bench_to_c_str_unchecked_long(bh: &mut BenchHarness) {
|
|
|
|
bench_to_c_str_unchecked(bh, s_long)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn bench_with_c_str(bh: &mut BenchHarness, s: &str) {
|
|
|
|
do bh.iter {
|
|
|
|
do s.with_c_str |c_str_buf| {
|
|
|
|
check(s, c_str_buf)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
|
|
|
fn bench_with_c_str_short(bh: &mut BenchHarness) {
|
|
|
|
bench_with_c_str(bh, s_short)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
|
|
|
fn bench_with_c_str_medium(bh: &mut BenchHarness) {
|
|
|
|
bench_with_c_str(bh, s_medium)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
|
|
|
fn bench_with_c_str_long(bh: &mut BenchHarness) {
|
|
|
|
bench_with_c_str(bh, s_long)
|
|
|
|
}
|
2013-09-20 18:48:07 -05:00
|
|
|
|
|
|
|
fn bench_with_c_str_unchecked(bh: &mut BenchHarness, s: &str) {
|
|
|
|
do bh.iter {
|
|
|
|
unsafe {
|
|
|
|
do s.with_c_str_unchecked |c_str_buf| {
|
|
|
|
check(s, c_str_buf)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
|
|
|
fn bench_with_c_str_unchecked_short(bh: &mut BenchHarness) {
|
|
|
|
bench_with_c_str_unchecked(bh, s_short)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
|
|
|
fn bench_with_c_str_unchecked_medium(bh: &mut BenchHarness) {
|
|
|
|
bench_with_c_str_unchecked(bh, s_medium)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
|
|
|
fn bench_with_c_str_unchecked_long(bh: &mut BenchHarness) {
|
|
|
|
bench_with_c_str_unchecked(bh, s_long)
|
|
|
|
}
|
2013-09-18 14:32:45 -05:00
|
|
|
}
|