Skip to content

Commit 7b559d0

Browse files
committed
---
yaml --- r: 83509 b: refs/heads/try c: 2d87803 h: refs/heads/master i: 83507: 2d74c30 v: v3
1 parent 948f517 commit 7b559d0

File tree

2 files changed

+48
-1
lines changed

2 files changed

+48
-1
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
refs/heads/master: 0e4d1fc8cae42e15e00f71d9f439b01bb25a86ae
33
refs/heads/snap-stage1: e33de59e47c5076a89eadeb38f4934f58a3618a6
44
refs/heads/snap-stage3: 6c08cc2db4f98e9f07ae7d50338396c4123c2f0a
5-
refs/heads/try: 4868273d97958d832fc7d0681be096bb265ae6ba
5+
refs/heads/try: 2d878033fd77f13a41bb9142ba2a4e9b976d0089
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
77
refs/heads/ndm: f3868061cd7988080c30d6d5bf352a5a5fe2460b
88
refs/heads/try2: 147ecfdd8221e4a4d4e090486829a06da1e0ca3c

branches/try/src/libstd/c_str.rs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,11 @@ impl<'self> ToCStr for &'self str {
247247
fn with_c_str<T>(&self, f: &fn(*libc::c_char) -> T) -> T {
248248
self.as_bytes().with_c_str(f)
249249
}
250+
251+
#[inline]
252+
unsafe fn with_c_str_unchecked<T>(&self, f: &fn(*libc::c_char) -> T) -> T {
253+
self.as_bytes().with_c_str_unchecked(f)
254+
}
250255
}
251256

252257
// The length of the stack allocated buffer for `vec.with_c_str()`
@@ -297,6 +302,23 @@ impl<'self> ToCStr for &'self [u8] {
297302
self.to_c_str().with_ref(f)
298303
}
299304
}
305+
306+
unsafe fn with_c_str_unchecked<T>(&self, f: &fn(*libc::c_char) -> T) -> T {
307+
if self.len() < BUF_LEN {
308+
do self.as_imm_buf |self_buf, self_len| {
309+
let mut buf: [u8, .. BUF_LEN] = intrinsics::uninit();
310+
311+
do buf.as_mut_buf |buf, _| {
312+
ptr::copy_memory(buf, self_buf, self_len);
313+
*ptr::mut_offset(buf, self_len as int) = 0;
314+
315+
f(buf as *libc::c_char)
316+
}
317+
}
318+
} else {
319+
self.to_c_str().with_ref(f)
320+
}
321+
}
300322
}
301323

302324
#[inline]
@@ -616,4 +638,29 @@ mod bench {
616638
fn bench_with_c_str_long(bh: &mut BenchHarness) {
617639
bench_with_c_str(bh, s_long)
618640
}
641+
642+
fn bench_with_c_str_unchecked(bh: &mut BenchHarness, s: &str) {
643+
do bh.iter {
644+
unsafe {
645+
do s.with_c_str_unchecked |c_str_buf| {
646+
check(s, c_str_buf)
647+
}
648+
}
649+
}
650+
}
651+
652+
#[bench]
653+
fn bench_with_c_str_unchecked_short(bh: &mut BenchHarness) {
654+
bench_with_c_str_unchecked(bh, s_short)
655+
}
656+
657+
#[bench]
658+
fn bench_with_c_str_unchecked_medium(bh: &mut BenchHarness) {
659+
bench_with_c_str_unchecked(bh, s_medium)
660+
}
661+
662+
#[bench]
663+
fn bench_with_c_str_unchecked_long(bh: &mut BenchHarness) {
664+
bench_with_c_str_unchecked(bh, s_long)
665+
}
619666
}

0 commit comments

Comments
 (0)