]> git.lizzy.rs Git - rust.git/blobdiff - src/libstd/sys/windows/mod.rs
use of wmemchr for faster searching in [u16]
[rust.git] / src / libstd / sys / windows / mod.rs
index a515b382ab02af6017e344721e81910975678a21..cb75e8122fdd5a3595b271c9cef9af399f6f9276 100644 (file)
@@ -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,
@@ -262,12 +272,12 @@ pub fn dur2timeout(dur: Duration) -> c::DWORD {
 // terminating the process but without necessarily bypassing all exception
 // handlers.
 //
-// https://msdn.microsoft.com/en-us/library/dn774154.aspx
+// https://docs.microsoft.com/en-us/cpp/intrinsics/fastfail
 #[allow(unreachable_code)]
 pub unsafe fn abort_internal() -> ! {
     #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
     {
-        asm!("int $$0x29" :: "{ecx}"(7) ::: volatile); // 7 is FAST_FAIL_FATAL_APP_EXIT
+        llvm_asm!("int $$0x29" :: "{ecx}"(7) ::: volatile); // 7 is FAST_FAIL_FATAL_APP_EXIT
         crate::intrinsics::unreachable();
     }
     crate::intrinsics::abort();