about summary refs log tree commit diff
path: root/src/libstd
diff options
context:
space:
mode:
authorLzu Tao <taolzu@gmail.com>2019-12-29 15:47:13 +0000
committerLzu Tao <taolzu@gmail.com>2020-04-02 02:41:38 +0000
commit041e1704fcd9cc932a4fa587c43d32ed9dcb9712 (patch)
tree5acad32e86a582f2e9a8d9f5259d30530a446910 /src/libstd
parent76b11980ad416c3ad6143504c2277757ecacf9b5 (diff)
downloadrust-041e1704fcd9cc932a4fa587c43d32ed9dcb9712.tar.gz
rust-041e1704fcd9cc932a4fa587c43d32ed9dcb9712.zip
use of wmemchr for faster searching in [u16]
Diffstat (limited to 'src/libstd')
-rw-r--r--src/libstd/sys/windows/mod.rs14
1 files changed, 12 insertions, 2 deletions
diff --git a/src/libstd/sys/windows/mod.rs b/src/libstd/sys/windows/mod.rs
index 74dd41fd501..cb75e8122fd 100644
--- a/src/libstd/sys/windows/mod.rs
+++ b/src/libstd/sys/windows/mod.rs
@@ -81,10 +81,20 @@ pub fn decode_error_kind(errno: i32) -> ErrorKind {
     }
 }
 
+pub fn wmemchr(needle: u16, haystack: &[u16]) -> Option<usize> {
+    extern "C" {
+        fn wmemchr(s: *const u16, c: u16, n: usize) -> *mut u16;
+    }
+    let len = haystack.len();
+    let ptr = haystack.as_ptr();
+    let p = unsafe { wmemchr(ptr, needle, len) };
+    if p.is_null() { None } else { Some((p as usize - ptr as usize) / 2) }
+}
+
 pub fn to_u16s<S: AsRef<OsStr>>(s: S) -> crate::io::Result<Vec<u16>> {
     fn inner(s: &OsStr) -> crate::io::Result<Vec<u16>> {
         let mut maybe_result: Vec<u16> = s.encode_wide().collect();
-        if maybe_result.iter().any(|&u| u == 0) {
+        if wmemchr(0, &maybe_result).is_some() {
             return Err(crate::io::Error::new(
                 ErrorKind::InvalidInput,
                 "strings passed to WinAPI cannot contain NULs",
@@ -214,7 +224,7 @@ fn wide_char_to_multi_byte(
 }
 
 pub fn truncate_utf16_at_nul(v: &[u16]) -> &[u16] {
-    match v.iter().position(|c| *c == 0) {
+    match wmemchr(0, v) {
         // don't include the 0
         Some(i) => &v[..i],
         None => v,