Skip to content

Commit 7a6b0fb

Browse files
committed
---
yaml --- r: 82715 b: refs/heads/auto c: 2d87803 h: refs/heads/master i: 82713: 710e62d 82711: 9ac37af v: v3
1 parent 409dc62 commit 7a6b0fb

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
@@ -13,7 +13,7 @@ refs/heads/try3: 9387340aab40a73e8424c48fd42f0c521a4875c0
1313
refs/tags/release-0.3.1: 495bae036dfe5ec6ceafd3312b4dca48741e845b
1414
refs/tags/release-0.4: e828ea2080499553b97dfe33b3f4d472b4562ad7
1515
refs/tags/release-0.5: 7e3bcfbf21278251ee936ad53e92e9b719702d73
16-
refs/heads/auto: 4868273d97958d832fc7d0681be096bb265ae6ba
16+
refs/heads/auto: 2d878033fd77f13a41bb9142ba2a4e9b976d0089
1717
refs/heads/servo: af82457af293e2a842ba6b7759b70288da276167
1818
refs/tags/release-0.6: b4ebcfa1812664df5e142f0134a5faea3918544c
1919
refs/tags/0.1: b19db808c2793fe2976759b85a355c3ad8c8b336

branches/auto/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)