1 // Copyright 2012 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
13 C-string manipulation and management
15 This modules provides the basic methods for creating and manipulating
16 null-terminated strings for use with FFI calls (back to C). Most C APIs require
17 that the string being passed to them is null-terminated, and by default rust's
18 string types are *not* null terminated.
20 The other problem with translating Rust strings to C strings is that Rust
21 strings can validly contain a null-byte in the middle of the string (0 is a
22 valid unicode codepoint). This means that not all Rust strings can actually be
23 translated to C strings.
25 # Creation of a C string
27 A C string is managed through the `CString` type defined in this module. It
28 "owns" the internal buffer of characters and will automatically deallocate the
29 buffer when the string is dropped. The `ToCStr` trait is implemented for `&str`
30 and `&[u8]`, but the conversions can fail due to some of the limitations
33 This also means that currently whenever a C string is created, an allocation
34 must be performed to place the data elsewhere (the lifetime of the C string is
35 not tied to the lifetime of the original string/data buffer). If C strings are
36 heavily used in applications, then caching may be advisable to prevent
37 unnecessary amounts of allocations.
39 An example of creating and using a C string would be:
44 fn puts(s: *libc::c_char);
47 let my_string = "Hello, world!";
49 // Allocate the C string with an explicit local that owns the string. The
50 // `c_buffer` pointer will be deallocated when `my_c_string` goes out of scope.
51 let my_c_string = my_string.to_c_str();
52 my_c_string.with_ref(|c_buffer| {
53 unsafe { puts(c_buffer); }
56 // Don't save off the allocation of the C string, the `c_buffer` will be
57 // deallocated when this block returns!
58 my_string.with_c_str(|c_buffer| {
59 unsafe { puts(c_buffer); }
66 use container::Container;
67 use iter::{Iterator, range};
70 use option::{Option, Some, None};
75 use vec::{CopyableVector, ImmutableVector, MutableVector};
77 use unstable::intrinsics;
79 /// Resolution options for the `null_byte` condition
80 pub enum NullByteResolution {
81 /// Truncate at the null byte
83 /// Use a replacement byte
84 ReplaceWith(libc::c_char)
88 // This should be &[u8] but there's a lifetime issue (#5370).
89 pub null_byte: (~[u8]) -> NullByteResolution;
92 /// The representation of a C String.
94 /// This structure wraps a `*libc::c_char`, and will automatically free the
95 /// memory it is pointing to when it goes out of scope.
97 priv buf: *libc::c_char,
98 priv owns_buffer_: bool,
102 /// Create a C String from a pointer.
103 pub unsafe fn new(buf: *libc::c_char, owns_buffer: bool) -> CString {
104 CString { buf: buf, owns_buffer_: owns_buffer }
107 /// Unwraps the wrapped `*libc::c_char` from the `CString` wrapper.
108 /// Any ownership of the buffer by the `CString` wrapper is forgotten.
109 pub unsafe fn unwrap(self) -> *libc::c_char {
110 let mut c_str = self;
111 c_str.owns_buffer_ = false;
115 /// Calls a closure with a reference to the underlying `*libc::c_char`.
119 /// Fails if the CString is null.
120 pub fn with_ref<T>(&self, f: |*libc::c_char| -> T) -> T {
121 if self.buf.is_null() { fail!("CString is null!"); }
125 /// Calls a closure with a mutable reference to the underlying `*libc::c_char`.
129 /// Fails if the CString is null.
130 pub fn with_mut_ref<T>(&mut self, f: |*mut libc::c_char| -> T) -> T {
131 if self.buf.is_null() { fail!("CString is null!"); }
132 f(unsafe { cast::transmute_mut_unsafe(self.buf) })
135 /// Returns true if the CString is a null.
136 pub fn is_null(&self) -> bool {
140 /// Returns true if the CString is not null.
141 pub fn is_not_null(&self) -> bool {
142 self.buf.is_not_null()
145 /// Returns whether or not the `CString` owns the buffer.
146 pub fn owns_buffer(&self) -> bool {
150 /// Converts the CString into a `&[u8]` without copying.
154 /// Fails if the CString is null.
156 pub fn as_bytes<'a>(&'a self) -> &'a [u8] {
157 if self.buf.is_null() { fail!("CString is null!"); }
159 cast::transmute((self.buf, self.len() + 1))
163 /// Converts the CString into a `&str` without copying.
164 /// Returns None if the CString is not UTF-8 or is null.
166 pub fn as_str<'a>(&'a self) -> Option<&'a str> {
167 if self.buf.is_null() { return None; }
168 let buf = self.as_bytes();
169 let buf = buf.slice_to(buf.len()-1); // chop off the trailing NUL
170 str::from_utf8_opt(buf)
173 /// Return a CString iterator.
174 pub fn iter<'a>(&'a self) -> CStringIterator<'a> {
177 lifetime: unsafe { cast::transmute(self.buf) },
182 impl Drop for CString {
184 if self.owns_buffer_ {
186 libc::free(self.buf as *libc::c_void)
192 impl Container for CString {
194 fn len(&self) -> uint {
196 ptr::position(self.buf, |c| *c == 0)
201 /// A generic trait for converting a value to a CString.
203 /// Copy the receiver into a CString.
207 /// Raises the `null_byte` condition if the receiver has an interior null.
208 fn to_c_str(&self) -> CString;
210 /// Unsafe variant of `to_c_str()` that doesn't check for nulls.
211 unsafe fn to_c_str_unchecked(&self) -> CString;
213 /// Work with a temporary CString constructed from the receiver.
214 /// The provided `*libc::c_char` will be freed immediately upon return.
221 /// let s = "PATH".with_c_str(|path| unsafe {
222 /// libc::getenv(path)
228 /// Raises the `null_byte` condition if the receiver has an interior null.
230 fn with_c_str<T>(&self, f: |*libc::c_char| -> T) -> T {
231 self.to_c_str().with_ref(f)
234 /// Unsafe variant of `with_c_str()` that doesn't check for nulls.
236 unsafe fn with_c_str_unchecked<T>(&self, f: |*libc::c_char| -> T) -> T {
237 self.to_c_str_unchecked().with_ref(f)
241 impl<'a> ToCStr for &'a str {
243 fn to_c_str(&self) -> CString {
244 self.as_bytes().to_c_str()
248 unsafe fn to_c_str_unchecked(&self) -> CString {
249 self.as_bytes().to_c_str_unchecked()
253 fn with_c_str<T>(&self, f: |*libc::c_char| -> T) -> T {
254 self.as_bytes().with_c_str(f)
258 unsafe fn with_c_str_unchecked<T>(&self, f: |*libc::c_char| -> T) -> T {
259 self.as_bytes().with_c_str_unchecked(f)
263 // The length of the stack allocated buffer for `vec.with_c_str()`
264 static BUF_LEN: uint = 128;
266 impl<'a> ToCStr for &'a [u8] {
267 fn to_c_str(&self) -> CString {
268 let mut cs = unsafe { self.to_c_str_unchecked() };
269 cs.with_mut_ref(|buf| check_for_null(*self, buf));
273 unsafe fn to_c_str_unchecked(&self) -> CString {
274 let self_len = self.len();
275 let buf = libc::malloc(self_len as libc::size_t + 1) as *mut u8;
277 fail!("failed to allocate memory!");
280 ptr::copy_memory(buf, self.as_ptr(), self_len);
281 *ptr::mut_offset(buf, self_len as int) = 0;
283 CString::new(buf as *libc::c_char, true)
286 fn with_c_str<T>(&self, f: |*libc::c_char| -> T) -> T {
287 unsafe { with_c_str(*self, true, f) }
290 unsafe fn with_c_str_unchecked<T>(&self, f: |*libc::c_char| -> T) -> T {
291 with_c_str(*self, false, f)
295 // Unsafe function that handles possibly copying the &[u8] into a stack array.
296 unsafe fn with_c_str<T>(v: &[u8], checked: bool, f: |*libc::c_char| -> T) -> T {
297 if v.len() < BUF_LEN {
298 let mut buf: [u8, .. BUF_LEN] = intrinsics::uninit();
299 vec::bytes::copy_memory(buf, v);
302 let buf = buf.as_mut_ptr();
304 check_for_null(v, buf as *mut libc::c_char);
307 f(buf as *libc::c_char)
309 v.to_c_str().with_ref(f)
311 v.to_c_str_unchecked().with_ref(f)
316 fn check_for_null(v: &[u8], buf: *mut libc::c_char) {
317 for i in range(0, v.len()) {
319 let p = buf.offset(i as int);
321 match null_byte::cond.raise(v.to_owned()) {
323 ReplaceWith(c) => *p = c
330 /// External iterator for a CString's bytes.
332 /// Use with the `std::iter` module.
333 pub struct CStringIterator<'a> {
334 priv ptr: *libc::c_char,
335 priv lifetime: &'a libc::c_char, // FIXME: #5922
338 impl<'a> Iterator<libc::c_char> for CStringIterator<'a> {
339 fn next(&mut self) -> Option<libc::c_char> {
340 let ch = unsafe { *self.ptr };
344 self.ptr = unsafe { ptr::offset(self.ptr, 1) };
350 /// Parses a C "multistring", eg windows env values or
351 /// the req->ptr result in a uv_fs_readdir() call.
353 /// Optionally, a `count` can be passed in, limiting the
354 /// parsing to only being done `count`-times.
356 /// The specified closure is invoked with each string that
357 /// is found, and the number of strings found is returned.
358 pub unsafe fn from_c_multistring(buf: *libc::c_char,
360 f: |&CString|) -> uint {
362 let mut curr_ptr: uint = buf as uint;
364 let (limited_count, limit) = match count {
365 Some(limit) => (true, limit),
368 while ((limited_count && ctr < limit) || !limited_count)
369 && *(curr_ptr as *libc::c_char) != 0 as libc::c_char {
370 let cstr = CString::new(curr_ptr as *libc::c_char, false);
372 curr_ptr += cstr.len() + 1;
383 use option::{Some, None};
386 fn test_str_multistring_parsing() {
388 let input = bytes!("zero", "\x00", "one", "\x00", "\x00");
389 let ptr = input.as_ptr();
390 let expected = ["zero", "one"];
391 let mut it = expected.iter();
392 let result = from_c_multistring(ptr as *libc::c_char, None, |c| {
393 let cbytes = c.as_bytes().slice_to(c.len());
394 assert_eq!(cbytes, it.next().unwrap().as_bytes());
396 assert_eq!(result, 2);
397 assert!(it.next().is_none());
402 fn test_str_to_c_str() {
403 "".to_c_str().with_ref(|buf| {
405 assert_eq!(*ptr::offset(buf, 0), 0);
409 "hello".to_c_str().with_ref(|buf| {
411 assert_eq!(*ptr::offset(buf, 0), 'h' as libc::c_char);
412 assert_eq!(*ptr::offset(buf, 1), 'e' as libc::c_char);
413 assert_eq!(*ptr::offset(buf, 2), 'l' as libc::c_char);
414 assert_eq!(*ptr::offset(buf, 3), 'l' as libc::c_char);
415 assert_eq!(*ptr::offset(buf, 4), 'o' as libc::c_char);
416 assert_eq!(*ptr::offset(buf, 5), 0);
422 fn test_vec_to_c_str() {
424 b.to_c_str().with_ref(|buf| {
426 assert_eq!(*ptr::offset(buf, 0), 0);
430 let _ = bytes!("hello").to_c_str().with_ref(|buf| {
432 assert_eq!(*ptr::offset(buf, 0), 'h' as libc::c_char);
433 assert_eq!(*ptr::offset(buf, 1), 'e' as libc::c_char);
434 assert_eq!(*ptr::offset(buf, 2), 'l' as libc::c_char);
435 assert_eq!(*ptr::offset(buf, 3), 'l' as libc::c_char);
436 assert_eq!(*ptr::offset(buf, 4), 'o' as libc::c_char);
437 assert_eq!(*ptr::offset(buf, 5), 0);
441 let _ = bytes!("foo", 0xff).to_c_str().with_ref(|buf| {
443 assert_eq!(*ptr::offset(buf, 0), 'f' as libc::c_char);
444 assert_eq!(*ptr::offset(buf, 1), 'o' as libc::c_char);
445 assert_eq!(*ptr::offset(buf, 2), 'o' as libc::c_char);
446 assert_eq!(*ptr::offset(buf, 3), 0xff as i8);
447 assert_eq!(*ptr::offset(buf, 4), 0);
454 let c_str = unsafe { CString::new(ptr::null(), false) };
455 assert!(c_str.is_null());
456 assert!(!c_str.is_not_null());
461 let c_str = "hello".to_c_str();
462 unsafe { libc::free(c_str.unwrap() as *libc::c_void) }
467 let c_str = "hello".to_c_str();
468 let len = unsafe { c_str.with_ref(|buf| libc::strlen(buf)) };
469 assert!(!c_str.is_null());
470 assert!(c_str.is_not_null());
476 fn test_with_ref_empty_fail() {
477 let c_str = unsafe { CString::new(ptr::null(), false) };
478 c_str.with_ref(|_| ());
483 let c_str = "".to_c_str();
484 let mut iter = c_str.iter();
485 assert_eq!(iter.next(), None);
487 let c_str = "hello".to_c_str();
488 let mut iter = c_str.iter();
489 assert_eq!(iter.next(), Some('h' as libc::c_char));
490 assert_eq!(iter.next(), Some('e' as libc::c_char));
491 assert_eq!(iter.next(), Some('l' as libc::c_char));
492 assert_eq!(iter.next(), Some('l' as libc::c_char));
493 assert_eq!(iter.next(), Some('o' as libc::c_char));
494 assert_eq!(iter.next(), None);
498 fn test_to_c_str_fail() {
499 use c_str::null_byte::cond;
501 let mut error_happened = false;
503 assert_eq!(err, bytes!("he", 0, "llo").to_owned())
504 error_happened = true;
506 }).inside(|| "he\x00llo".to_c_str());
507 assert!(error_happened);
510 ReplaceWith('?' as libc::c_char)
511 }).inside(|| "he\x00llo".to_c_str()).with_ref(|buf| {
513 assert_eq!(*buf.offset(0), 'h' as libc::c_char);
514 assert_eq!(*buf.offset(1), 'e' as libc::c_char);
515 assert_eq!(*buf.offset(2), '?' as libc::c_char);
516 assert_eq!(*buf.offset(3), 'l' as libc::c_char);
517 assert_eq!(*buf.offset(4), 'l' as libc::c_char);
518 assert_eq!(*buf.offset(5), 'o' as libc::c_char);
519 assert_eq!(*buf.offset(6), 0);
525 fn test_to_c_str_unchecked() {
527 "he\x00llo".to_c_str_unchecked().with_ref(|buf| {
528 assert_eq!(*buf.offset(0), 'h' as libc::c_char);
529 assert_eq!(*buf.offset(1), 'e' as libc::c_char);
530 assert_eq!(*buf.offset(2), 0);
531 assert_eq!(*buf.offset(3), 'l' as libc::c_char);
532 assert_eq!(*buf.offset(4), 'l' as libc::c_char);
533 assert_eq!(*buf.offset(5), 'o' as libc::c_char);
534 assert_eq!(*buf.offset(6), 0);
541 let c_str = "hello".to_c_str();
542 assert_eq!(c_str.as_bytes(), bytes!("hello", 0));
543 let c_str = "".to_c_str();
544 assert_eq!(c_str.as_bytes(), bytes!(0));
545 let c_str = bytes!("foo", 0xff).to_c_str();
546 assert_eq!(c_str.as_bytes(), bytes!("foo", 0xff, 0));
551 fn test_as_bytes_fail() {
552 let c_str = unsafe { CString::new(ptr::null(), false) };
558 let c_str = "hello".to_c_str();
559 assert_eq!(c_str.as_str(), Some("hello"));
560 let c_str = "".to_c_str();
561 assert_eq!(c_str.as_str(), Some(""));
562 let c_str = bytes!("foo", 0xff).to_c_str();
563 assert_eq!(c_str.as_str(), None);
564 let c_str = unsafe { CString::new(ptr::null(), false) };
565 assert_eq!(c_str.as_str(), None);
575 use extra::test::BenchHarness;
578 fn check(s: &str, c_str: *libc::c_char) {
579 let s_buf = s.as_ptr();
580 for i in range(0, s.len()) {
583 *ptr::offset(s_buf, i as int) as libc::c_char,
584 *ptr::offset(c_str, i as int));
589 static s_short: &'static str = "Mary";
590 static s_medium: &'static str = "Mary had a little lamb";
591 static s_long: &'static str = "\
592 Mary had a little lamb, Little lamb
593 Mary had a little lamb, Little lamb
594 Mary had a little lamb, Little lamb
595 Mary had a little lamb, Little lamb
596 Mary had a little lamb, Little lamb
597 Mary had a little lamb, Little lamb";
599 fn bench_to_str(bh: &mut BenchHarness, s: &str) {
601 let c_str = s.to_c_str();
602 c_str.with_ref(|c_str_buf| check(s, c_str_buf))
607 fn bench_to_c_str_short(bh: &mut BenchHarness) {
608 bench_to_str(bh, s_short)
612 fn bench_to_c_str_medium(bh: &mut BenchHarness) {
613 bench_to_str(bh, s_medium)
617 fn bench_to_c_str_long(bh: &mut BenchHarness) {
618 bench_to_str(bh, s_long)
621 fn bench_to_c_str_unchecked(bh: &mut BenchHarness, s: &str) {
623 let c_str = unsafe { s.to_c_str_unchecked() };
624 c_str.with_ref(|c_str_buf| check(s, c_str_buf))
629 fn bench_to_c_str_unchecked_short(bh: &mut BenchHarness) {
630 bench_to_c_str_unchecked(bh, s_short)
634 fn bench_to_c_str_unchecked_medium(bh: &mut BenchHarness) {
635 bench_to_c_str_unchecked(bh, s_medium)
639 fn bench_to_c_str_unchecked_long(bh: &mut BenchHarness) {
640 bench_to_c_str_unchecked(bh, s_long)
643 fn bench_with_c_str(bh: &mut BenchHarness, s: &str) {
645 s.with_c_str(|c_str_buf| check(s, c_str_buf))
650 fn bench_with_c_str_short(bh: &mut BenchHarness) {
651 bench_with_c_str(bh, s_short)
655 fn bench_with_c_str_medium(bh: &mut BenchHarness) {
656 bench_with_c_str(bh, s_medium)
660 fn bench_with_c_str_long(bh: &mut BenchHarness) {
661 bench_with_c_str(bh, s_long)
664 fn bench_with_c_str_unchecked(bh: &mut BenchHarness, s: &str) {
667 s.with_c_str_unchecked(|c_str_buf| check(s, c_str_buf))
673 fn bench_with_c_str_unchecked_short(bh: &mut BenchHarness) {
674 bench_with_c_str_unchecked(bh, s_short)
678 fn bench_with_c_str_unchecked_medium(bh: &mut BenchHarness) {
679 bench_with_c_str_unchecked(bh, s_medium)
683 fn bench_with_c_str_unchecked_long(bh: &mut BenchHarness) {
684 bench_with_c_str_unchecked(bh, s_long)